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
//! Youki/libcontainer runtime implementation
//!
//! Implements the Runtime trait using libcontainer (youki's container library)
//! for direct OCI container management without a daemon.
use crate::cgroups_stats::{self, ContainerStats};
use crate::error::{AgentError, Result};
use crate::runtime::{
ArchivePutOptions, ArchiveStream, ContainerId, ContainerState, ExecExitFuture, ExecHandle,
ExecOptions, ExecPtyStream, ImageInfo, LogChannel, LogChunk, LogsStream, LogsStreamOptions,
PathStat, PruneResult, PullProgress, PullProgressStream, Runtime, StatsSample, StatsStream,
};
use crate::storage_manager::StorageManager;
use libcontainer::container::builder::ContainerBuilder;
use libcontainer::container::{Container, ContainerStatus};
use libcontainer::signal::Signal;
use libcontainer::syscall::syscall::SyscallType;
use oci_client::manifest::OciImageManifest;
use std::collections::HashMap;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::fs;
use tokio::io::{AsyncBufReadExt, AsyncSeekExt, BufReader};
use tokio::sync::{mpsc, RwLock};
use tokio_stream::wrappers::ReceiverStream;
use tracing::instrument;
use zlayer_observability::logs::{LogEntry, LogSource, LogStream};
use zlayer_spec::{RegistryAuth, ServiceSpec};
/// Configuration for `YoukiRuntime`
#[derive(Debug, Clone)]
pub struct YoukiConfig {
/// State directory for libcontainer container state
pub state_dir: PathBuf,
/// Directory for unpacked image rootfs
pub rootfs_dir: PathBuf,
/// Directory for OCI bundles
pub bundle_dir: PathBuf,
/// Cache directory for image blobs
pub cache_dir: PathBuf,
/// Directory for persistent volumes
pub volume_dir: PathBuf,
/// Use systemd cgroups
pub use_systemd: bool,
/// Cache type configuration (if None, determined from environment)
pub cache_type: Option<zlayer_registry::CacheType>,
/// Base directory for structured container logs.
///
/// When set, container logs are written to
/// `{log_base_dir}/{deployment_name}/{service}/{container_id}.log`
/// instead of the bundle directory. The bundle `logs/` directory will
/// contain symlinks back to the structured location so that existing
/// code paths that read from the bundle still work.
pub log_base_dir: Option<PathBuf>,
/// Deployment name used in log directory hierarchy.
///
/// Only meaningful when `log_base_dir` is also set. Defaults to
/// `"default"` if unset.
pub deployment_name: Option<String>,
}
impl Default for YoukiConfig {
fn default() -> Self {
let dirs = zlayer_paths::ZLayerDirs::system_default();
Self {
state_dir: std::env::var("ZLAYER_STATE_DIR")
.map_or_else(|_| dirs.containers(), PathBuf::from),
rootfs_dir: std::env::var("ZLAYER_ROOTFS_DIR")
.map_or_else(|_| dirs.rootfs(), PathBuf::from),
bundle_dir: std::env::var("ZLAYER_BUNDLE_DIR")
.map_or_else(|_| dirs.bundles(), PathBuf::from),
cache_dir: std::env::var("ZLAYER_CACHE_DIR")
.map_or_else(|_| dirs.cache(), PathBuf::from),
volume_dir: std::env::var("ZLAYER_VOLUME_DIR")
.map_or_else(|_| dirs.volumes(), PathBuf::from),
use_systemd: std::env::var("ZLAYER_USE_SYSTEMD")
.is_ok_and(|v| v == "1" || v.to_lowercase() == "true"),
cache_type: None,
log_base_dir: None,
deployment_name: None,
}
}
}
/// Process-global mutex to serialize libcontainer operations.
/// libcontainer uses `chdir()` internally for notify socket operations
/// (to work around Unix socket 108-char path limit), which affects the
/// entire process. Concurrent container operations race on the CWD.
/// This must be process-global, not per-runtime, since chdir affects all threads.
static LIBCONTAINER_LOCK: std::sync::LazyLock<std::sync::Mutex<()>> =
std::sync::LazyLock::new(|| std::sync::Mutex::new(()));
/// Container tracking information
#[derive(Debug)]
struct ContainerInfo {
/// Image reference
#[allow(dead_code)]
image: String,
/// Bundle path
#[allow(dead_code)]
bundle_path: PathBuf,
/// Rootfs path
#[allow(dead_code)]
rootfs_path: PathBuf,
/// Stdout log file path
stdout_path: PathBuf,
/// Stderr log file path
stderr_path: PathBuf,
/// Process ID (once running)
#[allow(dead_code)]
pid: Option<u32>,
/// Most recent restart policy applied via
/// [`Runtime::update_container_resources`]. None when never updated.
/// Persisted across runtime restarts is not yet wired; this is the
/// in-memory copy that the supervisor consults on container exit.
#[allow(dead_code)]
restart_policy: Option<crate::runtime::ContainerRestartPolicyUpdate>,
}
/// Youki/libcontainer-based container runtime
///
/// This runtime uses libcontainer directly to create and manage OCI containers
/// without requiring a daemon like containerd.
pub struct YoukiRuntime {
/// Configuration
config: YoukiConfig,
/// Local container state tracking
containers: RwLock<HashMap<String, ContainerInfo>>,
/// Authentication resolver for registry pulls
auth_resolver: zlayer_core::AuthResolver,
/// Storage volume manager
storage_manager: std::sync::Arc<tokio::sync::RwLock<StorageManager>>,
/// Shared blob cache for image layers (avoids repeated opens and ensures cache persistence)
blob_cache: std::sync::Arc<Box<dyn zlayer_registry::BlobCacheBackend>>,
/// Local OCI registry for resolving locally-built images
local_registry: Option<std::sync::Arc<zlayer_registry::LocalRegistry>>,
/// Cached image configs (entrypoint, cmd, env, etc.) keyed by image reference
image_configs: RwLock<HashMap<String, zlayer_registry::ImageConfig>>,
/// Auth context for container-to-host API authentication.
auth_context: Option<crate::runtime::ContainerAuthContext>,
}
impl std::fmt::Debug for YoukiRuntime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("YoukiRuntime")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
impl YoukiRuntime {
/// Create a new `YoukiRuntime` with the given configuration
///
/// # Errors
/// Returns an error if the required directories cannot be created.
pub async fn new(
config: YoukiConfig,
auth_context: Option<crate::runtime::ContainerAuthContext>,
) -> Result<Self> {
// Ensure directories exist
for dir in [
&config.state_dir,
&config.rootfs_dir,
&config.bundle_dir,
&config.cache_dir,
] {
fs::create_dir_all(dir)
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to create directory {}: {}", dir.display(), e),
})?;
}
// Initialize storage manager
let storage_manager =
StorageManager::new(&config.volume_dir).map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to create storage manager: {e}"),
})?;
// Initialize shared blob cache using CacheType configuration
// If cache_type is provided, use it directly; otherwise use environment-based config
// but override the path for Persistent variant to use config.cache_dir
let blob_cache = if let Some(cache_type) = &config.cache_type {
cache_type
.build()
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to build blob cache: {e}"),
})?
} else {
let cache_type =
zlayer_registry::CacheType::from_env().map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to read cache config from env: {e}"),
})?;
// Override persistent path to use config.cache_dir
#[allow(clippy::match_wildcard_for_single_variants)]
let cache_type = match cache_type {
zlayer_registry::CacheType::Persistent { .. } => {
zlayer_registry::CacheType::persistent_at(config.cache_dir.join("blobs.redb"))
}
other => other,
};
cache_type
.build()
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to build blob cache: {e}"),
})?
};
let local_registry = {
let registry_path = config.cache_dir.parent().map_or_else(
|| config.cache_dir.join("registry"),
|data_dir| data_dir.join("registry"),
);
match zlayer_registry::LocalRegistry::new(registry_path).await {
Ok(reg) => Some(std::sync::Arc::new(reg)),
Err(e) => {
tracing::warn!("Failed to open local registry: {e}");
None
}
}
};
Ok(Self {
config,
containers: RwLock::new(HashMap::new()),
auth_resolver: zlayer_core::AuthResolver::new(zlayer_core::AuthConfig::default()),
storage_manager: std::sync::Arc::new(tokio::sync::RwLock::new(storage_manager)),
blob_cache,
local_registry,
image_configs: RwLock::new(HashMap::new()),
auth_context,
})
}
/// Create a new `YoukiRuntime` with default configuration
///
/// # Errors
/// Returns an error if the runtime cannot be initialized.
pub async fn with_defaults() -> Result<Self> {
Self::new(YoukiConfig::default(), None).await
}
/// Create a new `YoukiRuntime` with custom auth configuration
///
/// # Errors
/// Returns an error if the runtime cannot be initialized.
pub async fn with_auth(
config: YoukiConfig,
auth_config: zlayer_core::AuthConfig,
) -> Result<Self> {
// Ensure directories exist
for dir in [
&config.state_dir,
&config.rootfs_dir,
&config.bundle_dir,
&config.cache_dir,
] {
fs::create_dir_all(dir)
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to create directory {}: {}", dir.display(), e),
})?;
}
// Initialize storage manager
let storage_manager =
StorageManager::new(&config.volume_dir).map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to create storage manager: {e}"),
})?;
// Initialize shared blob cache using CacheType configuration
// If cache_type is provided, use it directly; otherwise use environment-based config
// but override the path for Persistent variant to use config.cache_dir
let blob_cache = if let Some(cache_type) = &config.cache_type {
cache_type
.build()
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to build blob cache: {e}"),
})?
} else {
let cache_type =
zlayer_registry::CacheType::from_env().map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to read cache config from env: {e}"),
})?;
// Override persistent path to use config.cache_dir
#[allow(clippy::match_wildcard_for_single_variants)]
let cache_type = match cache_type {
zlayer_registry::CacheType::Persistent { .. } => {
zlayer_registry::CacheType::persistent_at(config.cache_dir.join("blobs.redb"))
}
other => other,
};
cache_type
.build()
.await
.map_err(|e| AgentError::CreateFailed {
id: "runtime".to_string(),
reason: format!("failed to build blob cache: {e}"),
})?
};
let local_registry = {
let registry_path = config.cache_dir.parent().map_or_else(
|| config.cache_dir.join("registry"),
|data_dir| data_dir.join("registry"),
);
match zlayer_registry::LocalRegistry::new(registry_path).await {
Ok(reg) => Some(std::sync::Arc::new(reg)),
Err(e) => {
tracing::warn!("Failed to open local registry: {e}");
None
}
}
};
Ok(Self {
config,
containers: RwLock::new(HashMap::new()),
auth_resolver: zlayer_core::AuthResolver::new(auth_config),
storage_manager: std::sync::Arc::new(tokio::sync::RwLock::new(storage_manager)),
blob_cache,
local_registry,
image_configs: RwLock::new(HashMap::new()),
auth_context: None,
})
}
/// Configure S3-backed volume sync on the internal storage manager.
///
/// When set, named volumes will be automatically registered with the
/// `LayerSyncManager` on creation and restored from S3 if a backup
/// exists. Volumes are synced to S3 when containers are stopped via
/// the `sync_container_volumes` trait method.
#[cfg(feature = "s3")]
pub async fn set_layer_sync(
&self,
sync: std::sync::Arc<zlayer_storage::sync::LayerSyncManager>,
service_name: impl Into<String>,
) {
let mut storage_manager = self.storage_manager.write().await;
storage_manager.set_layer_sync(sync, service_name);
}
/// Get the container ID string
#[allow(clippy::unused_self)]
fn container_id_str(&self, id: &ContainerId) -> String {
format!("{}-{}", id.service, id.replica)
}
/// Get the root path for a container's state
fn container_root(&self, id: &ContainerId) -> PathBuf {
self.config.state_dir.join(self.container_id_str(id))
}
/// Get the bundle path for a container
fn bundle_path(&self, id: &ContainerId) -> PathBuf {
self.config.bundle_dir.join(self.container_id_str(id))
}
/// Get log directory for a container.
///
/// When `log_base_dir` is configured, returns a structured path:
/// `{log_base_dir}/{deployment}/{service}/`
/// Otherwise falls back to the bundle directory:
/// `{bundle_dir}/{container_id}/logs/`
fn log_dir(&self, id: &ContainerId) -> PathBuf {
if let Some(ref base) = self.config.log_base_dir {
let deployment = self.config.deployment_name.as_deref().unwrap_or("default");
base.join(deployment).join(&id.service)
} else {
// Fall back to bundle directory to avoid conflicting with libcontainer's state directory
self.bundle_path(id).join("logs")
}
}
/// Get log file paths for a container.
///
/// Returns `(stdout_path, stderr_path)`. When structured logging is
/// enabled via `log_base_dir`, the files are named after the container
/// ID (e.g. `myservice-rep-1.stdout.log`).
fn log_paths(&self, id: &ContainerId) -> (PathBuf, PathBuf) {
let log_dir = self.log_dir(id);
if self.config.log_base_dir.is_some() {
let container_id = self.container_id_str(id);
(
log_dir.join(format!("{container_id}.stdout.log")),
log_dir.join(format!("{container_id}.stderr.log")),
)
} else {
(log_dir.join("stdout.log"), log_dir.join("stderr.log"))
}
}
/// Map libcontainer status to our `ContainerState`
#[allow(clippy::unused_self)]
fn map_status(&self, status: ContainerStatus) -> ContainerState {
match status {
ContainerStatus::Creating | ContainerStatus::Created => ContainerState::Pending,
ContainerStatus::Running => ContainerState::Running,
ContainerStatus::Stopped => ContainerState::Exited { code: 0 },
ContainerStatus::Paused => ContainerState::Stopping,
}
}
/// Create log files and return file descriptors for stdout/stderr.
///
/// When `log_base_dir` is configured, creates the structured log
/// directory (`/var/log/zlayer/{deployment}/{service}/`) and places
/// symlinks in the bundle's `logs/` directory so that existing code
/// reading from the bundle still works.
#[allow(unsafe_code)]
async fn create_log_files(
&self,
id: &ContainerId,
) -> Result<(PathBuf, PathBuf, OwnedFd, OwnedFd)> {
let log_dir = self.log_dir(id);
fs::create_dir_all(&log_dir)
.await
.map_err(|e| AgentError::CreateFailed {
id: id.to_string(),
reason: format!("failed to create log dir: {e}"),
})?;
let (stdout_path, stderr_path) = self.log_paths(id);
// Create stdout file
let stdout_file =
std::fs::File::create(&stdout_path).map_err(|e| AgentError::CreateFailed {
id: id.to_string(),
reason: format!("failed to create stdout log: {e}"),
})?;
// Create stderr file
let stderr_file =
std::fs::File::create(&stderr_path).map_err(|e| AgentError::CreateFailed {
id: id.to_string(),
reason: format!("failed to create stderr log: {e}"),
})?;
// When using structured logging, also create symlinks from the bundle
// logs directory so that code reading from bundle_path/logs/ still works.
if self.config.log_base_dir.is_some() {
let bundle_log_dir = self.bundle_path(id).join("logs");
let _ = fs::create_dir_all(&bundle_log_dir).await;
// Symlink bundle_log_dir/stdout.log -> structured stdout_path
let bundle_stdout = bundle_log_dir.join("stdout.log");
let _ = fs::remove_file(&bundle_stdout).await;
if let Err(e) = tokio::fs::symlink(&stdout_path, &bundle_stdout).await {
tracing::warn!(
container = %id,
error = %e,
"Failed to create stdout symlink in bundle logs dir"
);
}
// Symlink bundle_log_dir/stderr.log -> structured stderr_path
let bundle_stderr = bundle_log_dir.join("stderr.log");
let _ = fs::remove_file(&bundle_stderr).await;
if let Err(e) = tokio::fs::symlink(&stderr_path, &bundle_stderr).await {
tracing::warn!(
container = %id,
error = %e,
"Failed to create stderr symlink in bundle logs dir"
);
}
}
// Convert to OwnedFd
// SAFETY: `into_raw_fd()` transfers ownership of the fd, and
// `OwnedFd::from_raw_fd` takes ownership. No double-close.
let stdout_fd = unsafe {
use std::os::unix::io::IntoRawFd;
OwnedFd::from_raw_fd(stdout_file.into_raw_fd())
};
let stderr_fd = unsafe {
use std::os::unix::io::IntoRawFd;
OwnedFd::from_raw_fd(stderr_file.into_raw_fd())
};
Ok((stdout_path, stderr_path, stdout_fd, stderr_fd))
}
/// Clean up bundle directory for a container
async fn cleanup_bundle(&self, id: &ContainerId) -> Result<()> {
let bundle_path = self.bundle_path(id);
if bundle_path.exists() {
fs::remove_dir_all(&bundle_path)
.await
.map_err(|e| AgentError::CreateFailed {
id: id.to_string(),
reason: format!("failed to remove bundle: {e}"),
})?;
}
Ok(())
}
/// Pull image layers and return them for extraction
///
/// Uses the shared blob cache to avoid repeated network requests for cached layers.
/// The `policy` parameter is translated to a `force_refresh` flag: `PullPolicy::Always`
/// clears the manifest cache before fetching, while `IfNotPresent` and `Never` reuse
/// any cached manifest (the puller still revalidates mutable tags via HEAD for
/// `IfNotPresent`, and serves purely from cache for `Never`).
async fn pull_image_layers(
&self,
image: &str,
policy: zlayer_spec::PullPolicy,
) -> Result<Vec<(Vec<u8>, String)>> {
// Use the shared blob cache instead of opening a new one each time
let puller = {
let p = zlayer_registry::ImagePuller::with_cache(self.blob_cache.clone());
if let Some(ref registry) = self.local_registry {
p.with_local_registry(registry.clone())
} else {
p
}
};
let auth = self.auth_resolver.resolve(image);
let force_refresh = matches!(policy, zlayer_spec::PullPolicy::Always);
puller
.pull_image_with_policy(image, &auth, force_refresh)
.await
.map_err(|e| AgentError::PullFailed {
image: image.to_string(),
reason: format!("failed to pull image: {e}"),
})
}
/// Prepare storage volumes for a container, returning paths for mounts
async fn prepare_storage_volumes(
&self,
id: &ContainerId,
spec: &ServiceSpec,
) -> Result<std::collections::HashMap<String, PathBuf>> {
use zlayer_spec::StorageSpec;
let mut volume_paths = std::collections::HashMap::new();
let container_id = self.container_id_str(id);
let mut storage_manager = self.storage_manager.write().await;
for storage in &spec.storage {
match storage {
StorageSpec::Named { name, .. } => {
let path = storage_manager
.ensure_volume_with_sync(name)
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("failed to ensure volume '{name}': {e}"),
})?;
storage_manager
.attach_volume(name, &container_id)
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("failed to attach volume '{name}': {e}"),
})?;
volume_paths.insert(name.clone(), path);
}
StorageSpec::Anonymous { target, .. } => {
let path = storage_manager
.create_anonymous(&container_id, target)
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!(
"failed to create anonymous volume for '{target}': {e}"
),
})?;
let key = format!("_anon_{}", target.trim_start_matches('/').replace('/', "_"));
volume_paths.insert(key, path);
}
// Bind and tmpfs mounts don't need preparation
StorageSpec::Bind { .. } | StorageSpec::Tmpfs { .. } => {}
StorageSpec::S3 {
bucket,
prefix,
endpoint,
..
} => {
let path = storage_manager
.mount_s3(
bucket,
prefix.as_deref(),
endpoint.as_deref(),
&container_id,
)
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("failed to mount S3 bucket '{bucket}': {e}"),
})?;
let key = format!("_s3_{}_{}", bucket, prefix.as_deref().unwrap_or(""));
volume_paths.insert(key, path);
}
}
}
Ok(volume_paths)
}
/// Clean up storage volumes for a container
///
/// Note: This method requires the `ServiceSpec` to know which volumes to clean up.
/// For now, `remove_container` uses a simpler approach that only cleans up anonymous volumes.
/// This method is available for future use when the spec is stored/available at removal time.
#[allow(dead_code)]
async fn cleanup_storage_volumes(&self, id: &ContainerId, spec: &ServiceSpec) -> Result<()> {
use zlayer_spec::StorageSpec;
let container_id = self.container_id_str(id);
let mut storage_manager = self.storage_manager.write().await;
// Detach named volumes
for storage in &spec.storage {
match storage {
StorageSpec::Named { name, .. } => {
if let Err(e) = storage_manager.detach_volume(name, &container_id) {
tracing::warn!(
volume = %name,
container = %container_id,
error = %e,
"failed to detach volume"
);
}
}
StorageSpec::S3 { bucket, prefix, .. } => {
if let Err(e) =
storage_manager.unmount_s3(bucket, prefix.as_deref(), &container_id)
{
tracing::warn!(
bucket = %bucket,
container = %container_id,
error = %e,
"failed to unmount S3 bucket"
);
}
}
_ => {}
}
}
// Clean up anonymous volumes
if let Err(e) = storage_manager.cleanup_anonymous(&container_id) {
tracing::warn!(
container = %container_id,
error = %e,
"failed to cleanup anonymous volumes"
);
}
Ok(())
}
/// Get a cached image config by image reference
///
/// Returns the previously pulled image configuration (entrypoint, cmd, env, etc.)
/// for the given image reference, if available.
async fn get_image_config(&self, image: &str) -> Option<zlayer_registry::ImageConfig> {
let configs = self.image_configs.read().await;
configs.get(image).cloned()
}
}
#[async_trait::async_trait]
impl Runtime for YoukiRuntime {
/// Pull an image to local storage
///
/// Downloads image layers from a registry and unpacks them to a rootfs.
#[instrument(
skip(self),
fields(
otel.name = "image.pull",
container.image.name = %image,
)
)]
async fn pull_image(&self, image: &str) -> Result<()> {
self.pull_image_with_policy(image, zlayer_spec::PullPolicy::IfNotPresent, None)
.await
}
/// Pull an image to local storage with a specific pull policy
///
/// This downloads image layers to the blob cache. Layers are extracted
/// per-container in `create_container` to avoid race conditions.
///
/// The `_auth` parameter is accepted for trait conformance (§3.10) but
/// currently ignored: `zlayer-registry` resolves credentials through the
/// existing `AuthResolver` (hostname lookup in the persistent secret
/// store). Callers that need inline auth should use the Docker runtime.
#[instrument(
skip(self, _auth),
fields(
otel.name = "image.pull",
container.image.name = %image,
pull_policy = ?policy,
)
)]
async fn pull_image_with_policy(
&self,
image: &str,
policy: zlayer_spec::PullPolicy,
_auth: Option<&RegistryAuth>,
) -> Result<()> {
let puller = {
let p = zlayer_registry::ImagePuller::with_cache(self.blob_cache.clone());
if let Some(ref registry) = self.local_registry {
p.with_local_registry(registry.clone())
} else {
p
}
};
let auth = self.auth_resolver.resolve(image);
// For Never policy, skip pulling layers from the remote, but STILL
// fetch the image config from the local blob cache (populated by a
// prior `zlayer import` or an earlier pull). Without the image
// config, the bundle builder has no way to know the image's
// entrypoint/cmd/env/workdir/user and falls back to `/bin/sh`,
// which exits immediately and kills the container. Fetching the
// config from cache is cheap (~1 KB) and non-fatal on miss.
if matches!(policy, zlayer_spec::PullPolicy::Never) {
tracing::debug!(image = %image, "pull policy is Never, skipping layer pull");
match puller.pull_image_config(image, &auth).await {
Ok(config) => {
tracing::info!(
image = %image,
has_entrypoint = config.entrypoint.is_some(),
has_cmd = config.cmd.is_some(),
"image config loaded from cache"
);
let mut configs = self.image_configs.write().await;
configs.insert(image.to_string(), config);
}
Err(e) => {
tracing::warn!(
image = %image,
error = %e,
"failed to load image config from cache under pull_policy=Never, container will use spec defaults"
);
}
}
return Ok(());
}
// For IfNotPresent, check if image layers are in cache by trying to pull
// Use the shared blob cache to avoid repeated opens and ensure persistence
// For Always, force a round-trip to the registry by clearing the manifest cache.
let force_refresh = matches!(policy, zlayer_spec::PullPolicy::Always);
tracing::info!(image = %image, force_refresh, "pulling image layers to cache");
// Pull image layers from registry (cached layers are retrieved from cache)
let layers = puller
.pull_image_with_policy(image, &auth, force_refresh)
.await
.map_err(|e| AgentError::PullFailed {
image: image.to_string(),
reason: format!("failed to pull image: {e}"),
})?;
tracing::info!(
image = %image,
layer_count = layers.len(),
"image layers cached"
);
// Also pull and cache the image config (entrypoint, cmd, env, etc.)
match puller
.pull_image_config_with_policy(image, &auth, force_refresh)
.await
{
Ok(config) => {
tracing::info!(
image = %image,
has_entrypoint = config.entrypoint.is_some(),
has_cmd = config.cmd.is_some(),
"image config cached"
);
let mut configs = self.image_configs.write().await;
configs.insert(image.to_string(), config);
}
Err(e) => {
// Log but don't fail - the container can still run with spec defaults
tracing::warn!(
image = %image,
error = %e,
"failed to pull image config, container will use spec defaults"
);
}
}
Ok(())
}
/// Create a container
///
/// Creates an OCI bundle and uses libcontainer to create the container.
/// Each container gets its own rootfs extracted from cached layers.
#[instrument(
skip(self, spec),
fields(
otel.name = "container.create",
container.id = %self.container_id_str(id),
service.name = %id.service,
service.replica = %id.replica,
container.image.name = %spec.image.name,
)
)]
async fn create_container(&self, id: &ContainerId, spec: &ServiceSpec) -> Result<()> {
let container_id = self.container_id_str(id);
let image = spec.image.name.to_string();
// Clean up any stale container from a previous deploy (mirrors Docker runtime behavior)
// See docker.rs:370-402 for the equivalent pattern
let container_root = self.container_root(id);
if container_root.exists() {
tracing::warn!(
container = %container_id,
"stale container state found from previous deploy, cleaning up before re-create"
);
// Try to stop — process may already be dead, ignore errors
if let Err(e) = self.stop_container(id, Duration::from_secs(5)).await {
tracing::debug!(
container = %container_id,
error = %e,
"stop_container during stale cleanup (expected if process already dead)"
);
}
// Remove container — does full cleanup: libcontainer delete + state dir + bundle + volumes
if let Err(e) = self.remove_container(id).await {
tracing::warn!(
container = %container_id,
error = %e,
"remove_container failed during stale cleanup, attempting manual cleanup"
);
// Fall back to manual cleanup if remove_container fails
if container_root.exists() {
let _ = tokio::fs::remove_dir_all(&container_root).await;
}
let stale_bundle = self.bundle_path(id);
if stale_bundle.exists() {
let _ = tokio::fs::remove_dir_all(&stale_bundle).await;
}
}
tracing::info!(container = %container_id, "stale container cleaned up");
}
// Also clean up stale bundle directory if it exists but state dir didn't
let bundle_path = self.bundle_path(id);
if bundle_path.exists() {
tracing::warn!(container = %container_id, "stale bundle directory found, removing");
let _ = tokio::fs::remove_dir_all(&bundle_path).await;
}
let bundle_path = self.bundle_path(id);
let rootfs_path = bundle_path.join("rootfs");
tracing::info!("Creating container {} from image {}", container_id, image);
// Create bundle directory structure
fs::create_dir_all(&bundle_path)
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("failed to create bundle directory: {e}"),
})?;
// Pull image layers (from cache if available). Honor the spec's pull policy so
// that `Always` forces a manifest refresh, `IfNotPresent` serves from cache with
// mutable-tag revalidation, and `Never` reuses cached blobs.
let layers = self
.pull_image_layers(&image, spec.image.pull_policy)
.await?;
tracing::debug!(
container = %container_id,
layer_count = layers.len(),
"extracting layers to container rootfs"
);
// Extract layers to this container's own rootfs
let mut unpacker = zlayer_registry::LayerUnpacker::new(rootfs_path.clone());
unpacker
.unpack_layers(&layers)
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("failed to extract rootfs: {e}"),
})?;
// Log rootfs diagnostics for debugging container creation failures
match std::fs::read_dir(&rootfs_path) {
Ok(entries) => {
let count = entries.count();
tracing::info!(
container = %container_id,
rootfs = %rootfs_path.display(),
entry_count = count,
"rootfs extracted successfully"
);
}
Err(e) => {
tracing::warn!(
container = %container_id,
rootfs = %rootfs_path.display(),
error = %e,
"rootfs directory not readable after extraction"
);
}
}
// Get cached image config (entrypoint, cmd, env, workdir, user)
let img_config = self.get_image_config(&image).await;
// Prepare storage volumes
let volume_paths = self.prepare_storage_volumes(id, spec).await?;
// Generate OCI config.json via BundleBuilder (handles capabilities, devices,
// resource limits, storage mounts, env resolution, and command resolution)
let mut bundle_builder = crate::bundle::BundleBuilder::new(bundle_path.clone())
.with_volume_paths(volume_paths)
.with_host_network(spec.host_network);
if let Some(config) = img_config {
bundle_builder = bundle_builder.with_image_config(config);
}
// Inject auth env vars and socket mount so the container can talk to the host API
if let Some(ref auth_ctx) = self.auth_context {
let token = crate::auth::mint_container_token(
&auth_ctx.jwt_secret,
&id.service,
&format!("{}-{}", id.service, id.replica),
std::time::Duration::from_secs(86400 * 365),
)
.map_err(|e| crate::error::AgentError::CreateFailed {
id: id.to_string(),
reason: format!("Failed to mint container token: {e}"),
})?;
bundle_builder = bundle_builder
.with_env("ZLAYER_API_URL".to_string(), auth_ctx.api_url.clone())
.with_env("ZLAYER_TOKEN".to_string(), token)
.with_env(
"ZLAYER_SOCKET".to_string(),
zlayer_paths::ZLayerDirs::default_socket_path(),
)
.with_socket_mount(&auth_ctx.socket_path);
}
bundle_builder.write_config(id, spec).await?;
// Create log files
let (stdout_path, stderr_path, stdout_fd, stderr_fd) = self.create_log_files(id).await?;
// Use spawn_blocking for the synchronous libcontainer operations
let config = self.config.clone();
let container_id_clone = container_id.clone();
let bundle_path_clone = bundle_path.clone();
// Use state_dir as the root path - libcontainer appends container_id internally
let state_dir_clone = self.config.state_dir.clone();
let _container = tokio::task::spawn_blocking(move || {
// Acquire process-global lock to serialize libcontainer operations.
// libcontainer uses chdir() internally which affects the entire process,
// so concurrent operations would race on the working directory.
let _guard = LIBCONTAINER_LOCK
.lock()
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to acquire libcontainer lock: {e}"),
})?;
// Create container using libcontainer
// Set stdout/stderr on ContainerBuilder BEFORE calling as_init()
let container_builder =
ContainerBuilder::new(container_id_clone.clone(), SyscallType::Linux)
.with_stdout(stdout_fd)
.with_stderr(stderr_fd);
// Set container root path (base dir - libcontainer creates <root>/<container_id>)
let container_builder =
container_builder
.with_root_path(&state_dir_clone)
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to set root path: {e}"),
})?;
// Configure as init container (creates new namespaces)
let init_builder = container_builder
.as_init(&bundle_path_clone)
.with_systemd(config.use_systemd)
.with_detach(true); // Run detached
// Build the container (creates it but doesn't start)
let container = init_builder.build().map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to create container: {e:?}"),
})?;
Ok::<Container, AgentError>(container)
})
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
// Store container info
{
let mut containers = self.containers.write().await;
containers.insert(
container_id.clone(),
ContainerInfo {
image: image.clone(),
bundle_path,
rootfs_path,
stdout_path,
stderr_path,
pid: None,
restart_policy: None,
},
);
}
tracing::info!("Container {} created successfully", container_id);
Ok(())
}
/// Start a container
///
/// Starts the container's init process.
#[instrument(
skip(self),
fields(
otel.name = "container.start",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn start_container(&self, id: &ContainerId) -> Result<()> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
tracing::info!("Starting container {}", container_id);
// Load and start the container using spawn_blocking
let pid = tokio::task::spawn_blocking(move || {
// Acquire process-global lock to serialize libcontainer operations.
// libcontainer uses chdir() internally which affects the entire process,
// so concurrent operations would race on the working directory.
let _guard = LIBCONTAINER_LOCK
.lock()
.map_err(|e| AgentError::StartFailed {
id: container_id.clone(),
reason: format!("failed to acquire libcontainer lock: {e}"),
})?;
let mut container =
Container::load(container_root).map_err(|e| AgentError::StartFailed {
id: container_id.clone(),
reason: format!("failed to load container: {e}"),
})?;
// Start the container
container.start().map_err(|e| AgentError::StartFailed {
id: container_id.clone(),
reason: format!("failed to start container: {e}"),
})?;
// Get the PID after starting - access through state
#[allow(clippy::cast_sign_loss)]
let pid = container.pid().map(|p| p.as_raw() as u32);
Ok::<Option<u32>, AgentError>(pid)
})
.await
.map_err(|e| AgentError::StartFailed {
id: id.to_string(),
reason: format!("task join error: {e}"),
})??;
// Update container info with PID
{
let mut containers = self.containers.write().await;
if let Some(info) = containers.get_mut(&self.container_id_str(id)) {
info.pid = pid;
}
}
tracing::info!(
"Container {} started with PID {:?}",
self.container_id_str(id),
pid
);
Ok(())
}
/// Stop a container
///
/// Sends SIGTERM, waits for timeout, then sends SIGKILL if needed.
#[instrument(
skip(self),
fields(
otel.name = "container.stop",
container.id = %self.container_id_str(id),
service.name = %id.service,
timeout_ms = %timeout.as_millis(),
)
)]
async fn stop_container(&self, id: &ContainerId, timeout: Duration) -> Result<()> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
tracing::info!(
"Stopping container {} with {:?} timeout",
container_id,
timeout
);
// Send SIGTERM first
let container_root_clone = container_root.clone();
let container_id_clone = container_id.clone();
tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root_clone).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
// Check if container can be killed
if container.status().can_kill() {
// Send SIGTERM
use std::convert::TryFrom;
let signal = Signal::try_from("SIGTERM").map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("invalid signal: {e:?}"),
})?;
if let Err(e) = container.kill(signal, true) {
tracing::debug!("SIGTERM failed (container may already be stopped): {}", e);
}
}
Ok::<(), AgentError>(())
})
.await
.map_err(|e| AgentError::NotFound {
container: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
// Wait for container to stop
let start = std::time::Instant::now();
loop {
if start.elapsed() > timeout {
break;
}
// Check container state
let state = self.container_state(id).await?;
if matches!(
state,
ContainerState::Exited { .. } | ContainerState::Failed { .. }
) {
tracing::info!("Container {} stopped gracefully", container_id);
return Ok(());
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
// Timeout exceeded - send SIGKILL
tracing::debug!(
"Container {} did not stop gracefully, sending SIGKILL",
container_id
);
let container_root_clone = container_root.clone();
let container_id_clone = container_id.clone();
tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root_clone).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
if container.status().can_kill() {
use std::convert::TryFrom;
let signal = Signal::try_from("SIGKILL").map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("invalid signal: {e:?}"),
})?;
if let Err(e) = container.kill(signal, true) {
tracing::warn!("SIGKILL failed: {}", e);
}
}
Ok::<(), AgentError>(())
})
.await
.map_err(|e| AgentError::NotFound {
container: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
tracing::info!("Container {} killed", container_id);
Ok(())
}
/// Remove a container
///
/// Deletes the container and cleans up its bundle and state.
/// Cleanup always proceeds even if libcontainer operations fail.
#[instrument(
skip(self),
fields(
otel.name = "container.remove",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn remove_container(&self, id: &ContainerId) -> Result<()> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
tracing::info!("Removing container {}", container_id);
// Attempt libcontainer delete, but don't fail if container not found
let container_id_clone = container_id.clone();
let container_root_clone = container_root.clone();
let libcontainer_result = tokio::task::spawn_blocking(move || {
match Container::load(container_root_clone) {
Ok(mut container) => {
// Delete with force=true to handle any state
if let Err(e) = container.delete(true) {
tracing::warn!(
"libcontainer delete failed for {}: {}",
container_id_clone,
e
);
}
}
Err(e) => {
// Container may already be gone or state is in unexpected location
tracing::warn!(
"Container::load failed for {} (may already be removed): {}",
container_id_clone,
e
);
}
}
})
.await;
if let Err(e) = libcontainer_result {
tracing::warn!("spawn_blocking failed during remove: {}", e);
}
// ALWAYS clean up bundle regardless of libcontainer result
if let Err(e) = self.cleanup_bundle(id).await {
tracing::warn!("Failed to cleanup bundle for {}: {}", container_id, e);
}
// ALWAYS clean up state directory regardless of libcontainer result
let state_dir = self.container_root(id);
if state_dir.exists() {
if let Err(e) = fs::remove_dir_all(&state_dir).await {
tracing::warn!("Failed to remove state dir {}: {}", state_dir.display(), e);
}
}
// Clean up storage volumes
// Note: We need the spec to know what to clean up, but we don't have it here
// For now, we'll just clean up anonymous volumes by container ID
{
let mut storage_manager = self.storage_manager.write().await;
if let Err(e) = storage_manager.cleanup_anonymous(&container_id) {
tracing::warn!(
container = %container_id,
error = %e,
"failed to cleanup anonymous volumes"
);
}
}
// Remove from local tracking
{
let mut containers = self.containers.write().await;
containers.remove(&container_id);
}
tracing::info!("Container {} removed", container_id);
Ok(())
}
/// Get container state
#[instrument(
skip(self),
fields(
otel.name = "container.state",
container.id = %self.container_id_str(id),
)
)]
async fn container_state(&self, id: &ContainerId) -> Result<ContainerState> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
// Check if container root exists
if !container_root.exists() {
return Err(AgentError::NotFound {
container: container_id.clone(),
reason: "container state directory not found".to_string(),
});
}
// Load container and get status
let container_id_clone = container_id.clone();
let status = tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
// Refresh status to get current state
let _ = container.refresh_status();
Ok::<ContainerStatus, AgentError>(container.status())
})
.await
.map_err(|e| AgentError::NotFound {
container: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
Ok(self.map_status(status))
}
/// Get container logs
///
/// Reads from the container's stdout/stderr log files.
async fn container_logs(&self, id: &ContainerId, tail: usize) -> Result<Vec<LogEntry>> {
let container_id = self.container_id_str(id);
// Get log paths from local state
let (stdout_path, stderr_path) = {
let containers = self.containers.read().await;
match containers.get(&container_id) {
Some(info) => (info.stdout_path.clone(), info.stderr_path.clone()),
None => {
// Fall back to default paths
self.log_paths(id)
}
}
};
let now = chrono::Utc::now();
let source = LogSource::Container(id.to_string());
let mut entries = Vec::new();
// Read stdout
if stdout_path.exists() {
if let Ok(content) = fs::read_to_string(&stdout_path).await {
for line in content.lines() {
entries.push(LogEntry {
timestamp: now,
stream: LogStream::Stdout,
message: line.to_string(),
source: source.clone(),
service: None,
deployment: None,
});
}
}
}
// Read stderr
if stderr_path.exists() {
if let Ok(content) = fs::read_to_string(&stderr_path).await {
for line in content.lines() {
entries.push(LogEntry {
timestamp: now,
stream: LogStream::Stderr,
message: line.to_string(),
source: source.clone(),
service: None,
deployment: None,
});
}
}
}
// Sort by timestamp (all same for legacy files, but correct for future use)
entries.sort_by_key(|e| e.timestamp);
// Apply tail limit
if tail > 0 && entries.len() > tail {
entries = entries.split_off(entries.len() - tail);
}
Ok(entries)
}
/// Execute a command in a running container
///
/// Uses libcontainer's tenant builder to exec into the container's namespaces.
#[allow(unsafe_code)]
#[instrument(
skip(self),
fields(
otel.name = "container.exec",
container.id = %self.container_id_str(id),
command = ?cmd,
)
)]
async fn exec(&self, id: &ContainerId, cmd: &[String]) -> Result<(i32, String, String)> {
let container_id = self.container_id_str(id);
if cmd.is_empty() {
return Err(AgentError::InvalidSpec(
"exec command cannot be empty".to_string(),
));
}
tracing::debug!("Executing {:?} in container {}", cmd, container_id);
// Create temporary files for exec output
let exec_id = uuid::Uuid::new_v4().to_string();
let exec_dir = self.config.state_dir.join(format!("exec-{exec_id}"));
fs::create_dir_all(&exec_dir)
.await
.map_err(|e| AgentError::CreateFailed {
id: exec_id.clone(),
reason: format!("failed to create exec dir: {e}"),
})?;
let stdout_path = exec_dir.join("stdout");
let stderr_path = exec_dir.join("stderr");
// Create output files
let stdout_file =
std::fs::File::create(&stdout_path).map_err(|e| AgentError::CreateFailed {
id: exec_id.clone(),
reason: format!("failed to create stdout file: {e}"),
})?;
let stderr_file =
std::fs::File::create(&stderr_path).map_err(|e| AgentError::CreateFailed {
id: exec_id.clone(),
reason: format!("failed to create stderr file: {e}"),
})?;
// SAFETY: `into_raw_fd()` transfers ownership of the fd, and
// `OwnedFd::from_raw_fd` takes ownership. No double-close.
let stdout_fd = unsafe {
use std::os::unix::io::IntoRawFd;
OwnedFd::from_raw_fd(stdout_file.into_raw_fd())
};
let stderr_fd = unsafe {
use std::os::unix::io::IntoRawFd;
OwnedFd::from_raw_fd(stderr_file.into_raw_fd())
};
let cmd_clone = cmd.to_vec();
let container_id_clone = container_id.clone();
// Use state_dir as the root path - libcontainer expects base dir
let state_dir_clone = self.config.state_dir.clone();
// Execute using tenant builder
let exec_pid = tokio::task::spawn_blocking(move || {
// Create container builder for tenant (joining existing container)
// Set stdout/stderr on ContainerBuilder BEFORE calling as_tenant()
let container_builder =
ContainerBuilder::new(container_id_clone.clone(), SyscallType::Linux)
.with_stdout(stdout_fd)
.with_stderr(stderr_fd);
let container_builder =
container_builder
.with_root_path(&state_dir_clone)
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to set root path: {e}"),
})?;
// Configure as tenant (joins existing namespaces)
let tenant_builder = container_builder
.as_tenant()
.with_container_args(cmd_clone)
.with_detach(false); // Wait for completion
// Execute and wait
let pid = tenant_builder
.build()
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to exec in container: {e}"),
})?;
// Return raw pid as i32 to avoid nix version conflicts
// (libcontainer uses nix 0.29, we use nix 0.31)
Ok::<i32, AgentError>(pid.as_raw())
})
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
// Wait for process to complete and get exit status
let exit_code = tokio::task::spawn_blocking(move || {
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::Pid;
// Convert raw pid back to our nix version's Pid type
let pid = Pid::from_raw(exec_pid);
match waitpid(pid, None) {
Ok(WaitStatus::Exited(_, code)) => code,
Ok(WaitStatus::Signaled(_, signal, _)) => 128 + signal as i32,
Ok(_) | Err(_) => -1,
}
})
.await
.unwrap_or(-1);
// Read output
let stdout_content = fs::read_to_string(&stdout_path).await.unwrap_or_default();
let stderr_content = fs::read_to_string(&stderr_path).await.unwrap_or_default();
// Clean up exec directory
let _ = fs::remove_dir_all(&exec_dir).await;
Ok((exit_code, stdout_content, stderr_content))
}
/// Start an interactive exec session against a libcontainer-managed
/// container.
///
/// Allocates a master/slave pseudo-terminal pair via `nix::pty::openpty`,
/// duplicates the slave fd three times so libcontainer can take ownership
/// of it for the tenant process's stdin/stdout/stderr (each setter on
/// `ContainerBuilder` consumes an `OwnedFd`), and returns the master end
/// wrapped in a `tokio::io::unix::AsyncFd`-backed duplex stream.
///
/// The returned [`ExecHandle::resize`] channel drives a small task that
/// services `(rows, cols)` updates by issuing `ioctl(TIOCSWINSZ)` on the
/// master fd. The [`ExecHandle::exit`] future calls `waitpid` on the
/// tenant's pid in `spawn_blocking` and resolves with the exit code (or
/// `128 + signal` for signalled deaths).
///
/// Both `tty=true` and `tty=false` allocate a PTY: when `tty=false` the
/// caller still gets a single duplex byte stream (stdout and stderr are
/// merged through the slave terminal, matching how a tenant without a
/// requested TTY behaves when handed a terminal anyway).
#[allow(unsafe_code)]
#[instrument(
skip(self, opts),
fields(
otel.name = "container.exec_pty",
container.id = %self.container_id_str(id),
command = ?opts.command,
tty = opts.tty,
)
)]
async fn exec_pty(&self, id: &ContainerId, opts: ExecOptions) -> Result<ExecHandle> {
let container_id = self.container_id_str(id);
if opts.command.is_empty() {
return Err(AgentError::InvalidSpec(
"exec_pty command cannot be empty".to_string(),
));
}
// Allocate the PTY pair on a blocking thread — `openpty` is a syscall
// wrapper but we keep it off the async path for symmetry with the
// libcontainer call below.
let openpty_result = tokio::task::spawn_blocking(|| nix::pty::openpty(None, None))
.await
.map_err(|e| AgentError::Internal(format!("openpty join error: {e}")))?
.map_err(|e| AgentError::Internal(format!("openpty failed: {e}")))?;
let master_fd = openpty_result.master;
let slave_fd = openpty_result.slave;
// Duplicate the slave three times so libcontainer can take ownership
// of one fd per stdio. Closing the original slave on the host side
// happens automatically when `slave_fd` drops at the end of this
// scope; the duplicates travel into the tenant.
let stdin_fd = nix::unistd::dup(&slave_fd)
.map_err(|e| AgentError::Internal(format!("dup slave for stdin failed: {e}")))?;
let stdout_fd = nix::unistd::dup(&slave_fd)
.map_err(|e| AgentError::Internal(format!("dup slave for stdout failed: {e}")))?;
let stderr_fd = nix::unistd::dup(&slave_fd)
.map_err(|e| AgentError::Internal(format!("dup slave for stderr failed: {e}")))?;
drop(slave_fd);
// Mark the master end non-blocking so `AsyncFd` can drive it.
nix::fcntl::fcntl(
&master_fd,
nix::fcntl::FcntlArg::F_SETFL(nix::fcntl::OFlag::O_NONBLOCK),
)
.map_err(|e| AgentError::Internal(format!("F_SETFL O_NONBLOCK on pty master: {e}")))?;
let cmd_clone = opts.command.clone();
let container_id_clone = container_id.clone();
let state_dir_clone = self.config.state_dir.clone();
// Spawn the tenant on the libcontainer thread pool. Each stdio fd is
// moved into the closure and consumed by the builder.
let exec_pid = tokio::task::spawn_blocking(move || {
let container_builder =
ContainerBuilder::new(container_id_clone.clone(), SyscallType::Linux)
.with_stdin(stdin_fd)
.with_stdout(stdout_fd)
.with_stderr(stderr_fd);
let container_builder =
container_builder
.with_root_path(&state_dir_clone)
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to set root path: {e}"),
})?;
let tenant_builder = container_builder
.as_tenant()
.with_container_args(cmd_clone)
.with_detach(true);
let pid = tenant_builder
.build()
.map_err(|e| AgentError::CreateFailed {
id: container_id_clone.clone(),
reason: format!("failed to exec_pty in container: {e}"),
})?;
Ok::<i32, AgentError>(pid.as_raw())
})
.await
.map_err(|e| AgentError::CreateFailed {
id: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
// Resize task: pump (rows, cols) into TIOCSWINSZ on the master.
let (resize_tx, mut resize_rx) = tokio::sync::mpsc::channel::<(u16, u16)>(8);
let master_raw = master_fd.as_raw_fd();
tokio::spawn(async move {
while let Some((rows, cols)) = resize_rx.recv().await {
let ws = nix::pty::Winsize {
ws_row: rows,
ws_col: cols,
ws_xpixel: 0,
ws_ypixel: 0,
};
// SAFETY: `master_raw` remains a valid fd as long as the
// duplex stream is alive (it owns the master `OwnedFd`).
// `ws` is a stack-allocated `winsize` matching the layout
// the kernel expects. The ioctl reads from the pointer; it
// does not retain it past the call.
let rc = unsafe { libc::ioctl(master_raw, libc::TIOCSWINSZ, &ws) };
if rc != 0 {
let err = std::io::Error::last_os_error();
tracing::warn!(?err, "TIOCSWINSZ failed on pty master");
}
}
});
// Build the exit future: waitpid on a blocking thread.
let exit_fut: ExecExitFuture = Box::pin(async move {
let exit_code = tokio::task::spawn_blocking(move || {
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::Pid;
let pid = Pid::from_raw(exec_pid);
match waitpid(pid, None) {
Ok(WaitStatus::Exited(_, code)) => code,
Ok(WaitStatus::Signaled(_, signal, _)) => 128 + signal as i32,
Ok(_) | Err(_) => -1,
}
})
.await
.map_err(|e| AgentError::Internal(format!("waitpid join error: {e}")))?;
Ok(exit_code)
});
// Wrap the master end in an AsyncFd-backed duplex stream.
let stream: ExecPtyStream = Box::new(PtyDuplex::new(master_fd)?);
Ok(ExecHandle {
stream,
resize: resize_tx,
exit: exit_fut,
})
}
/// Get container resource statistics from cgroups
///
/// Reads CPU and memory statistics from the cgroups v2 filesystem.
/// Supports both systemd and cgroupfs cgroup drivers.
async fn get_container_stats(&self, id: &ContainerId) -> Result<ContainerStats> {
let container_id = self.container_id_str(id);
// Determine cgroup path based on cgroup driver
let cgroup_path = if self.config.use_systemd {
// systemd cgroup driver: /sys/fs/cgroup/system.slice/zlayer-{id}.scope
PathBuf::from(format!(
"/sys/fs/cgroup/system.slice/zlayer-{container_id}.scope"
))
} else {
// cgroupfs driver: /sys/fs/cgroup/zlayer/{id}
PathBuf::from(format!("/sys/fs/cgroup/zlayer/{container_id}"))
};
tracing::debug!(
container = %container_id,
cgroup_path = %cgroup_path.display(),
"reading container stats from cgroups"
);
cgroups_stats::read_container_stats(&cgroup_path)
.await
.map_err(|e| {
AgentError::Internal(format!(
"failed to read cgroup stats for container {container_id}: {e}"
))
})
}
/// Wait for a container to exit and return its exit code
///
/// This polls the container state until it reaches an exited state.
/// For libcontainer, we don't have a direct "wait" API, so we poll.
async fn wait_container(&self, id: &ContainerId) -> Result<i32> {
let container_id = self.container_id_str(id);
let poll_interval = Duration::from_millis(100);
let max_wait = Duration::from_secs(3600); // 1 hour max
let start = std::time::Instant::now();
tracing::debug!(
container = %container_id,
"waiting for container to exit"
);
loop {
if start.elapsed() > max_wait {
return Err(AgentError::Timeout { timeout: max_wait });
}
match self.container_state(id).await {
Ok(ContainerState::Exited { code }) => {
tracing::debug!(
container = %container_id,
exit_code = code,
"container exited"
);
return Ok(code);
}
Ok(ContainerState::Failed { reason }) => {
tracing::warn!(
container = %container_id,
reason = %reason,
"container failed"
);
return Err(AgentError::Internal(format!("container failed: {reason}")));
}
Ok(_) => {
// Still running, wait and poll again
tokio::time::sleep(poll_interval).await;
}
Err(AgentError::NotFound { .. }) => {
// Container may have been removed - treat as exited with code 0
tracing::debug!(
container = %container_id,
"container not found, treating as exited"
);
return Ok(0);
}
Err(e) => {
return Err(e);
}
}
}
}
/// Get container logs (stdout/stderr combined)
///
/// Reads from the container's log files and returns as a vector of lines.
async fn get_logs(&self, id: &ContainerId) -> Result<Vec<LogEntry>> {
let container_id = self.container_id_str(id);
// Get log paths
let (stdout_path, stderr_path) = {
let containers = self.containers.read().await;
match containers.get(&container_id) {
Some(info) => (info.stdout_path.clone(), info.stderr_path.clone()),
None => self.log_paths(id),
}
};
let now = chrono::Utc::now();
let source = LogSource::Container(id.to_string());
let mut entries = Vec::new();
// Read stdout
if stdout_path.exists() {
if let Ok(content) = fs::read_to_string(&stdout_path).await {
for line in content.lines() {
entries.push(LogEntry {
timestamp: now,
stream: LogStream::Stdout,
message: line.to_string(),
source: source.clone(),
service: None,
deployment: None,
});
}
}
}
// Read stderr
if stderr_path.exists() {
if let Ok(content) = fs::read_to_string(&stderr_path).await {
for line in content.lines() {
entries.push(LogEntry {
timestamp: now,
stream: LogStream::Stderr,
message: line.to_string(),
source: source.clone(),
service: None,
deployment: None,
});
}
}
}
// Sort by timestamp
entries.sort_by_key(|e| e.timestamp);
Ok(entries)
}
/// Get the PID of a container's main process
///
/// Returns:
/// - `Ok(Some(pid))` for running containers
/// - `Ok(None)` if the container exists but has no PID (not running or stopped)
/// - `Err` if the container doesn't exist or there's an error loading it
#[instrument(
skip(self),
fields(
otel.name = "container.get_pid",
container.id = %self.container_id_str(id),
)
)]
async fn get_container_pid(&self, id: &ContainerId) -> Result<Option<u32>> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
// Check if container root exists
if !container_root.exists() {
return Err(AgentError::NotFound {
container: container_id.clone(),
reason: "container state directory not found".to_string(),
});
}
// Load container and get PID
let container_id_clone = container_id.clone();
let pid = tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
// Refresh status to get current state
let _ = container.refresh_status();
// Get PID - returns None if container is not running
#[allow(clippy::cast_sign_loss)]
let pid = container.pid().map(|p| p.as_raw() as u32);
Ok::<Option<u32>, AgentError>(pid)
})
.await
.map_err(|e| AgentError::NotFound {
container: container_id.clone(),
reason: format!("task join error: {e}"),
})??;
tracing::debug!(
container = %container_id,
pid = ?pid,
"retrieved container PID"
);
Ok(pid)
}
async fn get_container_ip(&self, _id: &ContainerId) -> Result<Option<std::net::IpAddr>> {
// Youki containers use OCI network namespaces — IP assignment comes
// from the overlay manager, not the runtime itself.
Ok(None)
}
/// Sync all named volumes to S3 before container removal.
///
/// When the `s3` feature is enabled and a `LayerSyncManager` has been
/// configured on the storage manager, this iterates all non-anonymous
/// volumes and pushes any changes to S3. Errors are logged but do not
/// prevent container removal.
#[allow(unused_variables)]
async fn sync_container_volumes(&self, id: &ContainerId) -> Result<()> {
#[cfg(feature = "s3")]
{
let storage_manager = self.storage_manager.read().await;
if storage_manager.layer_sync().is_some() {
let container_id = self.container_id_str(id);
tracing::info!(
container = %container_id,
"syncing volumes to S3 before container removal"
);
match storage_manager.sync_all_volumes().await {
Ok(synced) => {
if synced > 0 {
tracing::info!(
container = %container_id,
synced_count = synced,
"volume sync complete"
);
}
}
Err(e) => {
tracing::warn!(
container = %container_id,
error = %e,
"volume sync failed, data may not be persisted"
);
}
}
}
}
Ok(())
}
async fn list_images(&self) -> Result<Vec<ImageInfo>> {
let keys = self
.blob_cache
.keys_with_prefix("manifest:")
.await
.map_err(|e| {
AgentError::Internal(format!("failed to list manifest cache keys: {e}"))
})?;
let mut images = Vec::with_capacity(keys.len());
for key in keys {
// Strip the "manifest:" prefix
let reference = match key.strip_prefix("manifest:") {
Some(r) => r.to_string(),
None => continue,
};
// Load manifest body to compute size and extract digest
let Ok(Some(manifest_bytes)) = self.blob_cache.get(&key).await else {
continue; // cache entry disappeared — skip
};
let Ok(manifest) = serde_json::from_slice::<OciImageManifest>(&manifest_bytes) else {
continue; // corrupt entry — skip
};
// Sum layer sizes + config size
let layers_size: i64 = manifest.layers.iter().map(|l| l.size).sum();
let config_size = manifest.config.size;
let total = layers_size.saturating_add(config_size);
let size_bytes = if total > 0 {
u64::try_from(total).ok()
} else {
None
};
// Look up the stored registry digest (Wave 2 convention)
let digest_key = format!("manifest-digest:{reference}");
let digest = self
.blob_cache
.get(&digest_key)
.await
.ok()
.flatten()
.and_then(|bytes| String::from_utf8(bytes).ok());
images.push(ImageInfo {
reference,
digest,
size_bytes,
});
}
Ok(images)
}
async fn remove_image(&self, image: &str, _force: bool) -> Result<()> {
let manifest_key = format!("manifest:{image}");
let digest_key = format!("manifest-digest:{image}");
// Load manifest to learn the blob digests it references
let manifest_bytes = self
.blob_cache
.get(&manifest_key)
.await
.map_err(|e| AgentError::Internal(format!("failed to read manifest cache: {e}")))?;
let Some(manifest_bytes) = manifest_bytes else {
return Err(AgentError::NotFound {
container: image.to_string(),
reason: format!("image '{image}' not found in cache"),
});
};
let manifest: OciImageManifest = serde_json::from_slice(&manifest_bytes).map_err(|e| {
AgentError::Internal(format!(
"failed to parse cached manifest for '{image}': {e}"
))
})?;
// Delete each referenced blob. Don't bail on individual failures — log and continue.
for layer in &manifest.layers {
if let Err(e) = self.blob_cache.delete(&layer.digest).await {
tracing::warn!(
image = %image,
digest = %layer.digest,
error = %e,
"failed to delete layer blob"
);
}
}
if let Err(e) = self.blob_cache.delete(&manifest.config.digest).await {
tracing::warn!(
image = %image,
digest = %manifest.config.digest,
error = %e,
"failed to delete config blob"
);
}
// Delete the manifest body and its digest sidecar
self.blob_cache
.delete(&manifest_key)
.await
.map_err(|e| AgentError::Internal(format!("failed to delete manifest: {e}")))?;
let _ = self.blob_cache.delete(&digest_key).await;
Ok(())
}
async fn prune_images(&self) -> Result<PruneResult> {
// 1. Collect all digests referenced by remaining manifests.
let manifest_keys = self
.blob_cache
.keys_with_prefix("manifest:")
.await
.map_err(|e| AgentError::Internal(format!("failed to list manifests: {e}")))?;
let mut referenced: std::collections::HashSet<String> = std::collections::HashSet::new();
for key in &manifest_keys {
let Ok(Some(bytes)) = self.blob_cache.get(key).await else {
continue;
};
let Ok(manifest) = serde_json::from_slice::<OciImageManifest>(&bytes) else {
continue;
};
referenced.insert(manifest.config.digest.clone());
for layer in &manifest.layers {
referenced.insert(layer.digest.clone());
}
}
// 2. Walk all sha256:* blob keys and delete those not referenced.
let all_blob_keys = self
.blob_cache
.keys_with_prefix("sha256:")
.await
.map_err(|e| AgentError::Internal(format!("failed to list blobs: {e}")))?;
let mut deleted = Vec::new();
let mut space_reclaimed: u64 = 0;
for key in all_blob_keys {
if referenced.contains(&key) {
continue;
}
// Grab the blob size before deleting (best-effort).
if let Ok(Some(bytes)) = self.blob_cache.get(&key).await {
space_reclaimed = space_reclaimed.saturating_add(bytes.len() as u64);
}
if let Err(e) = self.blob_cache.delete(&key).await {
tracing::warn!(
digest = %key,
error = %e,
"failed to delete orphaned blob during prune"
);
continue;
}
deleted.push(key);
}
Ok(PruneResult {
deleted,
space_reclaimed,
})
}
#[instrument(
skip(self),
fields(
otel.name = "container.kill",
container.id = %self.container_id_str(id),
service.name = %id.service,
signal = ?signal,
)
)]
async fn kill_container(&self, id: &ContainerId, signal: Option<&str>) -> Result<()> {
use std::convert::TryFrom;
let canonical = crate::runtime::validate_signal(signal.unwrap_or("SIGKILL"))?;
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
tracing::info!(
container = %container_id,
signal = %canonical,
"sending signal to container"
);
let container_id_clone = container_id.clone();
let canonical_clone = canonical.clone();
tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
if !container.status().can_kill() {
return Err(AgentError::InvalidSpec(format!(
"container '{container_id_clone}' is not in a killable state ({:?})",
container.status()
)));
}
let sig = Signal::try_from(canonical_clone.as_str()).map_err(|e| {
AgentError::InvalidSpec(format!("invalid signal '{canonical_clone}': {e:?}"))
})?;
container.kill(sig, true).map_err(|e| {
AgentError::Internal(format!(
"failed to deliver signal {canonical_clone} to '{container_id_clone}': {e}"
))
})?;
Ok::<(), AgentError>(())
})
.await
.map_err(|e| AgentError::Internal(format!("task join error during kill: {e}")))??;
Ok(())
}
/// Pause a container by freezing its cgroup via `Container::pause`.
///
/// Loaded from the on-disk libcontainer state; the call itself is
/// blocking so we hop to `spawn_blocking`. Errors map to `NotFound` when
/// the container state directory doesn't exist, `InvalidSpec` when the
/// container is in a non-pausable state (already paused, never started),
/// and `Internal` for cgroup write failures.
#[instrument(
skip(self),
fields(
otel.name = "container.pause",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn pause_container(&self, id: &ContainerId) -> Result<()> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
let container_id_clone = container_id.clone();
tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
if !container.can_pause() {
return Err(AgentError::InvalidSpec(format!(
"container '{container_id_clone}' is not in a pausable state ({:?})",
container.status()
)));
}
container.pause().map_err(|e| {
AgentError::Internal(format!(
"failed to pause container '{container_id_clone}': {e}"
))
})?;
Ok::<(), AgentError>(())
})
.await
.map_err(|e| AgentError::Internal(format!("task join error during pause: {e}")))??;
Ok(())
}
/// Resume a previously-paused container via `Container::resume`.
///
/// Symmetric inverse of `pause_container`: thaws the freezer cgroup. Same
/// error mapping conventions.
#[instrument(
skip(self),
fields(
otel.name = "container.unpause",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn unpause_container(&self, id: &ContainerId) -> Result<()> {
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
let container_id_clone = container_id.clone();
tokio::task::spawn_blocking(move || {
let mut container =
Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
if !container.can_resume() {
return Err(AgentError::InvalidSpec(format!(
"container '{container_id_clone}' is not in a resumable state ({:?})",
container.status()
)));
}
container.resume().map_err(|e| {
AgentError::Internal(format!(
"failed to resume container '{container_id_clone}': {e}"
))
})?;
Ok::<(), AgentError>(())
})
.await
.map_err(|e| AgentError::Internal(format!("task join error during unpause: {e}")))??;
Ok(())
}
/// Update a running container's cgroup v2 resource limits and persist
/// the new restart policy in the supervisor's in-memory state.
///
/// This implementation writes directly to the container's cgroup v2
/// hierarchy under `/sys/fs/cgroup/zlayer/<id>` (or
/// `/sys/fs/cgroup/system.slice/zlayer-<id>.scope` when systemd is the
/// driver). The fields it can apply natively on cgroup v2 are:
///
/// * `cpu_shares` → `cpu.weight` (mapped from the `2..262144` shares
/// range to v2's `1..10000` weight range)
/// * `memory` → `memory.max` (`0` clears the limit)
/// * `memory_reservation` → `memory.low`
/// * `memory_swap` → `memory.swap.max` (`-1` clears the limit)
/// * `pids_limit` → `pids.max` (`-1` or `0` clears)
/// * `cpuset_cpus` → `cpuset.cpus`
/// * `cpuset_mems` → `cpuset.mems`
/// * `cpu_period` + `cpu_quota` → `cpu.max` ("`<quota> <period>`")
/// * `blkio_weight` → `io.bfq.weight` (best-effort; emits a warning
/// when the BFQ controller isn't enabled)
///
/// `cpu_realtime_period` / `cpu_realtime_runtime` and `kernel_memory`
/// have no cgroup v2 equivalent and are surfaced as warnings rather
/// than errors.
///
/// `restart_policy` is captured into the in-memory `ContainerInfo`
/// entry so the supervisor sees the new policy when the container
/// next exits.
#[instrument(
skip(self, update),
fields(
otel.name = "container.update",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn update_container_resources(
&self,
id: &ContainerId,
update: &crate::runtime::ContainerResourceUpdate,
) -> Result<crate::runtime::ContainerUpdateOutcome> {
let container_id = self.container_id_str(id);
if update.is_empty() {
return Ok(crate::runtime::ContainerUpdateOutcome::default());
}
// Persist the new restart policy in our in-memory tracking. We
// do this even when the container's cgroup directory is gone
// (e.g. the container has already exited): the supervisor reads
// the policy on the *next* exit, so updating it for a stopped
// container is still meaningful.
if let Some(rp) = update.restart_policy.clone() {
let mut containers = self.containers.write().await;
if let Some(info) = containers.get_mut(&container_id) {
info.restart_policy = Some(rp);
}
}
let cgroup_path = if self.config.use_systemd {
PathBuf::from(format!(
"/sys/fs/cgroup/system.slice/zlayer-{container_id}.scope"
))
} else {
PathBuf::from(format!("/sys/fs/cgroup/zlayer/{container_id}"))
};
let mut warnings: Vec<String> = Vec::new();
// If there's nothing to write to cgroup files (only restart
// policy was set), bail out before touching the filesystem.
let needs_cgroup_write = update.cpu_shares.is_some()
|| update.memory.is_some()
|| update.memory_reservation.is_some()
|| update.memory_swap.is_some()
|| update.pids_limit.is_some()
|| update.cpuset_cpus.is_some()
|| update.cpuset_mems.is_some()
|| update.cpu_period.is_some()
|| update.cpu_quota.is_some()
|| update.blkio_weight.is_some();
if needs_cgroup_write && !cgroup_path.exists() {
return Err(AgentError::NotFound {
container: container_id.clone(),
reason: format!(
"cgroup directory '{}' not found — is the container running?",
cgroup_path.display()
),
});
}
if update.kernel_memory.is_some() {
warnings
.push("KernelMemory has no cgroup v2 equivalent and was not applied".to_string());
}
if update.cpu_realtime_period.is_some() || update.cpu_realtime_runtime.is_some() {
warnings.push(
"CpuRealtimePeriod/CpuRealtimeRuntime are not supported on cgroup v2; ignored"
.to_string(),
);
}
// cpu_shares -> cpu.weight (cgroup v2 mapping). v1 shares are
// 2..262144 with default 1024; v2 weight is 1..10000 with
// default 100. Use Docker's documented mapping:
// weight = 1 + ((shares - 2) * 9999 / 262142)
if let Some(shares) = update.cpu_shares {
let shares = shares.max(2);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let weight = 1_i64 + (shares - 2) * 9999 / 262_142;
let weight = weight.clamp(1, 10_000);
write_cgroup_file(
&cgroup_path.join("cpu.weight"),
&weight.to_string(),
&mut warnings,
)
.await?;
}
// cpu.max takes "<quota> <period>" or "max <period>".
if update.cpu_period.is_some() || update.cpu_quota.is_some() {
let period = update.cpu_period.unwrap_or(100_000);
let quota_str = match update.cpu_quota {
Some(q) if q > 0 => q.to_string(),
_ => "max".to_string(),
};
let value = format!("{quota_str} {period}");
write_cgroup_file(&cgroup_path.join("cpu.max"), &value, &mut warnings).await?;
}
if let Some(memory) = update.memory {
let value = if memory <= 0 {
"max".to_string()
} else {
memory.to_string()
};
write_cgroup_file(&cgroup_path.join("memory.max"), &value, &mut warnings).await?;
}
if let Some(reservation) = update.memory_reservation {
let value = if reservation <= 0 {
"0".to_string()
} else {
reservation.to_string()
};
write_cgroup_file(&cgroup_path.join("memory.low"), &value, &mut warnings).await?;
}
if let Some(swap) = update.memory_swap {
// Docker semantics: -1 means unlimited. Memory swap on v2
// is the *swap-only* limit, while Docker's `MemorySwap`
// historically meant memory+swap. Pass through the absolute
// value with a warning so the operator knows the v2
// semantic is different.
warnings.push(
"MemorySwap is interpreted as cgroup v2 memory.swap.max (swap-only); \
Docker's v1 semantics differ"
.to_string(),
);
let value = if swap < 0 {
"max".to_string()
} else {
swap.to_string()
};
write_cgroup_file(&cgroup_path.join("memory.swap.max"), &value, &mut warnings).await?;
}
if let Some(pids) = update.pids_limit {
let value = if pids <= 0 {
"max".to_string()
} else {
pids.to_string()
};
write_cgroup_file(&cgroup_path.join("pids.max"), &value, &mut warnings).await?;
}
if let Some(cpus) = update.cpuset_cpus.as_ref() {
write_cgroup_file(&cgroup_path.join("cpuset.cpus"), cpus, &mut warnings).await?;
}
if let Some(mems) = update.cpuset_mems.as_ref() {
write_cgroup_file(&cgroup_path.join("cpuset.mems"), mems, &mut warnings).await?;
}
if let Some(weight) = update.blkio_weight {
// io.bfq.weight expects 1..1000; Docker's BlkioWeight is
// 10..1000 with default 500. Pass through verbatim and let
// the kernel reject out-of-range values.
write_cgroup_file(
&cgroup_path.join("io.bfq.weight"),
&weight.to_string(),
&mut warnings,
)
.await?;
}
Ok(crate::runtime::ContainerUpdateOutcome { warnings })
}
/// List the processes running inside a container.
///
/// Reads the container's main PID from libcontainer's loaded state, walks
/// `/proc/{pid}/task/*` to enumerate tids inside the container's pid
/// namespace, then synthesises a Docker-style top response. The columns
/// returned are a fixed minimal subset (`UID`, `PID`, `PPID`, `STIME`,
/// `CMD`) — `ps_args` is accepted for trait conformance but ignored,
/// because youki has no privileged `ps`-runner per container.
#[instrument(
skip(self, _ps_args),
fields(
otel.name = "container.top",
container.id = %self.container_id_str(id),
service.name = %id.service,
)
)]
async fn top_container(
&self,
id: &ContainerId,
_ps_args: &[String],
) -> Result<crate::runtime::ContainerTopOutput> {
use crate::runtime::ContainerTopOutput;
let container_id = self.container_id_str(id);
let container_root = self.container_root(id);
let container_id_clone = container_id.clone();
// Snapshot the main process PID under spawn_blocking — Container::load
// walks the on-disk state file synchronously.
let pid = tokio::task::spawn_blocking(move || -> Result<i32> {
let container = Container::load(container_root).map_err(|e| AgentError::NotFound {
container: container_id_clone.clone(),
reason: format!("failed to load container: {e}"),
})?;
let pid = container.pid().ok_or_else(|| {
AgentError::InvalidSpec(format!(
"container '{container_id_clone}' has no running process"
))
})?;
Ok(pid.as_raw())
})
.await
.map_err(|e| AgentError::Internal(format!("task join error during top: {e}")))??;
// Walk /proc/{pid}/task/* to enumerate threads (which double as
// process IDs from the host's perspective). Containers running a
// single multi-threaded process expose all its tids here.
let task_dir = format!("/proc/{pid}/task");
let mut entries = match tokio::fs::read_dir(&task_dir).await {
Ok(it) => it,
Err(e) => {
return Err(AgentError::NotFound {
container: container_id.clone(),
reason: format!("failed to read /proc/{pid}/task: {e}"),
});
}
};
let mut processes: Vec<Vec<String>> = Vec::new();
while let Some(entry) = entries
.next_entry()
.await
.map_err(|e| AgentError::Internal(format!("failed to walk /proc tree: {e}")))?
{
let Ok(name) = entry.file_name().into_string() else {
continue;
};
// Skip entries that aren't PIDs (defensive — /proc/.../task only
// contains numeric directories in practice).
if !name.chars().all(|c| c.is_ascii_digit()) {
continue;
}
let row = read_proc_row(&name).await;
processes.push(row);
}
Ok(ContainerTopOutput {
titles: vec![
"UID".to_string(),
"PID".to_string(),
"PPID".to_string(),
"STIME".to_string(),
"CMD".to_string(),
],
processes,
})
}
/// `changes_container` is unsupported on Youki: the runtime stores
/// containers as a single mutable rootfs (extracted from the cached
/// layers in `bundle_path`), with no overlayfs upper/lower split to
/// diff against. Implementing this would require either re-extracting
/// the original image layers and walking both trees, or cooperating
/// with the storage driver — both out of scope for this trait method.
/// The REST layer translates `Unsupported` into a 501 response.
async fn changes_container(
&self,
_id: &ContainerId,
) -> Result<Vec<crate::runtime::FilesystemChangeEntry>> {
Err(AgentError::Unsupported(
"changes_container is not supported by the youki runtime: \
no layered filesystem to diff against"
.into(),
))
}
/// `port_mappings_container` is unsupported on Youki: the runtime relies
/// on the host's network namespace for port forwarding (proxy / overlay
/// network), not on a per-container `HostConfig.PortBindings` table. The
/// 501 from the REST layer signals to clients that they should consult
/// the daemon's deployment / endpoint metadata rather than a runtime
/// inspect call.
async fn port_mappings_container(
&self,
_id: &ContainerId,
) -> Result<Vec<crate::runtime::PortMappingEntry>> {
Err(AgentError::Unsupported(
"port_mappings_container is not supported by the youki runtime: \
port publishing is managed at the proxy / overlay layer"
.into(),
))
}
/// `prune_containers` is unsupported on Youki: cleanup of stopped
/// container state directories is driven by the container supervisor /
/// reaper, not by a daemon-wide prune sweep. Forcing a sweep here would
/// race the supervisor's own bookkeeping. Surfaces as 501 from the REST
/// layer.
async fn prune_containers(&self) -> Result<crate::runtime::ContainerPruneResult> {
Err(AgentError::Unsupported(
"prune_containers is not supported by the youki runtime: \
stopped containers are reaped by the supervisor"
.into(),
))
}
#[instrument(
skip(self),
fields(
otel.name = "image.tag",
source = %source,
target = %target,
)
)]
async fn tag_image(&self, source: &str, target: &str) -> Result<()> {
if source.trim().is_empty() || target.trim().is_empty() {
return Err(AgentError::InvalidSpec(
"source and target must be non-empty image references".to_string(),
));
}
if source == target {
// Nothing to do; idempotent.
return Ok(());
}
// Copy the source manifest and its digest sidecar under the target
// reference. All blobs remain shared content-addressed in the cache.
let src_manifest_key = format!("manifest:{source}");
let manifest_bytes = self
.blob_cache
.get(&src_manifest_key)
.await
.map_err(|e| AgentError::Internal(format!("failed to read manifest cache: {e}")))?
.ok_or_else(|| AgentError::NotFound {
container: source.to_string(),
reason: format!("source image '{source}' not found in cache"),
})?;
let dst_manifest_key = format!("manifest:{target}");
self.blob_cache
.put(&dst_manifest_key, &manifest_bytes)
.await
.map_err(|e| AgentError::Internal(format!("failed to write manifest for tag: {e}")))?;
// Best-effort: carry over the registry digest sidecar if present.
let src_digest_key = format!("manifest-digest:{source}");
if let Ok(Some(digest_bytes)) = self.blob_cache.get(&src_digest_key).await {
let dst_digest_key = format!("manifest-digest:{target}");
if let Err(e) = self.blob_cache.put(&dst_digest_key, &digest_bytes).await {
tracing::warn!(
source = %source,
target = %target,
error = %e,
"failed to copy manifest-digest sidecar for tag (non-fatal)"
);
}
}
tracing::info!(source = %source, target = %target, "tagged image");
Ok(())
}
/// Stream container logs by tailing the on-disk stdout/stderr files
/// produced by [`Self::create_log_files`].
///
/// Implementation notes:
/// * Each line of the file produces one [`LogChunk`]. Youki's runtime
/// does not write timestamps into the log files, so chunks carry the
/// wall-clock time the line was read when `opts.timestamps` is set
/// and `None` otherwise.
/// * `opts.tail` is honoured by counting `\n` bytes from the end of
/// each file before streaming begins.
/// * `opts.follow` keeps the stream alive after EOF, polling every
/// 200ms for new content. When `follow=false` the stream completes
/// after the buffered lines drain.
/// * `opts.since`/`opts.until` filter chunks by the read-time
/// wallclock (see above — file format has no per-line timestamps,
/// so this is the best the youki backend can do).
/// * `opts.stdout`/`opts.stderr` toggle each channel; if neither is
/// true (Docker's default-on shorthand), both are streamed.
#[instrument(
skip(self),
fields(
otel.name = "container.logs.stream",
container.id = %self.container_id_str(id),
)
)]
async fn logs_stream(&self, id: &ContainerId, opts: LogsStreamOptions) -> Result<LogsStream> {
// Resolve log paths from local state, falling back to the default
// bundle/structured paths just like `container_logs` / `get_logs`.
let (stdout_path, stderr_path) = {
let containers = self.containers.read().await;
match containers.get(&self.container_id_str(id)) {
Some(info) => (info.stdout_path.clone(), info.stderr_path.clone()),
None => self.log_paths(id),
}
};
// If neither channel was requested, default to streaming both
// (matches Docker's behaviour when `stdout=false&stderr=false` is
// sent — Docker treats it as "both", since requesting nothing is
// never useful).
let none_specified = !opts.stdout && !opts.stderr;
let want_stdout = opts.stdout || none_specified;
let want_stderr = opts.stderr || none_specified;
// Use a bounded channel so a slow consumer applies natural
// back-pressure on the file readers.
let (tx, rx) = mpsc::channel::<Result<LogChunk>>(64);
if want_stdout && stdout_path.exists() {
let tx = tx.clone();
let path = stdout_path.clone();
let opts_cloned = opts.clone();
tokio::spawn(async move {
let _ = stream_log_file(path, LogChannel::Stdout, opts_cloned, tx).await;
});
}
if want_stderr && stderr_path.exists() {
let tx_err = tx.clone();
let path = stderr_path.clone();
let opts_cloned = opts.clone();
tokio::spawn(async move {
let _ = stream_log_file(path, LogChannel::Stderr, opts_cloned, tx_err).await;
});
}
// Drop the original sender so the stream terminates once both
// (or all available) tailers exit.
drop(tx);
Ok(Box::pin(ReceiverStream::new(rx)))
}
/// Stream periodic resource-usage samples for a container by polling
/// its cgroup v2 directory once per second.
///
/// Reuses [`cgroups_stats::read_container_stats`] for `cpu.stat`,
/// `memory.current`, and `memory.max`, and additionally reads
/// `pids.current` / `pids.max` directly so the [`StatsSample`] reflects
/// pids counters that the existing internal [`ContainerStats`] type
/// does not carry.
///
/// Network and block-IO counters are not surfaced by youki's cgroup
/// directory at the same path (network stats live in the container's
/// netns, blkio stats require the legacy v1 hierarchy) so they are
/// reported as `0`. Consumers that need those numbers should use the
/// Docker runtime, which does have them.
#[instrument(
skip(self),
fields(
otel.name = "container.stats.stream",
container.id = %self.container_id_str(id),
)
)]
async fn stats_stream(&self, id: &ContainerId) -> Result<StatsStream> {
let container_id = self.container_id_str(id);
let cgroup_path = if self.config.use_systemd {
PathBuf::from(format!(
"/sys/fs/cgroup/system.slice/zlayer-{container_id}.scope"
))
} else {
PathBuf::from(format!("/sys/fs/cgroup/zlayer/{container_id}"))
};
let (tx, rx) = mpsc::channel::<Result<StatsSample>>(8);
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(1));
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
#[allow(clippy::cast_possible_truncation)]
let online_cpus = num_cpus::get() as u32;
loop {
interval.tick().await;
let sample_result = read_stats_sample(&cgroup_path, online_cpus).await;
let send_result = match sample_result {
Ok(sample) => tx.send(Ok(sample)).await,
Err(err) => {
// Surface the error and terminate the stream — a
// missing cgroup directory means the container is
// gone, retrying on every tick would just spam.
let _ = tx.send(Err(err)).await;
break;
}
};
if send_result.is_err() {
// Receiver dropped — stop sampling.
break;
}
}
});
Ok(Box::pin(ReceiverStream::new(rx)))
}
/// Stream image pull progress by wrapping the synchronous
/// [`zlayer_registry::ImagePuller::pull_image_with_policy`] code path.
///
/// The puller does not expose a per-layer progress callback today, so
/// this implementation synthesises a coarse progression:
/// 1. `Status { status: "Pulling manifest" }` before manifest fetch.
/// 2. One `Status { status: "Pulling layer", id: <digest> }` per
/// layer in the manifest, emitted before each layer is fetched.
/// 3. A final `Done { reference, digest }` event when the pull
/// succeeds (or an `Err` item if it fails).
///
/// Each layer event carries the layer's `total` size from the manifest
/// so consumers can render proportional progress bars even though
/// `current` cannot be reported until the puller gains a streaming
/// callback.
///
/// `auth` is currently ignored on this backend — youki resolves
/// credentials through the persistent secret store via
/// [`zlayer_core::AuthResolver`], matching the semantics of
/// [`Self::pull_image_with_policy`].
#[instrument(
skip(self, _auth),
fields(
otel.name = "image.pull.stream",
container.image.name = %image,
)
)]
async fn pull_image_stream(
&self,
image: &str,
_auth: Option<&RegistryAuth>,
) -> Result<PullProgressStream> {
let (tx, rx) = mpsc::channel::<Result<PullProgress>>(32);
// Build the puller eagerly (cheap clone of cache + optional
// local registry) so the spawned task owns everything it needs.
let puller = {
let p = zlayer_registry::ImagePuller::with_cache(self.blob_cache.clone());
if let Some(ref registry) = self.local_registry {
p.with_local_registry(registry.clone())
} else {
p
}
};
let auth = self.auth_resolver.resolve(image);
let image_owned = image.to_string();
tokio::spawn(async move {
// Step 1: announce manifest pull.
if tx
.send(Ok(PullProgress::Status {
id: None,
status: "Pulling manifest".to_string(),
progress: None,
current: None,
total: None,
}))
.await
.is_err()
{
return;
}
// Step 2: fetch the manifest so we can enumerate layers.
// `pull_image_manifest` is exposed via the public client; the
// higher-level pull_image will redo this internally but the
// cost is one cached lookup and the API is the cleanest way
// to learn about layers up-front for streaming events.
let layers_meta: Vec<(String, u64)> =
match puller.pull_manifest(&image_owned, &auth).await {
Ok((manifest, _digest)) => manifest
.layers
.iter()
.map(|l| {
let size = u64::try_from(l.size).unwrap_or(0);
(l.digest.clone(), size)
})
.collect(),
Err(e) => {
let _ = tx
.send(Err(AgentError::PullFailed {
image: image_owned.clone(),
reason: format!("failed to pull manifest: {e}"),
}))
.await;
return;
}
};
// Step 3: emit one Status event per layer before the actual
// pull. The puller will retrieve cached layers near-instantly
// and uncached ones over the network; either way, consumers
// see one event per layer with the digest as `id`.
for (digest, size) in &layers_meta {
if tx
.send(Ok(PullProgress::Status {
id: Some(digest.clone()),
status: "Pulling fs layer".to_string(),
progress: None,
current: None,
total: if *size > 0 { Some(*size) } else { None },
}))
.await
.is_err()
{
return;
}
}
// Step 4: do the actual pull (uses the shared blob cache;
// already-cached layers are no-ops).
let force_refresh = false;
match puller
.pull_image_with_policy(&image_owned, &auth, force_refresh)
.await
{
Ok(_layers) => {
// Best-effort fetch of the registry digest sidecar so
// the `Done` event can carry a content-addressed
// identifier when one is available.
let _ = puller
.pull_image_config_with_policy(&image_owned, &auth, force_refresh)
.await;
let _ = tx
.send(Ok(PullProgress::Done {
reference: image_owned.clone(),
digest: None,
}))
.await;
}
Err(e) => {
let _ = tx
.send(Err(AgentError::PullFailed {
image: image_owned.clone(),
reason: format!("failed to pull image: {e}"),
}))
.await;
}
}
});
Ok(Box::pin(ReceiverStream::new(rx)))
}
/// Stream a TAR archive of a path inside the container's rootfs.
///
/// The youki backend doesn't run a daemon and has no live attach API to
/// the container's mount namespace, so we satisfy `archive_get` by
/// walking the on-disk rootfs at `<bundle>/rootfs<container_path>` and
/// streaming the TAR archive on the fly. This works for non-running
/// containers and for live containers whose rootfs has not been
/// `pivot_root`'d into a private mount namespace inaccessible from the
/// host (the standard Youki layout keeps the bundle rootfs visible).
#[instrument(
skip(self),
fields(
otel.name = "container.archive_get",
container.id = %self.container_id_str(id),
archive.path = %path,
)
)]
async fn archive_get(&self, id: &ContainerId, path: &str) -> Result<ArchiveStream> {
let bundle_path = self.bundle_path(id);
let rootfs_path = bundle_path.join("rootfs");
if !rootfs_path.exists() {
return Err(AgentError::NotFound {
container: self.container_id_str(id),
reason: format!(
"container rootfs '{}' does not exist on disk",
rootfs_path.display()
),
});
}
let rel = path.trim_start_matches('/');
let abs_target = if rel.is_empty() {
rootfs_path.clone()
} else {
rootfs_path.join(rel)
};
// Reject path-traversal attempts: the canonicalized target must live
// strictly under the rootfs.
let canon_root = tokio::fs::canonicalize(&rootfs_path).await.map_err(|e| {
AgentError::Internal(format!(
"failed to canonicalize rootfs '{}': {e}",
rootfs_path.display()
))
})?;
let canon_target = match tokio::fs::canonicalize(&abs_target).await {
Ok(p) => p,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Err(AgentError::NotFound {
container: self.container_id_str(id),
reason: format!("path '{path}' not found in container rootfs"),
});
}
Err(e) => {
return Err(AgentError::Internal(format!(
"failed to canonicalize path '{path}': {e}"
)));
}
};
if !canon_target.starts_with(&canon_root) {
return Err(AgentError::InvalidSpec(format!(
"archive path '{path}' escapes container rootfs"
)));
}
// Build the TAR archive on a blocking thread so we never block the
// async runtime on filesystem I/O.
let (tx, rx) = mpsc::channel::<Result<bytes::Bytes>>(8);
let target_for_task = canon_target.clone();
let path_for_task = path.to_string();
tokio::task::spawn_blocking(move || {
let result = build_tar_into_sender(&target_for_task, &path_for_task, &tx);
if let Err(e) = result {
let _ = tx.blocking_send(Err(e));
}
});
Ok(Box::pin(ReceiverStream::new(rx)))
}
/// Extract a TAR archive into the container at `path` by unpacking
/// directly into `<bundle>/rootfs<path>`.
#[instrument(
skip(self, tar_bytes),
fields(
otel.name = "container.archive_put",
container.id = %self.container_id_str(id),
archive.path = %path,
archive.bytes = tar_bytes.len(),
)
)]
async fn archive_put(
&self,
id: &ContainerId,
path: &str,
tar_bytes: bytes::Bytes,
opts: ArchivePutOptions,
) -> Result<()> {
let bundle_path = self.bundle_path(id);
let rootfs_path = bundle_path.join("rootfs");
if !rootfs_path.exists() {
return Err(AgentError::NotFound {
container: self.container_id_str(id),
reason: format!(
"container rootfs '{}' does not exist on disk",
rootfs_path.display()
),
});
}
let rel = path.trim_start_matches('/');
let abs_dest = if rel.is_empty() {
rootfs_path.clone()
} else {
rootfs_path.join(rel)
};
// The destination must already exist and be a directory (Docker's
// semantics).
match tokio::fs::metadata(&abs_dest).await {
Ok(m) if m.is_dir() => {}
Ok(_) => {
return Err(AgentError::InvalidSpec(format!(
"destination '{path}' inside container is not a directory"
)));
}
Err(_) => {
return Err(AgentError::NotFound {
container: self.container_id_str(id),
reason: format!("destination path '{path}' does not exist in container"),
});
}
}
// Validate that abs_dest stays under canonical rootfs.
let canon_root = tokio::fs::canonicalize(&rootfs_path).await.map_err(|e| {
AgentError::Internal(format!(
"failed to canonicalize rootfs '{}': {e}",
rootfs_path.display()
))
})?;
let canon_dest = tokio::fs::canonicalize(&abs_dest).await.map_err(|e| {
AgentError::Internal(format!("failed to canonicalize dest '{path}': {e}"))
})?;
if !canon_dest.starts_with(&canon_root) {
return Err(AgentError::InvalidSpec(format!(
"archive destination '{path}' escapes container rootfs"
)));
}
let dest_for_task = canon_dest.clone();
tokio::task::spawn_blocking(move || -> Result<()> {
unpack_tar_into(&dest_for_task, tar_bytes.as_ref(), opts)
})
.await
.map_err(|e| AgentError::Internal(format!("archive_put task panicked: {e}")))??;
Ok(())
}
/// Return path-stat metadata for `path` inside the container's rootfs.
#[instrument(
skip(self),
fields(
otel.name = "container.archive_head",
container.id = %self.container_id_str(id),
archive.path = %path,
)
)]
async fn archive_head(&self, id: &ContainerId, path: &str) -> Result<PathStat> {
let bundle_path = self.bundle_path(id);
let rootfs_path = bundle_path.join("rootfs");
if !rootfs_path.exists() {
return Err(AgentError::NotFound {
container: self.container_id_str(id),
reason: format!(
"container rootfs '{}' does not exist on disk",
rootfs_path.display()
),
});
}
let rel = path.trim_start_matches('/');
let abs_target = if rel.is_empty() {
rootfs_path.clone()
} else {
rootfs_path.join(rel)
};
// symlink_metadata so we report the link itself, not its target.
let meta = tokio::fs::symlink_metadata(&abs_target)
.await
.map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => AgentError::NotFound {
container: self.container_id_str(id),
reason: format!("path '{path}' not found in container rootfs"),
},
_ => AgentError::Internal(format!("failed to stat path '{path}': {e}")),
})?;
let name = std::path::Path::new(path)
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_string();
#[allow(clippy::cast_possible_wrap)]
let size = meta.len() as i64;
#[cfg(unix)]
let mode = {
use std::os::unix::fs::MetadataExt;
meta.mode()
};
#[cfg(not(unix))]
let mode: u32 = 0;
let mtime = meta
.modified()
.ok()
.and_then(|t| chrono::DateTime::<chrono::Utc>::from(t).to_rfc3339().into());
let link_target = if meta.file_type().is_symlink() {
tokio::fs::read_link(&abs_target)
.await
.ok()
.and_then(|p| p.to_str().map(String::from))
.unwrap_or_default()
} else {
String::new()
};
Ok(PathStat {
name,
size,
mode,
mtime,
link_target,
})
}
}
/// Walk `target` and stream a TAR archive into `tx` synchronously.
///
/// Used by `YoukiRuntime::archive_get` from a `spawn_blocking` task. Each
/// chunk emitted by the underlying `tar::Builder` is forwarded to the
/// channel as a `bytes::Bytes` so the async caller can pipe it straight to
/// the HTTP response body.
fn build_tar_into_sender(
target: &Path,
archive_path: &str,
tx: &mpsc::Sender<Result<bytes::Bytes>>,
) -> Result<()> {
use std::io::Write;
/// `std::io::Write` adapter that forwards every write into a tokio mpsc
/// channel as a `bytes::Bytes` chunk.
struct ChannelWriter<'a> {
tx: &'a mpsc::Sender<Result<bytes::Bytes>>,
}
impl Write for ChannelWriter<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let chunk = bytes::Bytes::copy_from_slice(buf);
self.tx
.blocking_send(Ok(chunk))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::BrokenPipe, e))?;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let writer = ChannelWriter { tx };
let mut builder = tar::Builder::new(writer);
builder.follow_symlinks(false);
// Determine the in-archive name (Docker uses the basename of the
// requested path so the TAR contains entries like `foo/...`).
let entry_name = std::path::Path::new(archive_path).file_name().map_or_else(
|| std::ffi::OsString::from("."),
std::ffi::OsStr::to_os_string,
);
let meta = std::fs::symlink_metadata(target)
.map_err(|e| AgentError::Internal(format!("failed to stat archive target: {e}")))?;
if meta.is_dir() {
builder
.append_dir_all(&entry_name, target)
.map_err(|e| AgentError::Internal(format!("failed to append dir to tar: {e}")))?;
} else {
let mut f = std::fs::File::open(target)
.map_err(|e| AgentError::Internal(format!("failed to open archive target: {e}")))?;
builder
.append_file(&entry_name, &mut f)
.map_err(|e| AgentError::Internal(format!("failed to append file to tar: {e}")))?;
}
builder
.finish()
.map_err(|e| AgentError::Internal(format!("failed to finalize tar: {e}")))?;
Ok(())
}
/// Unpack a TAR archive into `dest` synchronously, honouring
/// [`ArchivePutOptions`].
///
/// `no_overwrite_dir_non_dir` rejects the case where an entry would replace
/// an existing directory with a non-directory (or vice versa) — implemented
/// via a pre-pass over the archive's entries before extracting. `copy_uid_gid`
/// is forwarded to `tar::Archive::set_preserve_ownerships` so the unpacker
/// keeps the archive's uid/gid instead of chown'ing to the calling user.
fn unpack_tar_into(dest: &Path, tar_bytes: &[u8], opts: ArchivePutOptions) -> Result<()> {
if opts.no_overwrite_dir_non_dir {
// Pre-pass: detect directory/non-directory replacements.
let mut probe = tar::Archive::new(std::io::Cursor::new(tar_bytes));
let entries = probe
.entries()
.map_err(|e| AgentError::Internal(format!("failed to read tar entries: {e}")))?;
for entry in entries {
let entry =
entry.map_err(|e| AgentError::Internal(format!("invalid tar entry: {e}")))?;
let p = entry
.path()
.map_err(|e| AgentError::Internal(format!("invalid tar path: {e}")))?
.into_owned();
let dest_p = dest.join(&p);
if let Ok(existing) = std::fs::symlink_metadata(&dest_p) {
let entry_is_dir = entry.header().entry_type().is_dir();
if existing.is_dir() != entry_is_dir {
return Err(AgentError::InvalidSpec(format!(
"archive entry '{}' would replace a {} with a {}",
p.display(),
if existing.is_dir() {
"directory"
} else {
"non-directory"
},
if entry_is_dir {
"directory"
} else {
"non-directory"
}
)));
}
}
}
}
let mut archive = tar::Archive::new(std::io::Cursor::new(tar_bytes));
archive.set_preserve_permissions(true);
archive.set_preserve_ownerships(opts.copy_uid_gid);
archive
.unpack(dest)
.map_err(|e| AgentError::Internal(format!("failed to unpack archive: {e}")))?;
Ok(())
}
/// Build a TAR archive containing exactly one entry from a host path,
/// returning the bytes. Test-only helper used by
/// `archive_helpers_reject_dir_nondir_replacements`.
#[cfg(test)]
fn build_tar_from_path_for_test(src: &Path, entry_name: &str) -> Vec<u8> {
let mut buf = Vec::new();
{
let mut builder = tar::Builder::new(&mut buf);
builder.follow_symlinks(false);
let meta = std::fs::symlink_metadata(src).unwrap();
if meta.is_dir() {
builder.append_dir_all(entry_name, src).unwrap();
} else {
let mut f = std::fs::File::open(src).unwrap();
builder.append_file(entry_name, &mut f).unwrap();
}
builder.finish().unwrap();
}
buf
}
/// Read one row of `top`-style data for a host PID by parsing
/// `/proc/{pid}/status` and `/proc/{pid}/cmdline`.
///
/// The columns mirror the trait's documented `top_container` shape:
/// Write `value` to a cgroup v2 control file, demoting recoverable
/// errors (missing controller, invalid value) to warnings so a single
/// unsupported field doesn't sink the whole update. Hard errors
/// (permission denied, IO errors) propagate as
/// [`AgentError::Internal`].
async fn write_cgroup_file(
path: &std::path::Path,
value: &str,
warnings: &mut Vec<String>,
) -> Result<()> {
match tokio::fs::write(path, value).await {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
warnings.push(format!(
"cgroup file '{}' not found; controller may not be enabled",
path.display()
));
Ok(())
}
Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {
warnings.push(format!(
"cgroup write to '{}' rejected value '{}': {e}",
path.display(),
value
));
Ok(())
}
Err(e) => Err(AgentError::Internal(format!(
"failed to write '{}' to {}: {e}",
value,
path.display()
))),
}
}
/// `[UID, PID, PPID, STIME, CMD]`. Any field that fails to read is filled
/// with the empty string so the row width stays constant — `top` clients
/// expect the matrix to be rectangular.
async fn read_proc_row(pid: &str) -> Vec<String> {
let status_path = format!("/proc/{pid}/status");
let mut uid = String::new();
let mut parent_pid = String::new();
if let Ok(text) = tokio::fs::read_to_string(&status_path).await {
for line in text.lines() {
if let Some(rest) = line.strip_prefix("Uid:") {
// First field after the tabs is the real UID.
if let Some(first) = rest.split_whitespace().next() {
uid = first.to_string();
}
} else if let Some(rest) = line.strip_prefix("PPid:") {
parent_pid = rest.trim().to_string();
}
}
}
// STIME would normally come from `ps`'s formatter; we simulate it as
// the truncated wallclock seen on the start of the row read. This is
// a coarse approximation but matches Docker's documented contract:
// youki has no internal `ps` runner so we surface the best-effort
// string with the same shape.
let stime = chrono::Utc::now().format("%H:%M:%S").to_string();
let cmdline_path = format!("/proc/{pid}/cmdline");
let cmd = match tokio::fs::read(&cmdline_path).await {
Ok(bytes) => {
// /proc/{pid}/cmdline uses NUL separators; replace with spaces
// and trim the trailing NUL the kernel emits.
let normalised: Vec<u8> = bytes
.into_iter()
.map(|b| if b == 0 { b' ' } else { b })
.collect();
let mut s = String::from_utf8_lossy(&normalised).into_owned();
while s.ends_with(' ') {
s.pop();
}
s
}
Err(_) => String::new(),
};
vec![uid, pid.to_string(), parent_pid, stime, cmd]
}
/// Tail a single log file and forward each line as a [`LogChunk`] over
/// `tx`. Honours [`LogsStreamOptions::follow`] (poll-on-EOF), `tail`
/// (count `\n` bytes from end before streaming), and `since`/`until`
/// (wallclock filter applied to the moment each line is read — youki log
/// files do not carry per-line timestamps).
async fn stream_log_file(
path: PathBuf,
channel: LogChannel,
opts: LogsStreamOptions,
tx: mpsc::Sender<Result<LogChunk>>,
) -> Result<()> {
let file = match tokio::fs::File::open(&path).await {
Ok(f) => f,
Err(e) => {
let _ = tx
.send(Err(AgentError::Internal(format!(
"failed to open log file {}: {}",
path.display(),
e
))))
.await;
return Ok(());
}
};
let mut reader = BufReader::new(file);
// Apply `tail`: seek so that the next read begins at the start of
// the most-recent N lines. Implemented by counting newlines from the
// end of the file.
if let Some(tail) = opts.tail {
if tail > 0 {
let metadata = match reader.get_ref().metadata().await {
Ok(m) => m,
Err(e) => {
let _ = tx
.send(Err(AgentError::Internal(format!(
"failed to stat log file {}: {}",
path.display(),
e
))))
.await;
return Ok(());
}
};
let start = compute_tail_offset(reader.get_mut(), metadata.len(), tail).await;
if let Err(e) = reader.seek(std::io::SeekFrom::Start(start)).await {
let _ = tx
.send(Err(AgentError::Internal(format!(
"failed to seek log file {}: {}",
path.display(),
e
))))
.await;
return Ok(());
}
}
}
let mut line = String::new();
loop {
line.clear();
let bytes_read = match reader.read_line(&mut line).await {
Ok(n) => n,
Err(e) => {
let _ = tx
.send(Err(AgentError::Internal(format!(
"failed to read log file {}: {}",
path.display(),
e
))))
.await;
return Ok(());
}
};
if bytes_read == 0 {
// EOF — terminate unless we're in follow mode.
if !opts.follow {
return Ok(());
}
tokio::time::sleep(Duration::from_millis(200)).await;
continue;
}
let now = chrono::Utc::now();
let now_secs = now.timestamp();
if let Some(since) = opts.since {
if now_secs < since {
continue;
}
}
if let Some(until) = opts.until {
if now_secs > until {
// Past the cutoff — stop streaming this channel.
return Ok(());
}
}
let chunk = LogChunk {
stream: channel,
bytes: bytes::Bytes::copy_from_slice(line.as_bytes()),
timestamp: if opts.timestamps { Some(now) } else { None },
};
if tx.send(Ok(chunk)).await.is_err() {
// Receiver dropped — stop tailing.
return Ok(());
}
}
}
/// Compute the byte offset of the start of the last `tail` lines in a
/// file of size `file_len`. Reads backwards in 4 KiB chunks until enough
/// newlines have been seen or the file start is reached.
///
/// "The last N lines" is defined as the bytes following the
/// `(N+1)`th-from-end newline, mirroring `tail -n N`. If the file has
/// fewer than `N` lines, returns `0` (stream the whole file).
async fn compute_tail_offset(file: &mut tokio::fs::File, file_len: u64, tail: u64) -> u64 {
const CHUNK_USIZE: usize = 4096;
if file_len == 0 || tail == 0 {
return 0;
}
let chunk: u64 = CHUNK_USIZE as u64;
let target = tail.saturating_add(1); // the newline *before* the first wanted line
let mut pos = file_len;
let mut newlines: u64 = 0;
let mut buf = vec![0u8; CHUNK_USIZE];
while pos > 0 {
let read_len = std::cmp::min(chunk, pos);
pos -= read_len;
if file.seek(std::io::SeekFrom::Start(pos)).await.is_err() {
return 0;
}
// `read_len` is bounded by `CHUNK_USIZE`, so the cast is safe on
// every target ZLayer supports (Linux x86_64 / aarch64).
let slice_len = usize::try_from(read_len).unwrap_or(CHUNK_USIZE);
let buf_slice = &mut buf[..slice_len];
if tokio::io::AsyncReadExt::read_exact(file, buf_slice)
.await
.is_err()
{
return 0;
}
for (i, byte) in buf_slice.iter().enumerate().rev() {
if *byte == b'\n' {
newlines += 1;
if newlines == target {
// `i` is the index of the (tail+1)-th newline from
// the end *within the current chunk*. The first
// wanted byte sits immediately after it.
let absolute = pos + (i as u64) + 1;
return absolute.min(file_len);
}
}
}
}
0
}
/// Read a single [`StatsSample`] from `cgroup_path`. Wraps the existing
/// [`cgroups_stats::read_container_stats`] (cpu + memory) and supplements
/// it with `pids.current` / `pids.max` read directly from sysfs.
async fn read_stats_sample(cgroup_path: &Path, online_cpus: u32) -> Result<StatsSample> {
let stats = cgroups_stats::read_container_stats(cgroup_path)
.await
.map_err(|e| {
AgentError::Internal(format!(
"failed to read cgroup stats at {}: {}",
cgroup_path.display(),
e
))
})?;
let pids_current = read_u64_file(cgroup_path.join("pids.current"))
.await
.unwrap_or(0);
let pids_limit = read_pids_limit(cgroup_path.join("pids.max")).await;
let mem_limit_bytes = if stats.memory_limit == u64::MAX {
0
} else {
stats.memory_limit
};
Ok(StatsSample {
cpu_total_ns: stats.cpu_usage_usec.saturating_mul(1_000),
cpu_system_ns: 0,
online_cpus,
mem_used_bytes: stats.memory_bytes,
mem_limit_bytes,
net_rx_bytes: 0,
net_tx_bytes: 0,
blkio_read_bytes: 0,
blkio_write_bytes: 0,
pids_current,
pids_limit,
timestamp: chrono::Utc::now(),
})
}
/// Read a small text file containing a single decimal integer. Used for
/// `pids.current`. Returns `None` when the file is missing or unreadable
/// so the caller can substitute a sentinel (`0` for unknown counters).
async fn read_u64_file(path: PathBuf) -> Option<u64> {
let content = tokio::fs::read_to_string(&path).await.ok()?;
content.trim().parse::<u64>().ok()
}
/// Read `pids.max`, which is either a decimal integer or the literal
/// `"max"` (cgroup v2 sentinel for "no limit"). Returns `None` for
/// `"max"` or any read/parse error so the caller can leave
/// [`StatsSample::pids_limit`] unset.
async fn read_pids_limit(path: PathBuf) -> Option<u64> {
let content = tokio::fs::read_to_string(&path).await.ok()?;
let trimmed = content.trim();
if trimmed == "max" {
return None;
}
trimmed.parse::<u64>().ok()
}
/// Async duplex wrapper around a non-blocking PTY master fd, used by
/// [`YoukiRuntime::exec_pty`] to expose the master end as an
/// `AsyncRead + AsyncWrite + Send + Unpin` stream that fits the
/// [`ExecPtyStream`] trait object.
///
/// The fd is owned: dropping `PtyDuplex` closes the master, which causes the
/// kernel to send `SIGHUP` to the slave's controlling process group and tears
/// the session down cleanly.
struct PtyDuplex {
inner: tokio::io::unix::AsyncFd<OwnedFd>,
}
impl PtyDuplex {
fn new(fd: OwnedFd) -> Result<Self> {
let inner = tokio::io::unix::AsyncFd::new(fd)
.map_err(|e| AgentError::Internal(format!("AsyncFd::new on pty master: {e}")))?;
Ok(Self { inner })
}
}
impl tokio::io::AsyncRead for PtyDuplex {
#[allow(unsafe_code)]
fn poll_read(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
let this = self.get_mut();
loop {
let mut guard = std::task::ready!(this.inner.poll_read_ready(cx))?;
// SAFETY: `read(2)` only writes into the buffer, never reads from
// its uninitialised tail. We pass the unfilled portion as a raw
// pointer + length and tell `ReadBuf` how many bytes were
// actually written before exposing them as initialised. The
// pointer is valid for the duration of the `read` call because
// `buf` is borrowed mutably for the entire `poll_read` body.
let unfilled = unsafe {
std::slice::from_raw_parts_mut(
buf.unfilled_mut().as_mut_ptr().cast::<libc::c_void>(),
buf.remaining(),
)
};
let fd = guard.get_ref().as_raw_fd();
// SAFETY: `fd` is a valid PTY master fd owned by `self.inner`.
// `unfilled` points into a unique mutable borrow of `buf`. The
// syscall touches at most `unfilled.len()` bytes.
let rc = unsafe { libc::read(fd, unfilled.as_mut_ptr(), unfilled.len()) };
if rc < 0 {
let err = std::io::Error::last_os_error();
if err.kind() == std::io::ErrorKind::WouldBlock {
guard.clear_ready();
continue;
}
// Linux PTY masters return EIO once the slave hangs up; treat
// that as a clean EOF so callers see end-of-stream rather
// than a confusing error.
if err.raw_os_error() == Some(libc::EIO) {
return std::task::Poll::Ready(Ok(()));
}
return std::task::Poll::Ready(Err(err));
}
// We checked `rc < 0` above, so the cast is well-defined.
#[allow(clippy::cast_sign_loss)]
let n = rc as usize;
// SAFETY: the kernel just wrote `n` bytes into the unfilled
// tail; mark them as initialised + filled.
unsafe {
buf.assume_init(n);
}
buf.advance(n);
return std::task::Poll::Ready(Ok(()));
}
}
}
impl tokio::io::AsyncWrite for PtyDuplex {
#[allow(unsafe_code)]
fn poll_write(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
bufdata: &[u8],
) -> std::task::Poll<std::io::Result<usize>> {
let this = self.get_mut();
loop {
let mut guard = std::task::ready!(this.inner.poll_write_ready(cx))?;
let fd = guard.get_ref().as_raw_fd();
// SAFETY: `fd` is owned by `self.inner` and remains valid for
// the call. `bufdata` is a borrowed slice valid for `bufdata.len()`
// bytes; `write(2)` only reads from it.
let rc = unsafe { libc::write(fd, bufdata.as_ptr().cast(), bufdata.len()) };
if rc < 0 {
let err = std::io::Error::last_os_error();
if err.kind() == std::io::ErrorKind::WouldBlock {
guard.clear_ready();
continue;
}
return std::task::Poll::Ready(Err(err));
}
// We checked `rc < 0` above; safe to cast.
#[allow(clippy::cast_sign_loss)]
let n = rc as usize;
return std::task::Poll::Ready(Ok(n));
}
}
fn poll_flush(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
// PTYs have no userspace buffer; the kernel handles framing.
std::task::Poll::Ready(Ok(()))
}
fn poll_shutdown(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
// Closing the master happens on drop; no half-close on PTYs.
std::task::Poll::Ready(Ok(()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_youki_config_default() {
let config = YoukiConfig::default();
let dirs = zlayer_paths::ZLayerDirs::system_default();
assert_eq!(config.state_dir, dirs.containers());
assert_eq!(config.rootfs_dir, dirs.rootfs());
assert_eq!(config.bundle_dir, dirs.bundles());
assert_eq!(config.cache_dir, dirs.cache());
assert_eq!(config.volume_dir, dirs.volumes());
assert!(!config.use_systemd);
assert!(config.cache_type.is_none());
assert!(config.log_base_dir.is_none());
assert!(config.deployment_name.is_none());
}
#[test]
fn test_container_id_str() {
let id = ContainerId {
service: "myservice".to_string(),
replica: 1,
};
let expected = "myservice-1";
assert_eq!(format!("{}-{}", id.service, id.replica), expected);
}
#[test]
fn test_rootfs_path_sanitization() {
// Test that image names are sanitized for filesystem paths
let images = vec![
(
"docker.io/library/nginx:latest",
"docker.io_library_nginx_latest",
),
("ghcr.io/owner/repo:v1.0", "ghcr.io_owner_repo_v1.0"),
(
"registry.example.com/image@sha256:abc123",
"registry.example.com_image_sha256_abc123",
),
];
for (image, expected_suffix) in images {
let safe_name = image.replace(['/', ':', '@'], "_");
assert_eq!(safe_name, expected_suffix);
}
}
#[test]
fn test_map_status() {
// Test status mapping without runtime instance
let mappings = vec![
(ContainerStatus::Creating, "Pending"),
(ContainerStatus::Created, "Pending"),
(ContainerStatus::Running, "Running"),
(ContainerStatus::Stopped, "Exited"),
(ContainerStatus::Paused, "Stopping"),
];
for (status, expected) in mappings {
let state = match status {
ContainerStatus::Creating | ContainerStatus::Created => ContainerState::Pending,
ContainerStatus::Running => ContainerState::Running,
ContainerStatus::Stopped => ContainerState::Exited { code: 0 },
ContainerStatus::Paused => ContainerState::Stopping,
};
let state_str = match state {
ContainerState::Pending => "Pending",
ContainerState::Running => "Running",
ContainerState::Exited { .. } => "Exited",
ContainerState::Stopping => "Stopping",
_ => "Other",
};
assert_eq!(state_str, expected);
}
}
#[test]
fn test_log_paths() {
let config = YoukiConfig::default();
let dirs = zlayer_paths::ZLayerDirs::system_default();
let id = ContainerId {
service: "testservice".to_string(),
replica: 2,
};
let container_id = format!("{}-{}", id.service, id.replica);
let state_dir = config.state_dir.join(&container_id);
let stdout = state_dir.join("stdout.log");
let stderr = state_dir.join("stderr.log");
assert_eq!(stdout, dirs.containers().join("testservice-2/stdout.log"));
assert_eq!(stderr, dirs.containers().join("testservice-2/stderr.log"));
}
#[test]
fn test_youki_config_clone() {
let config = YoukiConfig {
state_dir: PathBuf::from("/custom/state"),
rootfs_dir: PathBuf::from("/custom/rootfs"),
bundle_dir: PathBuf::from("/custom/bundles"),
cache_dir: PathBuf::from("/custom/cache"),
volume_dir: PathBuf::from("/custom/volumes"),
use_systemd: true,
cache_type: Some(zlayer_registry::CacheType::memory()),
log_base_dir: Some(PathBuf::from("/var/log/zlayer")),
deployment_name: Some("myapp".to_string()),
};
let cloned = config.clone();
assert_eq!(cloned.state_dir, config.state_dir);
assert_eq!(cloned.rootfs_dir, config.rootfs_dir);
assert_eq!(cloned.bundle_dir, config.bundle_dir);
assert_eq!(cloned.cache_dir, config.cache_dir);
assert_eq!(cloned.volume_dir, config.volume_dir);
assert_eq!(cloned.use_systemd, config.use_systemd);
assert!(cloned.cache_type.is_some());
assert_eq!(cloned.log_base_dir, config.log_base_dir);
assert_eq!(cloned.deployment_name, config.deployment_name);
}
/// Test that `YoukiRuntime::new()` creates directories
#[tokio::test]
async fn test_youki_runtime_directory_creation() {
// Use a unique temp directory based on test run
let temp_base = std::env::temp_dir().join(format!("youki_test_{}", std::process::id()));
let config = YoukiConfig {
state_dir: temp_base.join("state"),
rootfs_dir: temp_base.join("rootfs"),
bundle_dir: temp_base.join("bundles"),
cache_dir: temp_base.join("cache"),
volume_dir: temp_base.join("volumes"),
use_systemd: false,
cache_type: None,
log_base_dir: None,
deployment_name: None,
};
// Clean up any previous test run
let _ = std::fs::remove_dir_all(&temp_base);
// This should succeed and create all directories
let result = YoukiRuntime::new(config.clone(), None).await;
assert!(
result.is_ok(),
"Failed to create runtime: {:?}",
result.err()
);
// Verify directories were created
assert!(config.state_dir.exists());
assert!(config.rootfs_dir.exists());
assert!(config.bundle_dir.exists());
assert!(config.cache_dir.exists());
assert!(config.volume_dir.exists());
// Clean up
let _ = std::fs::remove_dir_all(&temp_base);
}
/// Tail-from-end logic: write a file with a known number of lines,
/// ask `compute_tail_offset` for the start of the last 2, and verify
/// the offset lands on the third-to-last line's start.
#[tokio::test]
async fn youki_tail_offset_returns_last_n_lines() {
let dir = std::env::temp_dir().join(format!(
"zlayer_tail_test_{}_{}",
std::process::id(),
chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
));
tokio::fs::create_dir_all(&dir).await.unwrap();
let path = dir.join("log");
// 5 lines, each ending in '\n', distinguishable bodies.
tokio::fs::write(&path, b"a\nbb\nccc\ndddd\neeeee\n")
.await
.unwrap();
let mut file = tokio::fs::File::open(&path).await.unwrap();
let len = file.metadata().await.unwrap().len();
// Last 2 lines are "dddd\n" + "eeeee\n", combined 11 bytes;
// offset should be `len - 11`.
let offset = compute_tail_offset(&mut file, len, 2).await;
assert_eq!(offset, len - 11, "expected last-2-lines offset");
// Tail >= total line count must yield 0 (whole file).
let mut file = tokio::fs::File::open(&path).await.unwrap();
assert_eq!(compute_tail_offset(&mut file, len, 100).await, 0);
let _ = tokio::fs::remove_dir_all(&dir).await;
}
/// `stream_log_file` with `follow=false` reads all existing lines and
/// then terminates cleanly at EOF. Exercises the non-follow path
/// without needing a real container.
#[tokio::test]
async fn youki_logs_stream_reads_static_file_without_follow() {
let dir = std::env::temp_dir().join(format!(
"zlayer_logs_static_{}_{}",
std::process::id(),
chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
));
tokio::fs::create_dir_all(&dir).await.unwrap();
let path = dir.join("stdout.log");
tokio::fs::write(&path, b"hello\nworld\n").await.unwrap();
let (tx, mut rx) = mpsc::channel::<Result<LogChunk>>(8);
let opts = LogsStreamOptions {
follow: false,
tail: None,
since: None,
until: None,
timestamps: true,
stdout: true,
stderr: false,
};
stream_log_file(path, LogChannel::Stdout, opts, tx)
.await
.unwrap();
let mut received = Vec::new();
while let Some(item) = rx.recv().await {
let chunk = item.unwrap();
received.push(chunk);
}
assert_eq!(
received.len(),
2,
"expected 2 chunks, got {}",
received.len()
);
assert_eq!(received[0].stream, LogChannel::Stdout);
assert!(received[0].timestamp.is_some());
assert_eq!(received[0].bytes.as_ref(), b"hello\n");
assert_eq!(received[1].bytes.as_ref(), b"world\n");
let _ = tokio::fs::remove_dir_all(&dir).await;
}
/// `read_stats_sample` requires a real cgroup v2 directory layout
/// that we cannot easily synthesise on a non-root test host, so this
/// test is `#[ignore]`-d. Run with
/// `cargo test -p zlayer-agent youki_stats_sample_reads_cgroup -- --ignored`
/// inside a real container or as root with a fake cgroup path.
#[tokio::test]
#[ignore = "requires a real cgroup v2 hierarchy"]
async fn youki_stats_sample_reads_cgroup() {
// Try the host's own root cgroup as a smoke target — every cgroup
// v2 system has /sys/fs/cgroup/cpu.stat and memory.current at the
// root. pids.current/pids.max may be missing at the root, which
// is fine — read_stats_sample treats them as 0/None.
let path = Path::new("/sys/fs/cgroup");
let sample = read_stats_sample(path, 1).await.unwrap();
// CPU monotonically increases; memory.current is non-negative.
assert!(sample.cpu_total_ns < u64::MAX);
assert!(sample.online_cpus >= 1);
}
/// `(rows, cols)` from the resize channel converts to `nix::pty::Winsize`
/// with `ws_row` and `ws_col` populated and the pixel fields zeroed.
/// Pure shape conversion — no fd touched.
#[test]
fn youki_pty_resize_winsize_shape() {
let (rows, cols): (u16, u16) = (24, 80);
let ws = nix::pty::Winsize {
ws_row: rows,
ws_col: cols,
ws_xpixel: 0,
ws_ypixel: 0,
};
assert_eq!(ws.ws_row, 24);
assert_eq!(ws.ws_col, 80);
assert_eq!(ws.ws_xpixel, 0);
assert_eq!(ws.ws_ypixel, 0);
// Maximum values still fit cleanly through the channel and the
// ioctl payload (winsize is u16 across all four fields).
let ws_max = nix::pty::Winsize {
ws_row: u16::MAX,
ws_col: u16::MAX,
ws_xpixel: 0,
ws_ypixel: 0,
};
assert_eq!(ws_max.ws_row, u16::MAX);
assert_eq!(ws_max.ws_col, u16::MAX);
}
/// `exec_pty` rejects an empty command vector with `InvalidSpec` before
/// touching libcontainer or allocating a PTY. Exercises the input
/// validation path without needing a running container.
#[tokio::test]
async fn youki_exec_pty_rejects_empty_command() {
let temp_base =
std::env::temp_dir().join(format!("youki_exec_pty_empty_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&temp_base);
let config = YoukiConfig {
state_dir: temp_base.join("state"),
rootfs_dir: temp_base.join("rootfs"),
bundle_dir: temp_base.join("bundles"),
cache_dir: temp_base.join("cache"),
volume_dir: temp_base.join("volumes"),
use_systemd: false,
cache_type: None,
log_base_dir: None,
deployment_name: None,
};
let runtime = YoukiRuntime::new(config, None).await.unwrap();
let id = ContainerId {
service: "missing".to_string(),
replica: 0,
};
let result = runtime
.exec_pty(
&id,
ExecOptions {
command: Vec::new(),
tty: true,
..ExecOptions::default()
},
)
.await;
assert!(matches!(result, Err(AgentError::InvalidSpec(_))));
let _ = std::fs::remove_dir_all(&temp_base);
}
/// `PtyDuplex::new` accepts a non-blocking PTY master fd produced by
/// `nix::pty::openpty` and exposes it as `AsyncRead + AsyncWrite`. This
/// is purely a wrapper smoke test — no container, no exec.
///
/// Marked `#[ignore]` because real PTY allocation requires
/// `/dev/ptmx`, which CI sandboxes occasionally restrict; run with
/// `cargo test -p zlayer-agent --features youki-runtime youki_pty_duplex_wraps_master -- --ignored`.
#[tokio::test]
#[ignore = "requires /dev/ptmx access"]
async fn youki_pty_duplex_wraps_master() {
let pair = nix::pty::openpty(None, None).expect("openpty");
nix::fcntl::fcntl(
&pair.master,
nix::fcntl::FcntlArg::F_SETFL(nix::fcntl::OFlag::O_NONBLOCK),
)
.expect("F_SETFL O_NONBLOCK");
let _duplex = PtyDuplex::new(pair.master).expect("PtyDuplex::new");
// Slave is dropped here, which makes the master EOF on next read.
drop(pair.slave);
}
/// Sanity-check the `unpack_tar_into` + `build_tar_into_sender`
/// helpers used by the youki archive endpoints: a TAR archive built
/// from a host directory must round-trip back to the same file tree
/// when unpacked elsewhere.
#[tokio::test]
async fn archive_helpers_round_trip_a_directory_tree() {
// Build a small tree.
let src_dir = tempfile::tempdir().unwrap();
let nested = src_dir.path().join("a/b");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("c.txt"), b"deep file").unwrap();
std::fs::write(src_dir.path().join("top.txt"), b"top file").unwrap();
// Drive `build_tar_into_sender` through a Tokio mpsc and collect bytes.
let (tx, mut rx) = mpsc::channel::<Result<bytes::Bytes>>(8);
let target = src_dir.path().to_path_buf();
let handle =
tokio::task::spawn_blocking(move || super::build_tar_into_sender(&target, "root", &tx));
let mut buf = Vec::new();
while let Some(item) = rx.recv().await {
let chunk = item.expect("tar chunk");
buf.extend_from_slice(&chunk);
}
handle.await.unwrap().unwrap();
// Unpack into a fresh dir; the entry should land under `root/`.
let dest_dir = tempfile::tempdir().unwrap();
super::unpack_tar_into(
dest_dir.path(),
&buf,
crate::runtime::ArchivePutOptions::default(),
)
.unwrap();
assert!(dest_dir.path().join("root/top.txt").exists());
assert!(dest_dir.path().join("root/a/b/c.txt").exists());
assert_eq!(
std::fs::read(dest_dir.path().join("root/a/b/c.txt")).unwrap(),
b"deep file",
);
}
/// `unpack_tar_into` with `no_overwrite_dir_non_dir = true` must
/// reject an archive entry that would replace an existing directory
/// with a non-directory (or vice versa).
#[tokio::test]
async fn archive_helpers_reject_dir_nondir_replacements() {
let dest_dir = tempfile::tempdir().unwrap();
// Pre-create a directory at `target`.
let target = dest_dir.path().join("target");
std::fs::create_dir_all(&target).unwrap();
// Build an archive whose only entry is a *file* named `target`.
let src_file_dir = tempfile::tempdir().unwrap();
let src_file = src_file_dir.path().join("target");
std::fs::write(&src_file, b"i am a file").unwrap();
let bytes = super::build_tar_from_path_for_test(&src_file, "target");
let opts = crate::runtime::ArchivePutOptions {
no_overwrite_dir_non_dir: true,
copy_uid_gid: false,
};
let err = super::unpack_tar_into(dest_dir.path(), &bytes, opts).unwrap_err();
assert!(
matches!(err, AgentError::InvalidSpec(_)),
"expected InvalidSpec, got {err:?}"
);
}
}