summaryrefslogtreecommitdiffstats
path: root/src/shrpx.cc
blob: 86d00dcc91c5701b6c2bc2eb3b332e1e1c7c2546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
/*
 * nghttp2 - HTTP/2 C Library
 *
 * Copyright (c) 2012 Tatsuhiro Tsujikawa
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "shrpx.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_SOCKET_H
#  include <sys/socket.h>
#endif // HAVE_SYS_SOCKET_H
#include <sys/un.h>
#ifdef HAVE_NETDB_H
#  include <netdb.h>
#endif // HAVE_NETDB_H
#include <signal.h>
#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif // HAVE_NETINET_IN_H
#include <netinet/tcp.h>
#ifdef HAVE_ARPA_INET_H
#  include <arpa/inet.h>
#endif // HAVE_ARPA_INET_H
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif // HAVE_UNISTD_H
#include <getopt.h>
#ifdef HAVE_SYSLOG_H
#  include <syslog.h>
#endif // HAVE_SYSLOG_H
#ifdef HAVE_LIMITS_H
#  include <limits.h>
#endif // HAVE_LIMITS_H
#ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
#endif // HAVE_SYS_TIME_H
#include <sys/resource.h>
#ifdef HAVE_LIBSYSTEMD
#  include <systemd/sd-daemon.h>
#endif // HAVE_LIBSYSTEMD
#ifdef HAVE_LIBBPF
#  include <bpf/libbpf.h>
#endif // HAVE_LIBBPF

#include <cinttypes>
#include <limits>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <initializer_list>
#include <random>
#include <span>

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <ev.h>

#include <nghttp2/nghttp2.h>

#ifdef ENABLE_HTTP3
#  include <ngtcp2/ngtcp2.h>
#  include <nghttp3/nghttp3.h>
#endif // ENABLE_HTTP3

#include "shrpx_config.h"
#include "shrpx_tls.h"
#include "shrpx_log_config.h"
#include "shrpx_worker.h"
#include "shrpx_http2_upstream.h"
#include "shrpx_http2_session.h"
#include "shrpx_worker_process.h"
#include "shrpx_process.h"
#include "shrpx_signal.h"
#include "shrpx_connection.h"
#include "shrpx_log.h"
#include "shrpx_http.h"
#include "util.h"
#include "app_helper.h"
#include "tls.h"
#include "template.h"
#include "allocator.h"
#include "ssl_compat.h"
#include "xsi_strerror.h"

extern char **environ;

using namespace nghttp2;

namespace shrpx {

// Deprecated: Environment variables to tell new binary the listening
// socket's file descriptors.  They are not close-on-exec.
constexpr auto ENV_LISTENER4_FD = "NGHTTPX_LISTENER4_FD"_sr;
constexpr auto ENV_LISTENER6_FD = "NGHTTPX_LISTENER6_FD"_sr;

// Deprecated: Environment variable to tell new binary the port number
// the current binary is listening to.
constexpr auto ENV_PORT = "NGHTTPX_PORT"_sr;

// Deprecated: Environment variable to tell new binary the listening
// socket's file descriptor if frontend listens UNIX domain socket.
constexpr auto ENV_UNIX_FD = "NGHTTP2_UNIX_FD"_sr;
// Deprecated: Environment variable to tell new binary the UNIX domain
// socket path.
constexpr auto ENV_UNIX_PATH = "NGHTTP2_UNIX_PATH"_sr;

// Prefix of environment variables to tell new binary the listening
// socket's file descriptor.  They are not close-on-exec.  For TCP
// socket, the value must be comma separated 2 parameters: tcp,<FD>.
// <FD> is file descriptor.  For UNIX domain socket, the value must be
// comma separated 3 parameters: unix,<FD>,<PATH>.  <FD> is file
// descriptor.  <PATH> is a path to UNIX domain socket.
constexpr auto ENV_ACCEPT_PREFIX = "NGHTTPX_ACCEPT_"_sr;

// This environment variable contains PID of the original main
// process, assuming that it created this main process as a result of
// SIGUSR2.  The new main process is expected to send QUIT signal to
// the original main process to shut it down gracefully.
constexpr auto ENV_ORIG_PID = "NGHTTPX_ORIG_PID"_sr;

// Prefix of environment variables to tell new binary the QUIC IPC
// file descriptor and Worker ID of the lingering worker process.  The
// value must be comma separated parameters:
//
// <FD>,<WORKER_ID_0>,<WORKER_ID_1>,...,<WORKER_ID_I>
//
// <FD> is the file descriptor.  <WORKER_ID_I> is the I-th Worker ID
// in hex encoded string.
constexpr auto ENV_QUIC_WORKER_PROCESS_PREFIX =
    "NGHTTPX_QUIC_WORKER_PROCESS_"_sr;

#ifndef _KERNEL_FASTOPEN
#  define _KERNEL_FASTOPEN
// conditional define for TCP_FASTOPEN mostly on ubuntu
#  ifndef TCP_FASTOPEN
#    define TCP_FASTOPEN 23
#  endif

// conditional define for SOL_TCP mostly on ubuntu
#  ifndef SOL_TCP
#    define SOL_TCP 6
#  endif
#endif

// This configuration is fixed at the first startup of the main
// process, and does not change after subsequent reloadings.
struct StartupConfig {
  // This contains all options given in command-line.
  std::vector<std::pair<StringRef, StringRef>> cmdcfgs;
  // The current working directory where this process started.
  char *cwd;
  // The pointer to original argv (not sure why we have this?)
  char **original_argv;
  // The pointer to argv, this is a deep copy of original argv.
  char **argv;
  // The number of elements in argv.
  int argc;
};

namespace {
StartupConfig suconfig;
} // namespace

struct InheritedAddr {
  // IP address if TCP socket.  Otherwise, UNIX domain socket path.
  StringRef host;
  uint16_t port;
  // true if UNIX domain socket path
  bool host_unix;
  int fd;
  bool used;
};

namespace {
void signal_cb(struct ev_loop *loop, ev_signal *w, int revents);
} // namespace

namespace {
void worker_process_child_cb(struct ev_loop *loop, ev_child *w, int revents);
} // namespace

struct WorkerProcess {
  WorkerProcess(struct ev_loop *loop, pid_t worker_pid, int ipc_fd
#ifdef ENABLE_HTTP3
                ,
                int quic_ipc_fd, std::vector<WorkerID> worker_ids, uint16_t seq
#endif // ENABLE_HTTP3
                )
      : loop(loop),
        worker_pid(worker_pid),
        ipc_fd(ipc_fd)
#ifdef ENABLE_HTTP3
        ,
        quic_ipc_fd(quic_ipc_fd),
        worker_ids(std::move(worker_ids)),
        seq(seq)
#endif // ENABLE_HTTP3
  {
    ev_child_init(&worker_process_childev, worker_process_child_cb, worker_pid,
                  0);
    worker_process_childev.data = this;
    ev_child_start(loop, &worker_process_childev);
  }

  ~WorkerProcess() {
    ev_child_stop(loop, &worker_process_childev);

#ifdef ENABLE_HTTP3
    if (quic_ipc_fd != -1) {
      close(quic_ipc_fd);
    }
#endif // ENABLE_HTTP3

    if (ipc_fd != -1) {
      shutdown(ipc_fd, SHUT_WR);
      close(ipc_fd);
    }
  }

  ev_child worker_process_childev;
  struct ev_loop *loop;
  pid_t worker_pid;
  int ipc_fd;
  std::chrono::steady_clock::time_point termination_deadline;
#ifdef ENABLE_HTTP3
  int quic_ipc_fd;
  std::vector<WorkerID> worker_ids;
  uint16_t seq;
#endif // ENABLE_HTTP3
};

namespace {
void reload_config();
} // namespace

namespace {
std::deque<std::unique_ptr<WorkerProcess>> worker_processes;

#ifdef ENABLE_HTTP3
uint16_t worker_process_seq;
#endif // ENABLE_HTTP3
} // namespace

namespace {
ev_timer worker_process_grace_period_timer;
} // namespace

namespace {
void worker_process_grace_period_timercb(struct ev_loop *loop, ev_timer *w,
                                         int revents) {
  auto now = std::chrono::steady_clock::now();
  auto next_repeat = std::chrono::steady_clock::duration::zero();

  for (auto it = std::begin(worker_processes);
       it != std::end(worker_processes);) {
    auto &wp = *it;
    if (wp->termination_deadline.time_since_epoch().count() == 0) {
      ++it;

      continue;
    }

    auto d = wp->termination_deadline - now;
    if (d.count() > 0) {
      if (next_repeat == std::chrono::steady_clock::duration::zero() ||
          d < next_repeat) {
        next_repeat = d;
      }

      ++it;

      continue;
    }

    LOG(NOTICE) << "Deleting worker process pid=" << wp->worker_pid
                << " because its grace shutdown period is over";

    it = worker_processes.erase(it);
  }

  if (next_repeat.count() > 0) {
    w->repeat = util::ev_tstamp_from(next_repeat);
    ev_timer_again(loop, w);

    return;
  }

  ev_timer_stop(loop, w);
}
} // namespace

namespace {
void worker_process_set_termination_deadline(WorkerProcess *wp,
                                             struct ev_loop *loop) {
  auto config = get_config();

  if (!(config->worker_process_grace_shutdown_period > 0.)) {
    return;
  }

  wp->termination_deadline =
      std::chrono::steady_clock::now() +
      util::duration_from(config->worker_process_grace_shutdown_period);

  if (!ev_is_active(&worker_process_grace_period_timer)) {
    worker_process_grace_period_timer.repeat =
        config->worker_process_grace_shutdown_period;

    ev_timer_again(loop, &worker_process_grace_period_timer);
  }
}
} // namespace

namespace {
void worker_process_add(std::unique_ptr<WorkerProcess> wp) {
  worker_processes.push_back(std::move(wp));
}
} // namespace

namespace {
void worker_process_remove(const WorkerProcess *wp, struct ev_loop *loop) {
  for (auto it = std::begin(worker_processes); it != std::end(worker_processes);
       ++it) {
    auto &s = *it;

    if (s.get() != wp) {
      continue;
    }

    worker_processes.erase(it);

    if (worker_processes.empty()) {
      ev_timer_stop(loop, &worker_process_grace_period_timer);
    }

    break;
  }
}
} // namespace

namespace {
void worker_process_adjust_limit() {
  auto config = get_config();

  if (config->max_worker_processes &&
      worker_processes.size() > config->max_worker_processes) {
    worker_processes.pop_front();
  }
}
} // namespace

namespace {
void worker_process_remove_all(struct ev_loop *loop) {
  std::deque<std::unique_ptr<WorkerProcess>>().swap(worker_processes);

  ev_timer_stop(loop, &worker_process_grace_period_timer);
}
} // namespace

namespace {
// Send signal |signum| to all worker processes, and clears
// worker_processes.
void worker_process_kill(int signum, struct ev_loop *loop) {
  for (auto &s : worker_processes) {
    if (s->worker_pid == -1) {
      continue;
    }
    kill(s->worker_pid, signum);
  }
  worker_process_remove_all(loop);
}
} // namespace

namespace {
int save_pid() {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  auto config = get_config();

  constexpr auto SUFFIX = ".XXXXXX"_sr;
  auto &pid_file = config->pid_file;

  auto len = config->pid_file.size() + SUFFIX.size();
  auto buf = std::make_unique<char[]>(len + 1);
  auto p = buf.get();

  p = std::copy(std::begin(pid_file), std::end(pid_file), p);
  p = std::copy(std::begin(SUFFIX), std::end(SUFFIX), p);
  *p = '\0';

  auto temp_path = buf.get();

  auto fd = mkstemp(temp_path);
  if (fd == -1) {
    auto error = errno;
    LOG(ERROR) << "Could not save PID to file " << pid_file << ": "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  auto content = util::utos(config->pid) + '\n';

  if (write(fd, content.c_str(), content.size()) == -1) {
    auto error = errno;
    LOG(ERROR) << "Could not save PID to file " << pid_file << ": "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  if (fsync(fd) == -1) {
    auto error = errno;
    LOG(ERROR) << "Could not save PID to file " << pid_file << ": "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  close(fd);

  if (rename(temp_path, pid_file.data()) == -1) {
    auto error = errno;
    LOG(ERROR) << "Could not save PID to file " << pid_file << ": "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

    unlink(temp_path);

    return -1;
  }

  if (config->uid != 0) {
    if (chown(pid_file.data(), config->uid, config->gid) == -1) {
      auto error = errno;
      LOG(WARN) << "Changing owner of pid file " << pid_file << " failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
  }

  return 0;
}
} // namespace

namespace {
void shrpx_sd_notifyf(int unset_environment, const char *format, ...) {
#ifdef HAVE_LIBSYSTEMD
  va_list args;

  va_start(args, format);
  sd_notifyf(unset_environment, format, va_arg(args, char *));
  va_end(args);
#endif // HAVE_LIBSYSTEMD
}
} // namespace

namespace {
void exec_binary() {
  int rv;
  sigset_t oldset;
  std::array<char, STRERROR_BUFSIZE> errbuf;

  LOG(NOTICE) << "Executing new binary";

  shrpx_sd_notifyf(0, "RELOADING=1");

  rv = shrpx_signal_block_all(&oldset);
  if (rv != 0) {
    auto error = errno;
    LOG(ERROR) << "Blocking all signals failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

    return;
  }

  auto pid = fork();

  if (pid != 0) {
    if (pid == -1) {
      auto error = errno;
      LOG(ERROR) << "fork() failed errno=" << error;
    } else {
      // update PID tracking information in systemd
      shrpx_sd_notifyf(0, "MAINPID=%d\n", pid);
    }

    rv = shrpx_signal_set(&oldset);

    if (rv != 0) {
      auto error = errno;
      LOG(FATAL) << "Restoring signal mask failed: "
                 << xsi_strerror(error, errbuf.data(), errbuf.size());

      exit(EXIT_FAILURE);
    }

    return;
  }

  // child process

  shrpx_signal_unset_main_proc_ign_handler();

  rv = shrpx_signal_unblock_all();
  if (rv != 0) {
    auto error = errno;
    LOG(ERROR) << "Unblocking all signals failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

    nghttp2_Exit(EXIT_FAILURE);
  }

  auto exec_path =
      util::get_exec_path(suconfig.argc, suconfig.argv, suconfig.cwd);

  if (!exec_path) {
    LOG(ERROR) << "Could not resolve the executable path";
    nghttp2_Exit(EXIT_FAILURE);
  }

  auto argv = std::make_unique<char *[]>(suconfig.argc + 1);

  argv[0] = exec_path;
  for (int i = 1; i < suconfig.argc; ++i) {
    argv[i] = suconfig.argv[i];
  }
  argv[suconfig.argc] = nullptr;

  size_t envlen = 0;
  for (char **p = environ; *p; ++p, ++envlen)
    ;

  auto config = get_config();
  auto &listenerconf = config->conn.listener;

  // 2 for ENV_ORIG_PID and terminal nullptr.
  auto envp = std::make_unique<char *[]>(envlen + listenerconf.addrs.size() +
                                         worker_processes.size() + 2);
  size_t envidx = 0;

  std::vector<ImmutableString> fd_envs;
  for (size_t i = 0; i < listenerconf.addrs.size(); ++i) {
    auto &addr = listenerconf.addrs[i];
    auto s = std::string{ENV_ACCEPT_PREFIX};
    s += util::utos(i + 1);
    s += '=';
    if (addr.host_unix) {
      s += "unix,";
      s += util::utos(addr.fd);
      s += ',';
      s += addr.host;
    } else {
      s += "tcp,";
      s += util::utos(addr.fd);
    }

    fd_envs.emplace_back(s);
    envp[envidx++] = const_cast<char *>(fd_envs.back().c_str());
  }

  auto ipc_fd_str = std::string{ENV_ORIG_PID};
  ipc_fd_str += '=';
  ipc_fd_str += util::utos(config->pid);
  envp[envidx++] = const_cast<char *>(ipc_fd_str.c_str());

#ifdef ENABLE_HTTP3
  std::vector<ImmutableString> quic_lwps;
  for (size_t i = 0; i < worker_processes.size(); ++i) {
    auto &wp = worker_processes[i];
    auto s = std::string{ENV_QUIC_WORKER_PROCESS_PREFIX};
    s += util::utos(i + 1);
    s += '=';
    s += util::utos(wp->quic_ipc_fd);
    for (auto &wid : wp->worker_ids) {
      s += ',';
      s += util::format_hex(std::span{&wid, 1});
    }

    quic_lwps.emplace_back(s);
    envp[envidx++] = const_cast<char *>(quic_lwps.back().c_str());
  }
#endif // ENABLE_HTTP3

  for (size_t i = 0; i < envlen; ++i) {
    auto env = StringRef{environ[i]};
    if (util::starts_with(env, ENV_ACCEPT_PREFIX) ||
        util::starts_with(env, ENV_LISTENER4_FD) ||
        util::starts_with(env, ENV_LISTENER6_FD) ||
        util::starts_with(env, ENV_PORT) ||
        util::starts_with(env, ENV_UNIX_FD) ||
        util::starts_with(env, ENV_UNIX_PATH) ||
        util::starts_with(env, ENV_ORIG_PID) ||
        util::starts_with(env, ENV_QUIC_WORKER_PROCESS_PREFIX)) {
      continue;
    }

    envp[envidx++] = environ[i];
  }

  envp[envidx++] = nullptr;

  if (LOG_ENABLED(INFO)) {
    LOG(INFO) << "cmdline";
    for (int i = 0; argv[i]; ++i) {
      LOG(INFO) << i << ": " << argv[i];
    }
    LOG(INFO) << "environ";
    for (int i = 0; envp[i]; ++i) {
      LOG(INFO) << i << ": " << envp[i];
    }
  }

  // restores original stderr
  restore_original_fds();

  // reloading finished
  shrpx_sd_notifyf(0, "READY=1");

  if (execve(argv[0], argv.get(), envp.get()) == -1) {
    auto error = errno;
    LOG(ERROR) << "execve failed: errno=" << error;
    nghttp2_Exit(EXIT_FAILURE);
  }
}
} // namespace

namespace {
void ipc_send(WorkerProcess *wp, uint8_t ipc_event) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  ssize_t nwrite;
  while ((nwrite = write(wp->ipc_fd, &ipc_event, 1)) == -1 && errno == EINTR)
    ;

  if (nwrite < 0) {
    auto error = errno;
    LOG(ERROR) << "Could not send IPC event to worker process: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return;
  }

  if (nwrite == 0) {
    LOG(ERROR) << "Could not send IPC event due to pipe overflow";
    return;
  }
}
} // namespace

namespace {
void reopen_log(WorkerProcess *wp) {
  LOG(NOTICE) << "Reopening log files: main process";

  auto config = get_config();
  auto &loggingconf = config->logging;

  (void)reopen_log_files(loggingconf);
  redirect_stderr_to_errorlog(loggingconf);
  ipc_send(wp, SHRPX_IPC_REOPEN_LOG);
}
} // namespace

namespace {
void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) {
  switch (w->signum) {
  case REOPEN_LOG_SIGNAL:
    for (auto &wp : worker_processes) {
      reopen_log(wp.get());
    }

    return;
  case EXEC_BINARY_SIGNAL:
    exec_binary();
    return;
  case GRACEFUL_SHUTDOWN_SIGNAL: {
    auto &listenerconf = get_config()->conn.listener;
    for (auto &addr : listenerconf.addrs) {
      close(addr.fd);
    }

    for (auto &wp : worker_processes) {
      ipc_send(wp.get(), SHRPX_IPC_GRACEFUL_SHUTDOWN);
      worker_process_set_termination_deadline(wp.get(), loop);
    }

    return;
  }
  case RELOAD_SIGNAL:
    reload_config();

    return;
  default:
    worker_process_kill(w->signum, loop);
    ev_break(loop);
    return;
  }
}
} // namespace

namespace {
void worker_process_child_cb(struct ev_loop *loop, ev_child *w, int revents) {
  auto wp = static_cast<WorkerProcess *>(w->data);

  log_chld(w->rpid, w->rstatus, "Worker process");

  worker_process_remove(wp, loop);

  if (worker_processes.empty()) {
    ev_break(loop);
  }
}
} // namespace

namespace {
int create_unix_domain_server_socket(UpstreamAddr &faddr,
                                     std::vector<InheritedAddr> &iaddrs) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  auto found = std::find_if(
      std::begin(iaddrs), std::end(iaddrs), [&faddr](const InheritedAddr &ia) {
        return !ia.used && ia.host_unix && ia.host == faddr.host;
      });

  if (found != std::end(iaddrs)) {
    LOG(NOTICE) << "Listening on UNIX domain socket " << faddr.host
                << (faddr.tls ? ", tls" : "");
    (*found).used = true;
    faddr.fd = (*found).fd;
    faddr.hostport = "localhost"_sr;

    return 0;
  }

#ifdef SOCK_NONBLOCK
  auto fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (fd == -1) {
    auto error = errno;
    LOG(FATAL) << "socket() syscall failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }
#else  // !SOCK_NONBLOCK
  auto fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    auto error = errno;
    LOG(FATAL) << "socket() syscall failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }
  util::make_socket_nonblocking(fd);
#endif // !SOCK_NONBLOCK
  int val = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
                 static_cast<socklen_t>(sizeof(val))) == -1) {
    auto error = errno;
    LOG(FATAL) << "Failed to set SO_REUSEADDR option to listener socket: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    close(fd);
    return -1;
  }

  sockaddr_union addr;
  addr.un.sun_family = AF_UNIX;
  if (faddr.host.size() + 1 > sizeof(addr.un.sun_path)) {
    LOG(FATAL) << "UNIX domain socket path " << faddr.host << " is too long > "
               << sizeof(addr.un.sun_path);
    close(fd);
    return -1;
  }
  // copy path including terminal NULL
  std::copy_n(faddr.host.data(), faddr.host.size() + 1, addr.un.sun_path);

  // unlink (remove) already existing UNIX domain socket path
  unlink(faddr.host.data());

  if (bind(fd, &addr.sa, sizeof(addr.un)) != 0) {
    auto error = errno;
    LOG(FATAL) << "Failed to bind UNIX domain socket: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    close(fd);
    return -1;
  }

  auto &listenerconf = get_config()->conn.listener;

  if (listen(fd, listenerconf.backlog) != 0) {
    auto error = errno;
    LOG(FATAL) << "Failed to listen to UNIX domain socket: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
    close(fd);
    return -1;
  }

  LOG(NOTICE) << "Listening on UNIX domain socket " << faddr.host
              << (faddr.tls ? ", tls" : "");

  faddr.fd = fd;
  faddr.hostport = "localhost"_sr;

  return 0;
}
} // namespace

namespace {
int create_tcp_server_socket(UpstreamAddr &faddr,
                             std::vector<InheritedAddr> &iaddrs) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int fd = -1;
  int rv;

  auto &listenerconf = get_config()->conn.listener;

  auto service = util::utos(faddr.port);
  addrinfo hints{};
  hints.ai_family = faddr.family;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
#ifdef AI_ADDRCONFIG
  hints.ai_flags |= AI_ADDRCONFIG;
#endif // AI_ADDRCONFIG

  auto node = faddr.host == "*"_sr ? nullptr : faddr.host.data();

  addrinfo *res, *rp;
  rv = getaddrinfo(node, service.c_str(), &hints, &res);
#ifdef AI_ADDRCONFIG
  if (rv != 0) {
    // Retry without AI_ADDRCONFIG
    hints.ai_flags &= ~AI_ADDRCONFIG;
    rv = getaddrinfo(node, service.c_str(), &hints, &res);
  }
#endif // AI_ADDRCONFIG
  if (rv != 0) {
    LOG(FATAL) << "Unable to get IPv" << (faddr.family == AF_INET ? "4" : "6")
               << " address for " << faddr.host << ", port " << faddr.port
               << ": " << gai_strerror(rv);
    return -1;
  }

  auto res_d = defer(freeaddrinfo, res);

  std::array<char, NI_MAXHOST> host;

  for (rp = res; rp; rp = rp->ai_next) {

    rv = getnameinfo(rp->ai_addr, rp->ai_addrlen, host.data(), host.size(),
                     nullptr, 0, NI_NUMERICHOST);

    if (rv != 0) {
      LOG(WARN) << "getnameinfo() failed: " << gai_strerror(rv);
      continue;
    }

    auto host_sr = StringRef{host.data()};

    auto found = std::find_if(std::begin(iaddrs), std::end(iaddrs),
                              [&host_sr, &faddr](const InheritedAddr &ia) {
                                return !ia.used && !ia.host_unix &&
                                       ia.host == host_sr &&
                                       ia.port == faddr.port;
                              });

    if (found != std::end(iaddrs)) {
      (*found).used = true;
      fd = (*found).fd;
      break;
    }

#ifdef SOCK_NONBLOCK
    fd =
        socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK, rp->ai_protocol);
    if (fd == -1) {
      auto error = errno;
      LOG(WARN) << "socket() syscall failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
      continue;
    }
#else  // !SOCK_NONBLOCK
    fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (fd == -1) {
      auto error = errno;
      LOG(WARN) << "socket() syscall failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
      continue;
    }
    util::make_socket_nonblocking(fd);
#endif // !SOCK_NONBLOCK
    int val = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
                   static_cast<socklen_t>(sizeof(val))) == -1) {
      auto error = errno;
      LOG(WARN) << "Failed to set SO_REUSEADDR option to listener socket: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
      close(fd);
      continue;
    }

#ifdef IPV6_V6ONLY
    if (faddr.family == AF_INET6) {
      if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,
                     static_cast<socklen_t>(sizeof(val))) == -1) {
        auto error = errno;
        LOG(WARN) << "Failed to set IPV6_V6ONLY option to listener socket: "
                  << xsi_strerror(error, errbuf.data(), errbuf.size());
        close(fd);
        continue;
      }
    }
#endif // IPV6_V6ONLY

#ifdef TCP_DEFER_ACCEPT
    val = 3;
    if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &val,
                   static_cast<socklen_t>(sizeof(val))) == -1) {
      auto error = errno;
      LOG(WARN) << "Failed to set TCP_DEFER_ACCEPT option to listener socket: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
#endif // TCP_DEFER_ACCEPT

    // When we are executing new binary, and the old binary did not
    // bind privileged port (< 1024) for some reason, binding to those
    // ports will fail with permission denied error.
    if (bind(fd, rp->ai_addr, rp->ai_addrlen) == -1) {
      auto error = errno;
      LOG(WARN) << "bind() syscall failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
      close(fd);
      continue;
    }

    if (listenerconf.fastopen > 0) {
      val = listenerconf.fastopen;
      if (setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &val,
                     static_cast<socklen_t>(sizeof(val))) == -1) {
        auto error = errno;
        LOG(WARN) << "Failed to set TCP_FASTOPEN option to listener socket: "
                  << xsi_strerror(error, errbuf.data(), errbuf.size());
      }
    }

    if (listen(fd, listenerconf.backlog) == -1) {
      auto error = errno;
      LOG(WARN) << "listen() syscall failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
      close(fd);
      continue;
    }

    break;
  }

  if (!rp) {
    LOG(FATAL) << "Listening " << (faddr.family == AF_INET ? "IPv4" : "IPv6")
               << " socket failed";

    return -1;
  }

  faddr.fd = fd;
  faddr.hostport = util::make_http_hostport(mod_config()->balloc,
                                            StringRef{host.data()}, faddr.port);

  LOG(NOTICE) << "Listening on " << faddr.hostport
              << (faddr.tls ? ", tls" : "");

  return 0;
}
} // namespace

namespace {
// Returns array of InheritedAddr constructed from |config|.  This
// function is intended to be used when reloading configuration, and
// |config| is usually a current configuration.
std::vector<InheritedAddr>
get_inherited_addr_from_config(BlockAllocator &balloc, Config *config) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;

  auto &listenerconf = config->conn.listener;

  std::vector<InheritedAddr> iaddrs(listenerconf.addrs.size());

  size_t idx = 0;
  for (auto &addr : listenerconf.addrs) {
    auto &iaddr = iaddrs[idx++];

    if (addr.host_unix) {
      iaddr.host = addr.host;
      iaddr.host_unix = true;
      iaddr.fd = addr.fd;

      continue;
    }

    iaddr.port = addr.port;
    iaddr.fd = addr.fd;

    // We have to getsockname/getnameinfo for fd, since we may have
    // '*' appear in addr.host, which makes comparison against "real"
    // address fail.

    sockaddr_union su;
    socklen_t salen = sizeof(su);

    // We already added entry to iaddrs.  Even if we got errors, we
    // don't remove it.  This is required because we have to close the
    // socket if it is not reused.  The empty host name usually does
    // not match anything.

    if (getsockname(addr.fd, &su.sa, &salen) != 0) {
      auto error = errno;
      LOG(WARN) << "getsockname() syscall failed (fd=" << addr.fd
                << "): " << xsi_strerror(error, errbuf.data(), errbuf.size());
      continue;
    }

    std::array<char, NI_MAXHOST> host;
    rv = getnameinfo(&su.sa, salen, host.data(), host.size(), nullptr, 0,
                     NI_NUMERICHOST);
    if (rv != 0) {
      LOG(WARN) << "getnameinfo() failed (fd=" << addr.fd
                << "): " << gai_strerror(rv);
      continue;
    }

    iaddr.host = make_string_ref(balloc, StringRef{host.data()});
  }

  return iaddrs;
}
} // namespace

namespace {
// Returns array of InheritedAddr constructed from environment
// variables.  This function handles the old environment variable
// names used in 1.7.0 or earlier.
std::vector<InheritedAddr> get_inherited_addr_from_env(Config *config) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;
  std::vector<InheritedAddr> iaddrs;

  {
    // Upgrade from 1.7.0 or earlier
    auto portenv = getenv(ENV_PORT.data());
    if (portenv) {
      size_t i = 1;
      for (const auto &env_name : {ENV_LISTENER4_FD, ENV_LISTENER6_FD}) {
        auto fdenv = getenv(env_name.data());
        if (fdenv) {
          auto name = std::string{ENV_ACCEPT_PREFIX};
          name += util::utos(i);
          std::string value = "tcp,";
          value += fdenv;
          setenv(name.c_str(), value.c_str(), 0);
          ++i;
        }
      }
    } else {
      // The return value of getenv may be allocated statically.
      if (getenv(ENV_UNIX_PATH.data()) && getenv(ENV_UNIX_FD.data())) {
        auto name = std::string{ENV_ACCEPT_PREFIX};
        name += '1';
        std::string value = "unix,";
        value += getenv(ENV_UNIX_FD.data());
        value += ',';
        value += getenv(ENV_UNIX_PATH.data());
        setenv(name.c_str(), value.c_str(), 0);
      }
    }
  }

  for (size_t i = 1;; ++i) {
    auto name = std::string{ENV_ACCEPT_PREFIX};
    name += util::utos(i);
    auto env = getenv(name.c_str());
    if (!env) {
      break;
    }

    if (LOG_ENABLED(INFO)) {
      LOG(INFO) << "Read env " << name << "=" << env;
    }

    auto end_type = strchr(env, ',');
    if (!end_type) {
      continue;
    }

    auto type = StringRef(env, end_type);
    auto value = end_type + 1;

    if (type == "unix"_sr) {
      auto endfd = strchr(value, ',');
      if (!endfd) {
        continue;
      }
      auto fd = util::parse_uint(StringRef{value, endfd});
      if (!fd) {
        LOG(WARN) << "Could not parse file descriptor from "
                  << std::string(value, endfd - value);
        continue;
      }

      auto path = endfd + 1;
      if (strlen(path) == 0) {
        LOG(WARN) << "Empty UNIX domain socket path (fd=" << *fd << ")";
        close(*fd);
        continue;
      }

      if (LOG_ENABLED(INFO)) {
        LOG(INFO) << "Inherit UNIX domain socket fd=" << *fd
                  << ", path=" << path;
      }

      InheritedAddr addr{};
      addr.host = make_string_ref(config->balloc, StringRef{path});
      addr.host_unix = true;
      addr.fd = static_cast<int>(*fd);
      iaddrs.push_back(std::move(addr));
    }

    if (type == "tcp"_sr) {
      auto fd = util::parse_uint(value);
      if (!fd) {
        LOG(WARN) << "Could not parse file descriptor from " << value;
        continue;
      }

      sockaddr_union su;
      socklen_t salen = sizeof(su);

      if (getsockname(*fd, &su.sa, &salen) != 0) {
        auto error = errno;
        LOG(WARN) << "getsockname() syscall failed (fd=" << *fd
                  << "): " << xsi_strerror(error, errbuf.data(), errbuf.size());
        close(*fd);
        continue;
      }

      uint16_t port;

      switch (su.storage.ss_family) {
      case AF_INET:
        port = ntohs(su.in.sin_port);
        break;
      case AF_INET6:
        port = ntohs(su.in6.sin6_port);
        break;
      default:
        close(*fd);
        continue;
      }

      std::array<char, NI_MAXHOST> host;
      rv = getnameinfo(&su.sa, salen, host.data(), host.size(), nullptr, 0,
                       NI_NUMERICHOST);
      if (rv != 0) {
        LOG(WARN) << "getnameinfo() failed (fd=" << *fd
                  << "): " << gai_strerror(rv);
        close(*fd);
        continue;
      }

      if (LOG_ENABLED(INFO)) {
        LOG(INFO) << "Inherit TCP socket fd=" << *fd
                  << ", address=" << host.data() << ", port=" << port;
      }

      InheritedAddr addr{};
      addr.host = make_string_ref(config->balloc, StringRef{host.data()});
      addr.port = static_cast<uint16_t>(port);
      addr.fd = static_cast<int>(*fd);
      iaddrs.push_back(std::move(addr));
      continue;
    }
  }

  return iaddrs;
}
} // namespace

namespace {
// Closes all sockets which are not reused.
void close_unused_inherited_addr(const std::vector<InheritedAddr> &iaddrs) {
  for (auto &ia : iaddrs) {
    if (ia.used) {
      continue;
    }

    close(ia.fd);
  }
}
} // namespace

namespace {
// Returns the PID of the original main process from environment
// variable ENV_ORIG_PID.
pid_t get_orig_pid_from_env() {
  auto s = getenv(ENV_ORIG_PID.data());
  if (s == nullptr) {
    return -1;
  }
  return util::parse_uint(s).value_or(-1);
}
} // namespace

#ifdef ENABLE_HTTP3
namespace {
std::vector<QUICLingeringWorkerProcess>
    inherited_quic_lingering_worker_processes;
} // namespace

namespace {
std::vector<QUICLingeringWorkerProcess>
get_inherited_quic_lingering_worker_process_from_env() {
  std::vector<QUICLingeringWorkerProcess> lwps;

  for (size_t i = 1;; ++i) {
    auto name = std::string{ENV_QUIC_WORKER_PROCESS_PREFIX};
    name += util::utos(i);
    auto env = getenv(name.c_str());
    if (!env) {
      break;
    }

    if (LOG_ENABLED(INFO)) {
      LOG(INFO) << "Read env " << name << "=" << env;
    }

    auto envend = env + strlen(env);

    auto end_fd = std::find(env, envend, ',');
    if (end_fd == envend) {
      continue;
    }

    auto fd = util::parse_uint(StringRef{env, end_fd});
    if (!fd) {
      LOG(WARN) << "Could not parse file descriptor from "
                << StringRef{env, static_cast<size_t>(end_fd - env)};
      continue;
    }

    if (LOG_ENABLED(INFO)) {
      LOG(INFO) << "Inherit worker process QUIC IPC socket fd=" << *fd;
    }

    util::make_socket_closeonexec(*fd);

    std::vector<WorkerID> worker_ids;

    auto p = end_fd + 1;
    for (;;) {
      auto end = std::find(p, envend, ',');

      auto hex_wid = StringRef{p, end};
      if (hex_wid.size() != SHRPX_QUIC_WORKER_IDLEN * 2 ||
          !util::is_hex_string(hex_wid)) {
        LOG(WARN) << "Found invalid WorkerID=" << hex_wid;
        break;
      }

      if (LOG_ENABLED(INFO)) {
        LOG(INFO) << "Inherit worker process WorkerID=" << hex_wid;
      }

      worker_ids.emplace_back();

      util::decode_hex(reinterpret_cast<uint8_t *>(&worker_ids.back()),
                       hex_wid);

      if (end == envend) {
        break;
      }

      p = end + 1;
    }

    lwps.emplace_back(std::move(worker_ids), *fd);
  }

  if (!lwps.empty()) {
    const auto &lwp = lwps.back();

    if (!lwp.worker_ids.empty() &&
        worker_process_seq <= lwp.worker_ids[0].worker_process) {
      worker_process_seq = lwp.worker_ids[0].worker_process;
      ++worker_process_seq;
    }
  }

  return lwps;
}
} // namespace
#endif // ENABLE_HTTP3

namespace {
int create_acceptor_socket(Config *config, std::vector<InheritedAddr> &iaddrs) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  auto &listenerconf = config->conn.listener;

  for (auto &addr : listenerconf.addrs) {
    if (addr.host_unix) {
      if (create_unix_domain_server_socket(addr, iaddrs) != 0) {
        return -1;
      }

      if (config->uid != 0) {
        // fd is not associated to inode, so we cannot use fchown(2)
        // here.  https://lkml.org/lkml/2004/11/1/84
        if (chown(addr.host.data(), config->uid, config->gid) == -1) {
          auto error = errno;
          LOG(WARN) << "Changing owner of UNIX domain socket " << addr.host
                    << " failed: "
                    << xsi_strerror(error, errbuf.data(), errbuf.size());
        }
      }
      continue;
    }

    if (create_tcp_server_socket(addr, iaddrs) != 0) {
      return -1;
    }
  }

  return 0;
}
} // namespace

namespace {
int call_daemon() {
#ifdef __sgi
  return _daemonize(0, 0, 0, 0);
#else // !__sgi
#  ifdef HAVE_LIBSYSTEMD
  if (sd_booted() && (getenv("NOTIFY_SOCKET") != nullptr)) {
    LOG(NOTICE) << "Daemonising disabled under systemd";
    chdir("/");
    return 0;
  }
#  endif // HAVE_LIBSYSTEMD
  return util::daemonize(0, 0);
#endif   // !__sgi
}
} // namespace

namespace {
// Opens IPC socket used to communicate with worker proess.  The
// communication is unidirectional; that is main process sends
// messages to the worker process.  On success, ipc_fd[0] is for
// reading, and ipc_fd[1] for writing, just like pipe(2).
int create_ipc_socket(std::span<int, 2> ipc_fd) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;

  rv = pipe(ipc_fd.data());
  if (rv == -1) {
    auto error = errno;
    LOG(WARN) << "Failed to create pipe to communicate worker process: "
              << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  for (auto fd : ipc_fd) {
    util::make_socket_nonblocking(fd);
    util::make_socket_closeonexec(fd);
  }

  return 0;
}
} // namespace

namespace {
int create_worker_process_ready_ipc_socket(std::span<int, 2> ipc_fd) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;

  rv = socketpair(AF_UNIX, SOCK_DGRAM, 0, ipc_fd.data());
  if (rv == -1) {
    auto error = errno;
    LOG(WARN) << "Failed to create socket pair to communicate worker process "
                 "readiness: "
              << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  for (auto fd : ipc_fd) {
    util::make_socket_closeonexec(fd);
  }

  util::make_socket_nonblocking(ipc_fd[0]);

  return 0;
}
} // namespace

#ifdef ENABLE_HTTP3
namespace {
int create_quic_ipc_socket(std::span<int, 2> quic_ipc_fd) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;

  rv = socketpair(AF_UNIX, SOCK_DGRAM, 0, quic_ipc_fd.data());
  if (rv == -1) {
    auto error = errno;
    LOG(WARN) << "Failed to create socket pair to communicate worker process: "
              << xsi_strerror(error, errbuf.data(), errbuf.size());
    return -1;
  }

  for (auto fd : quic_ipc_fd) {
    util::make_socket_nonblocking(fd);
  }

  return 0;
}
} // namespace

namespace {
int generate_worker_id(std::vector<WorkerID> &worker_ids, uint16_t wp_seq,
                       const Config *config) {
  auto &apiconf = config->api;
  auto &quicconf = config->quic;

  size_t num_wid;
  if (config->single_thread) {
    num_wid = 1;
  } else {
    num_wid = config->num_worker;

    // API endpoint occupies the one dedicated worker thread.
    // Although such worker never gets QUIC traffic, we create Worker
    // ID for it to make code a bit simpler.
    if (apiconf.enabled) {
      ++num_wid;
    }
  }

  worker_ids.resize(num_wid);

  uint16_t idx = 0;

  for (auto &wid : worker_ids) {
    wid.server = quicconf.server_id;
    wid.worker_process = wp_seq;
    wid.thread = idx++;
  }

  return 0;
}
} // namespace

namespace {
std::vector<QUICLingeringWorkerProcess>
collect_quic_lingering_worker_processes() {
  std::vector<QUICLingeringWorkerProcess> quic_lwps{
      std::begin(inherited_quic_lingering_worker_processes),
      std::end(inherited_quic_lingering_worker_processes)};

  for (auto &wp : worker_processes) {
    quic_lwps.emplace_back(wp->worker_ids, wp->quic_ipc_fd);
  }

  return quic_lwps;
}
} // namespace
#endif // ENABLE_HTTP3

namespace {
ev_signal reopen_log_signalev;
ev_signal exec_binary_signalev;
ev_signal graceful_shutdown_signalev;
ev_signal reload_signalev;
} // namespace

namespace {
void start_signal_watchers(struct ev_loop *loop) {
  ev_signal_init(&reopen_log_signalev, signal_cb, REOPEN_LOG_SIGNAL);
  ev_signal_start(loop, &reopen_log_signalev);

  ev_signal_init(&exec_binary_signalev, signal_cb, EXEC_BINARY_SIGNAL);
  ev_signal_start(loop, &exec_binary_signalev);

  ev_signal_init(&graceful_shutdown_signalev, signal_cb,
                 GRACEFUL_SHUTDOWN_SIGNAL);
  ev_signal_start(loop, &graceful_shutdown_signalev);

  ev_signal_init(&reload_signalev, signal_cb, RELOAD_SIGNAL);
  ev_signal_start(loop, &reload_signalev);
}
} // namespace

namespace {
void shutdown_signal_watchers(struct ev_loop *loop) {
  ev_signal_stop(loop, &reload_signalev);
  ev_signal_stop(loop, &graceful_shutdown_signalev);
  ev_signal_stop(loop, &exec_binary_signalev);
  ev_signal_stop(loop, &reopen_log_signalev);
}
} // namespace

namespace {
// A pair of connected socket with which a worker process tells main
// process that it is ready for service.  A worker process writes its
// PID to worker_process_ready_ipc_fd[1] and main process reads it
// from worker_process_ready_ipc_fd[0].
std::array<int, 2> worker_process_ready_ipc_fd;
} // namespace

namespace {
ev_io worker_process_ready_ipcev;
} // namespace

namespace {
// PID received via NGHTTPX_ORIG_PID environment variable.
pid_t orig_pid = -1;
} // namespace

namespace {
void worker_process_ready_ipc_readcb(struct ev_loop *loop, ev_io *w,
                                     int revents) {
  std::array<uint8_t, 8> buf;
  ssize_t nread;

  while ((nread = read(w->fd, buf.data(), buf.size())) == -1 && errno == EINTR)
    ;

  if (nread == -1) {
    std::array<char, STRERROR_BUFSIZE> errbuf;
    auto error = errno;

    LOG(ERROR) << "Failed to read data from worker process ready IPC channel: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

    return;
  }

  if (nread == 0) {
    return;
  }

  if (nread != sizeof(pid_t)) {
    LOG(ERROR) << "Read " << nread
               << " bytes from worker process ready IPC channel";

    return;
  }

  pid_t pid;

  memcpy(&pid, buf.data(), sizeof(pid));

  LOG(NOTICE) << "Worker process pid=" << pid << " is ready";

  for (auto &wp : worker_processes) {
    // Send graceful shutdown signal to all worker processes prior to
    // pid.
    if (wp->worker_pid == pid) {
      break;
    }

    LOG(INFO) << "Sending graceful shutdown event to worker process pid="
              << wp->worker_pid;

    ipc_send(wp.get(), SHRPX_IPC_GRACEFUL_SHUTDOWN);
    worker_process_set_termination_deadline(wp.get(), loop);
  }

  if (orig_pid != -1) {
    LOG(NOTICE) << "Send QUIT signal to the original main process to tell "
                   "that we are ready to serve requests.";
    kill(orig_pid, SIGQUIT);

    orig_pid = -1;
  }
}
} // namespace

namespace {
void start_worker_process_ready_ipc_watcher(struct ev_loop *loop) {
  ev_io_init(&worker_process_ready_ipcev, worker_process_ready_ipc_readcb,
             worker_process_ready_ipc_fd[0], EV_READ);
  ev_io_start(loop, &worker_process_ready_ipcev);
}
} // namespace

namespace {
void shutdown_worker_process_ready_ipc_watcher(struct ev_loop *loop) {
  ev_io_stop(loop, &worker_process_ready_ipcev);
}
} // namespace

namespace {
// Creates worker process, and returns PID of worker process.  On
// success, file descriptor for IPC (send only) is assigned to
// |main_ipc_fd|.  In child process, we will close file descriptors
// which are inherited from previous configuration/process, but not
// used in the current configuration.
pid_t fork_worker_process(int &main_ipc_fd
#ifdef ENABLE_HTTP3
                          ,
                          int &wp_quic_ipc_fd
#endif // ENABLE_HTTP3
                          ,
                          const std::vector<InheritedAddr> &iaddrs
#ifdef ENABLE_HTTP3
                          ,
                          std::vector<WorkerID> worker_ids,
                          std::vector<QUICLingeringWorkerProcess> quic_lwps
#endif // ENABLE_HTTP3
) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  int rv;
  sigset_t oldset;

  std::array<int, 2> ipc_fd;

  rv = create_ipc_socket(ipc_fd);
  if (rv != 0) {
    return -1;
  }

#ifdef ENABLE_HTTP3
  std::array<int, 2> quic_ipc_fd;

  rv = create_quic_ipc_socket(quic_ipc_fd);
  if (rv != 0) {
    return -1;
  }
#endif // ENABLE_HTTP3

  rv = shrpx_signal_block_all(&oldset);
  if (rv != 0) {
    auto error = errno;
    LOG(ERROR) << "Blocking all signals failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

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

    return -1;
  }

  auto config = get_config();

  pid_t pid = 0;

  if (!config->single_process) {
    pid = fork();
  }

  if (pid == 0) {
    // We are in new process now, update pid for logger.
    log_config()->pid = getpid();

    ev_loop_fork(EV_DEFAULT);

    for (auto &addr : config->conn.listener.addrs) {
      util::make_socket_closeonexec(addr.fd);
    }

#ifdef ENABLE_HTTP3
    util::make_socket_closeonexec(quic_ipc_fd[0]);

    for (auto &lwp : quic_lwps) {
      util::make_socket_closeonexec(lwp.quic_ipc_fd);
    }

    for (auto &wp : worker_processes) {
      util::make_socket_closeonexec(wp->quic_ipc_fd);
      // Do not close quic_ipc_fd.
      wp->quic_ipc_fd = -1;
    }
#endif // ENABLE_HTTP3

    if (!config->single_process) {
      close(worker_process_ready_ipc_fd[0]);
      shutdown_worker_process_ready_ipc_watcher(EV_DEFAULT);

      shutdown_signal_watchers(EV_DEFAULT);
    }

    // Remove all WorkerProcesses to stop any registered watcher on
    // default loop.
    worker_process_remove_all(EV_DEFAULT);

    close_unused_inherited_addr(iaddrs);

    shrpx_signal_set_worker_proc_ign_handler();

    rv = shrpx_signal_unblock_all();
    if (rv != 0) {
      auto error = errno;
      LOG(FATAL) << "Unblocking all signals failed: "
                 << xsi_strerror(error, errbuf.data(), errbuf.size());

      if (config->single_process) {
        exit(EXIT_FAILURE);
      } else {
        nghttp2_Exit(EXIT_FAILURE);
      }
    }

    if (!config->single_process) {
      close(ipc_fd[1]);
#ifdef ENABLE_HTTP3
      close(quic_ipc_fd[1]);
#endif // ENABLE_HTTP3
    }

    WorkerProcessConfig wpconf{
        .ipc_fd = ipc_fd[0],
        .ready_ipc_fd = worker_process_ready_ipc_fd[1],
#ifdef ENABLE_HTTP3
        .worker_ids = std::move(worker_ids),
        .quic_ipc_fd = quic_ipc_fd[0],
        .quic_lingering_worker_processes = std::move(quic_lwps),
#endif // ENABLE_HTTP3
    };
    rv = worker_process_event_loop(&wpconf);
    if (rv != 0) {
      LOG(FATAL) << "Worker process returned error";

      if (config->single_process) {
        exit(EXIT_FAILURE);
      } else {
        nghttp2_Exit(EXIT_FAILURE);
      }
    }

    LOG(NOTICE) << "Worker process shutting down momentarily";

    // call exit(...) instead of nghttp2_Exit to get leak sanitizer report
    if (config->single_process) {
      exit(EXIT_SUCCESS);
    } else {
      nghttp2_Exit(EXIT_SUCCESS);
    }
  }

  // parent process
  if (pid == -1) {
    auto error = errno;
    LOG(ERROR) << "Could not spawn worker process: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());
  }

  rv = shrpx_signal_set(&oldset);
  if (rv != 0) {
    auto error = errno;
    LOG(FATAL) << "Restoring signal mask failed: "
               << xsi_strerror(error, errbuf.data(), errbuf.size());

    exit(EXIT_FAILURE);
  }

  if (pid == -1) {
    close(ipc_fd[0]);
    close(ipc_fd[1]);
#ifdef ENABLE_HTTP3
    close(quic_ipc_fd[0]);
    close(quic_ipc_fd[1]);
#endif // ENABLE_HTTP3

    return -1;
  }

  close(ipc_fd[0]);
#ifdef ENABLE_HTTP3
  close(quic_ipc_fd[0]);
#endif // ENABLE_HTTP3

  main_ipc_fd = ipc_fd[1];
#ifdef ENABLE_HTTP3
  wp_quic_ipc_fd = quic_ipc_fd[1];
#endif // ENABLE_HTTP3

  LOG(NOTICE) << "Worker process [" << pid << "] spawned";

  return pid;
}
} // namespace

namespace {
int event_loop() {
  std::array<char, STRERROR_BUFSIZE> errbuf;

  shrpx_signal_set_main_proc_ign_handler();

  auto config = mod_config();

  if (config->daemon) {
    if (call_daemon() == -1) {
      auto error = errno;
      LOG(FATAL) << "Failed to daemonize: "
                 << xsi_strerror(error, errbuf.data(), errbuf.size());
      return -1;
    }

    // We get new PID after successful daemon().
    mod_config()->pid = getpid();

    // daemon redirects stderr file descriptor to /dev/null, so we
    // need this.
    redirect_stderr_to_errorlog(config->logging);
  }

  // update systemd PID tracking
  shrpx_sd_notifyf(0, "MAINPID=%d\n", config->pid);

  {
    auto iaddrs = get_inherited_addr_from_env(config);

    if (create_acceptor_socket(config, iaddrs) != 0) {
      return -1;
    }

    close_unused_inherited_addr(iaddrs);
  }

  orig_pid = get_orig_pid_from_env();

#ifdef ENABLE_HTTP3
  inherited_quic_lingering_worker_processes =
      get_inherited_quic_lingering_worker_process_from_env();
#endif // ENABLE_HTTP3

  auto loop = ev_default_loop(config->ev_loop_flags);

  int ipc_fd = 0;
#ifdef ENABLE_HTTP3
  int quic_ipc_fd = 0;

  auto quic_lwps = collect_quic_lingering_worker_processes();

  std::vector<WorkerID> worker_ids;

  if (generate_worker_id(worker_ids, worker_process_seq, config) != 0) {
    return -1;
  }
#endif // ENABLE_HTTP3

  if (!config->single_process) {
    start_signal_watchers(loop);
  }

  create_worker_process_ready_ipc_socket(worker_process_ready_ipc_fd);
  start_worker_process_ready_ipc_watcher(loop);

  auto pid = fork_worker_process(ipc_fd
#ifdef ENABLE_HTTP3
                                 ,
                                 quic_ipc_fd
#endif // ENABLE_HTTP3
                                 ,
                                 {}
#ifdef ENABLE_HTTP3
                                 ,
                                 worker_ids, std::move(quic_lwps)
#endif // ENABLE_HTTP3
  );

  if (pid == -1) {
    return -1;
  }

  ev_timer_init(&worker_process_grace_period_timer,
                worker_process_grace_period_timercb, 0., 0.);

  worker_process_add(std::make_unique<WorkerProcess>(
      loop, pid, ipc_fd
#ifdef ENABLE_HTTP3
      ,
      quic_ipc_fd, std::move(worker_ids), worker_process_seq++
#endif // ENABLE_HTTP3
      ));

  // Write PID file when we are ready to accept connection from peer.
  // This makes easier to write restart script for nghttpx.  Because
  // when we know that PID file is recreated, it means we can send
  // QUIT signal to the old process to make it shutdown gracefully.
  if (!config->pid_file.empty()) {
    save_pid();
  }

  shrpx_sd_notifyf(0, "READY=1");

  ev_run(loop, 0);

  ev_timer_stop(loop, &worker_process_grace_period_timer);

  shutdown_worker_process_ready_ipc_watcher(loop);

  // config is now stale if reload has happened.
  if (!get_config()->single_process) {
    shutdown_signal_watchers(loop);
  }

  return 0;
}
} // namespace

namespace {
// Returns true if regular file or symbolic link |path| exists.
bool conf_exists(const char *path) {
  struct stat buf;
  int rv = stat(path, &buf);
  return rv == 0 && (buf.st_mode & (S_IFREG | S_IFLNK));
}
} // namespace

namespace {
constexpr auto DEFAULT_ALPN_LIST = "h2,h2-16,h2-14,http/1.1"_sr;
} // namespace

namespace {
constexpr auto DEFAULT_TLS_MIN_PROTO_VERSION = "TLSv1.2"_sr;
#ifdef TLS1_3_VERSION
constexpr auto DEFAULT_TLS_MAX_PROTO_VERSION = "TLSv1.3"_sr;
#else  // !TLS1_3_VERSION
constexpr auto DEFAULT_TLS_MAX_PROTO_VERSION = "TLSv1.2"_sr;
#endif // !TLS1_3_VERSION
} // namespace

namespace {
constexpr auto DEFAULT_ACCESSLOG_FORMAT =
    R"($remote_addr - - [$time_local] )"
    R"("$request" $status $body_bytes_sent )"
    R"("$http_referer" "$http_user_agent")"_sr;
} // namespace

namespace {
void fill_default_config(Config *config) {
  config->num_worker = 1;
  config->conf_path = "/etc/nghttpx/nghttpx.conf"_sr;
  config->pid = getpid();

#ifdef NOTHREADS
  config->single_thread = true;
#endif // NOTHREADS

  if (ev_supported_backends() & ~ev_recommended_backends() & EVBACKEND_KQUEUE) {
    config->ev_loop_flags = ev_recommended_backends() | EVBACKEND_KQUEUE;
  }

  auto &tlsconf = config->tls;
  {
    auto &ticketconf = tlsconf.ticket;
    {
      auto &memcachedconf = ticketconf.memcached;
      memcachedconf.max_retry = 3;
      memcachedconf.max_fail = 2;
      memcachedconf.interval = 10_min;
      memcachedconf.family = AF_UNSPEC;
    }

    auto &session_cacheconf = tlsconf.session_cache;
    {
      auto &memcachedconf = session_cacheconf.memcached;
      memcachedconf.family = AF_UNSPEC;
    }

    ticketconf.cipher = EVP_aes_128_cbc();
  }

  {
    auto &ocspconf = tlsconf.ocsp;
    // ocsp update interval = 14400 secs = 4 hours, borrowed from h2o
    ocspconf.update_interval = 4_h;
    ocspconf.fetch_ocsp_response_file = PKGDATADIR "/fetch-ocsp-response"_sr;
  }

  {
    auto &dyn_recconf = tlsconf.dyn_rec;
    dyn_recconf.warmup_threshold = 1_m;
    dyn_recconf.idle_timeout = 1_s;
  }

  tlsconf.session_timeout = std::chrono::hours(12);
  tlsconf.ciphers = StringRef{nghttp2::tls::DEFAULT_CIPHER_LIST};
  tlsconf.tls13_ciphers = StringRef{nghttp2::tls::DEFAULT_TLS13_CIPHER_LIST};
  tlsconf.client.ciphers = StringRef{nghttp2::tls::DEFAULT_CIPHER_LIST};
  tlsconf.client.tls13_ciphers =
      StringRef{nghttp2::tls::DEFAULT_TLS13_CIPHER_LIST};
  tlsconf.min_proto_version =
      tls::proto_version_from_string(DEFAULT_TLS_MIN_PROTO_VERSION);
  tlsconf.max_proto_version =
      tls::proto_version_from_string(DEFAULT_TLS_MAX_PROTO_VERSION);
  tlsconf.max_early_data = 16_k;
  tlsconf.ecdh_curves = "X25519:P-256:P-384:P-521"_sr;

  auto &httpconf = config->http;
  httpconf.server_name = "nghttpx"_sr;
  httpconf.no_host_rewrite = true;
  httpconf.request_header_field_buffer = 64_k;
  httpconf.max_request_header_fields = 100;
  httpconf.response_header_field_buffer = 64_k;
  httpconf.max_response_header_fields = 500;
  httpconf.redirect_https_port = "443"_sr;
  httpconf.max_requests = std::numeric_limits<size_t>::max();
  httpconf.xfp.add = true;
  httpconf.xfp.strip_incoming = true;
  httpconf.early_data.strip_incoming = true;
  httpconf.timeout.header = 1_min;

  auto &http2conf = config->http2;
  {
    auto &upstreamconf = http2conf.upstream;

    {
      auto &timeoutconf = upstreamconf.timeout;
      timeoutconf.settings = 10_s;
    }

    // window size for HTTP/2 upstream connection per stream.  2**16-1
    // = 64KiB-1, which is HTTP/2 default.
    upstreamconf.window_size = 64_k - 1;
    // HTTP/2 has connection-level flow control. The default window
    // size for HTTP/2 is 64KiB - 1.
    upstreamconf.connection_window_size = 64_k - 1;
    upstreamconf.max_concurrent_streams = 100;

    upstreamconf.encoder_dynamic_table_size = 4_k;
    upstreamconf.decoder_dynamic_table_size = 4_k;

    nghttp2_option_new(&upstreamconf.option);
    nghttp2_option_set_no_auto_window_update(upstreamconf.option, 1);
    nghttp2_option_set_no_recv_client_magic(upstreamconf.option, 1);
    nghttp2_option_set_max_deflate_dynamic_table_size(
        upstreamconf.option, upstreamconf.encoder_dynamic_table_size);
    nghttp2_option_set_server_fallback_rfc7540_priorities(upstreamconf.option,
                                                          1);
    nghttp2_option_set_builtin_recv_extension_type(upstreamconf.option,
                                                   NGHTTP2_PRIORITY_UPDATE);

    // For API endpoint, we enable automatic window update.  This is
    // because we are a sink.
    nghttp2_option_new(&upstreamconf.alt_mode_option);
    nghttp2_option_set_no_recv_client_magic(upstreamconf.alt_mode_option, 1);
    nghttp2_option_set_max_deflate_dynamic_table_size(
        upstreamconf.alt_mode_option, upstreamconf.encoder_dynamic_table_size);
  }

  http2conf.timeout.stream_write = 1_min;

  {
    auto &downstreamconf = http2conf.downstream;

    {
      auto &timeoutconf = downstreamconf.timeout;
      timeoutconf.settings = 10_s;
    }

    downstreamconf.window_size = 64_k - 1;
    downstreamconf.connection_window_size = (1u << 31) - 1;
    downstreamconf.max_concurrent_streams = 100;

    downstreamconf.encoder_dynamic_table_size = 4_k;
    downstreamconf.decoder_dynamic_table_size = 4_k;

    nghttp2_option_new(&downstreamconf.option);
    nghttp2_option_set_no_auto_window_update(downstreamconf.option, 1);
    nghttp2_option_set_peer_max_concurrent_streams(downstreamconf.option, 100);
    nghttp2_option_set_max_deflate_dynamic_table_size(
        downstreamconf.option, downstreamconf.encoder_dynamic_table_size);
  }

#ifdef ENABLE_HTTP3
  auto &quicconf = config->quic;
  {
    auto &upstreamconf = quicconf.upstream;

    {
      auto &timeoutconf = upstreamconf.timeout;
      timeoutconf.idle = 30_s;
    }

    auto &bpfconf = quicconf.bpf;
    bpfconf.prog_file = PKGLIBDIR "/reuseport_kern.o"_sr;

    upstreamconf.congestion_controller = NGTCP2_CC_ALGO_CUBIC;

    upstreamconf.initial_rtt =
        static_cast<ev_tstamp>(NGTCP2_DEFAULT_INITIAL_RTT) / NGTCP2_SECONDS;
  }

  if (RAND_bytes(reinterpret_cast<unsigned char *>(&quicconf.server_id),
                 sizeof(quicconf.server_id)) != 1) {
    assert(0);
    abort();
  }

  auto &http3conf = config->http3;
  {
    auto &upstreamconf = http3conf.upstream;

    upstreamconf.max_concurrent_streams = 100;
    upstreamconf.window_size = 256_k;
    upstreamconf.connection_window_size = 1_m;
    upstreamconf.max_window_size = 6_m;
    upstreamconf.max_connection_window_size = 8_m;
  }
#endif // ENABLE_HTTP3

  auto &loggingconf = config->logging;
  {
    auto &accessconf = loggingconf.access;
    accessconf.format =
        parse_log_format(config->balloc, DEFAULT_ACCESSLOG_FORMAT);

    auto &errorconf = loggingconf.error;
    errorconf.file = "/dev/stderr"_sr;
  }

  loggingconf.syslog_facility = LOG_DAEMON;
  loggingconf.severity = NOTICE;

  auto &connconf = config->conn;
  {
    auto &listenerconf = connconf.listener;
    {
      // Default accept() backlog
      listenerconf.backlog = 65536;
      listenerconf.timeout.sleep = 30_s;
    }
  }

  {
    auto &upstreamconf = connconf.upstream;
    {
      auto &timeoutconf = upstreamconf.timeout;
      // Idle timeout for HTTP2 upstream connection
      timeoutconf.http2_idle = 3_min;

      // Idle timeout for HTTP3 upstream connection
      timeoutconf.http3_idle = 3_min;

      // Write timeout for HTTP2/non-HTTP2 upstream connection
      timeoutconf.write = 30_s;

      // Keep alive (idle) timeout for HTTP/1 upstream connection
      timeoutconf.idle = 1_min;
    }
  }

  {
    connconf.downstream = std::make_shared<DownstreamConfig>();
    auto &downstreamconf = *connconf.downstream;
    {
      auto &timeoutconf = downstreamconf.timeout;
      // Read/Write timeouts for downstream connection
      timeoutconf.read = 1_min;
      timeoutconf.write = 30_s;
      // Timeout for pooled (idle) connections
      timeoutconf.idle_read = 2_s;
      timeoutconf.connect = 30_s;
      timeoutconf.max_backoff = 120_s;
    }

    downstreamconf.connections_per_host = 8;
    downstreamconf.request_buffer_size = 16_k;
    downstreamconf.response_buffer_size = 128_k;
    downstreamconf.family = AF_UNSPEC;
  }

  auto &apiconf = config->api;
  apiconf.max_request_body = 32_m;

  auto &dnsconf = config->dns;
  {
    auto &timeoutconf = dnsconf.timeout;
    timeoutconf.cache = 10_s;
    timeoutconf.lookup = 5_s;
  }
  dnsconf.max_try = 2;
}

} // namespace

namespace {
void print_version(std::ostream &out) {
  out << "nghttpx nghttp2/" NGHTTP2_VERSION
#ifdef ENABLE_HTTP3
         " ngtcp2/" NGTCP2_VERSION " nghttp3/" NGHTTP3_VERSION
#endif // ENABLE_HTTP3
      << std::endl;
}
} // namespace

namespace {
void print_usage(std::ostream &out) {
  out << R"(Usage: nghttpx [OPTIONS]... [<PRIVATE_KEY> <CERT>]
A reverse proxy for HTTP/3, HTTP/2, and HTTP/1.)"
      << std::endl;
}
} // namespace

namespace {
void print_help(std::ostream &out) {
  auto config = get_config();

  print_usage(out);
  out << R"(
  <PRIVATE_KEY>
              Set  path  to  server's private  key.   Required  unless
              "no-tls" parameter is used in --frontend option.
  <CERT>      Set  path  to  server's  certificate.   Required  unless
              "no-tls"  parameter is  used in  --frontend option.   To
              make OCSP stapling work, this must be an absolute path.

Options:
  The options are categorized into several groups.

Connections:
  -b, --backend=(<HOST>,<PORT>|unix:<PATH>)[;[<PATTERN>[:...]][[;<PARAM>]...]

              Set  backend  host  and   port.   The  multiple  backend
              addresses are  accepted by repeating this  option.  UNIX
              domain socket  can be  specified by prefixing  path name
              with "unix:" (e.g., unix:/var/run/backend.sock).

              Optionally, if <PATTERN>s are given, the backend address
              is  only  used  if  request matches  the  pattern.   The
              pattern  matching is  closely  designed  to ServeMux  in
              net/http package of  Go programming language.  <PATTERN>
              consists of  path, host +  path or just host.   The path
              must start  with "/".  If  it ends with "/",  it matches
              all  request path  in  its subtree.   To  deal with  the
              request  to the  directory without  trailing slash,  the
              path which ends  with "/" also matches  the request path
              which  only  lacks  trailing  '/'  (e.g.,  path  "/foo/"
              matches request path  "/foo").  If it does  not end with
              "/", it  performs exact match against  the request path.
              If  host  is given,  it  performs  a match  against  the
              request host.   For a  request received on  the frontend
              listener with  "sni-fwd" parameter enabled, SNI  host is
              used instead of a request host.  If host alone is given,
              "/" is  appended to it,  so that it matches  all request
              paths  under the  host  (e.g., specifying  "nghttp2.org"
              equals  to "nghttp2.org/").   CONNECT method  is treated
              specially.  It  does not have  path, and we  don't allow
              empty path.  To workaround  this, we assume that CONNECT
              method has "/" as path.

              Patterns with  host take  precedence over  patterns with
              just path.   Then, longer patterns take  precedence over
              shorter ones.

              Host  can  include "*"  in  the  left most  position  to
              indicate  wildcard match  (only suffix  match is  done).
              The "*" must match at least one character.  For example,
              host    pattern    "*.nghttp2.org"    matches    against
              "www.nghttp2.org"  and  "git.ngttp2.org", but  does  not
              match  against  "nghttp2.org".   The exact  hosts  match
              takes precedence over the wildcard hosts match.

              If path  part ends with  "*", it is treated  as wildcard
              path.  The  wildcard path  behaves differently  from the
              normal path.  For normal path,  match is made around the
              boundary of path component  separator,"/".  On the other
              hand, the wildcard  path does not take  into account the
              path component  separator.  All paths which  include the
              wildcard  path  without  last  "*" as  prefix,  and  are
              strictly longer than wildcard  path without last "*" are
              matched.  "*"  must match  at least one  character.  For
              example,  the   pattern  "/foo*"  matches   "/foo/"  and
              "/foobar".  But it does not match "/foo", or "/fo".

              If <PATTERN> is omitted or  empty string, "/" is used as
              pattern,  which  matches  all request  paths  (catch-all
              pattern).  The catch-all backend must be given.

              When doing  a match, nghttpx made  some normalization to
              pattern, request host and path.  For host part, they are
              converted to lower case.  For path part, percent-encoded
              unreserved characters  defined in RFC 3986  are decoded,
              and any  dot-segments (".."  and ".")   are resolved and
              removed.

              For   example,   -b'127.0.0.1,8080;nghttp2.org/httpbin/'
              matches the  request host "nghttp2.org" and  the request
              path "/httpbin/get", but does not match the request host
              "nghttp2.org" and the request path "/index.html".

              The  multiple <PATTERN>s  can  be specified,  delimiting
              them            by           ":".             Specifying
              -b'127.0.0.1,8080;nghttp2.org:www.nghttp2.org'  has  the
              same  effect  to specify  -b'127.0.0.1,8080;nghttp2.org'
              and -b'127.0.0.1,8080;www.nghttp2.org'.

              The backend addresses sharing same <PATTERN> are grouped
              together forming  load balancing  group.

              Several parameters <PARAM> are accepted after <PATTERN>.
              The  parameters are  delimited  by  ";".  The  available
              parameters       are:      "proto=<PROTO>",       "tls",
              "sni=<SNI_HOST>",         "fall=<N>",        "rise=<N>",
              "affinity=<METHOD>",    "dns",    "redirect-if-not-tls",
              "upgrade-scheme",                        "mruby=<PATH>",
              "read-timeout=<DURATION>",   "write-timeout=<DURATION>",
              "group=<GROUP>",  "group-weight=<N>", "weight=<N>",  and
              "dnf".    The  parameter   consists   of  keyword,   and
              optionally followed by "="  and value.  For example, the
              parameter "proto=h2" consists of the keyword "proto" and
              value "h2".  The parameter "tls" consists of the keyword
              "tls"  without value.   Each parameter  is described  as
              follows.

              The backend application protocol  can be specified using
              optional  "proto"   parameter,  and   in  the   form  of
              "proto=<PROTO>".  <PROTO> should be one of the following
              list  without  quotes:  "h2", "http/1.1".   The  default
              value of <PROTO> is  "http/1.1".  Note that usually "h2"
              refers to HTTP/2  over TLS.  But in this  option, it may
              mean HTTP/2  over cleartext TCP unless  "tls" keyword is
              used (see below).

              TLS  can   be  enabled  by  specifying   optional  "tls"
              parameter.  TLS is not enabled by default.

              With "sni=<SNI_HOST>" parameter, it can override the TLS
              SNI  field  value  with  given  <SNI_HOST>.   This  will
              default to the backend <HOST> name

              The  feature  to detect  whether  backend  is online  or
              offline can be enabled  using optional "fall" and "rise"
              parameters.   Using  "fall=<N>"  parameter,  if  nghttpx
              cannot connect  to a  this backend <N>  times in  a row,
              this  backend  is  assumed  to be  offline,  and  it  is
              excluded from load balancing.  If <N> is 0, this backend
              never  be excluded  from load  balancing whatever  times
              nghttpx cannot connect  to it, and this  is the default.
              There is  also "rise=<N>" parameter.  After  backend was
              excluded from load balancing group, nghttpx periodically
              attempts to make a connection to the failed backend, and
              if the  connection is made  successfully <N> times  in a
              row, the backend is assumed to  be online, and it is now
              eligible  for load  balancing target.   If <N>  is 0,  a
              backend  is permanently  offline, once  it goes  in that
              state, and this is the default behaviour.

              The     session     affinity    is     enabled     using
              "affinity=<METHOD>"  parameter.   If  "ip" is  given  in
              <METHOD>, client  IP based session affinity  is enabled.
              If "cookie"  is given in <METHOD>,  cookie based session
              affinity is  enabled.  If  "none" is given  in <METHOD>,
              session affinity  is disabled, and this  is the default.
              The session  affinity is  enabled per <PATTERN>.   If at
              least  one backend  has  "affinity"  parameter, and  its
              <METHOD> is not "none",  session affinity is enabled for
              all backend  servers sharing the same  <PATTERN>.  It is
              advised  to  set  "affinity" parameter  to  all  backend
              explicitly if session affinity  is desired.  The session
              affinity  may   break  if   one  of  the   backend  gets
              unreachable,  or   backend  settings  are   reloaded  or
              replaced by API.

              If   "affinity=cookie"    is   used,    the   additional
              configuration                is                required.
              "affinity-cookie-name=<NAME>" must be  used to specify a
              name     of     cookie      to     use.      Optionally,
              "affinity-cookie-path=<PATH>" can  be used to  specify a
              path   which   cookie    is   applied.    The   optional
              "affinity-cookie-secure=<SECURE>"  controls  the  Secure
              attribute of a cookie.  The default value is "auto", and
              the Secure attribute is  determined by a request scheme.
              If a request scheme is "https", then Secure attribute is
              set.  Otherwise, it  is not set.  If  <SECURE> is "yes",
              the  Secure attribute  is  always set.   If <SECURE>  is
              "no",   the   Secure   attribute  is   always   omitted.
              "affinity-cookie-stickiness=<STICKINESS>"       controls
              stickiness  of   this  affinity.   If   <STICKINESS>  is
              "loose", removing or adding a backend server might break
              the affinity  and the  request might  be forwarded  to a
              different backend server.   If <STICKINESS> is "strict",
              removing the designated  backend server breaks affinity,
              but adding  new backend server does  not cause breakage.
              If  the designated  backend server  becomes unavailable,
              new backend server is chosen  as if the request does not
              have  an  affinity  cookie.   <STICKINESS>  defaults  to
              "loose".

              By default, name resolution of backend host name is done
              at  start  up,  or reloading  configuration.   If  "dns"
              parameter   is  given,   name  resolution   takes  place
              dynamically.  This is useful  if backend address changes
              frequently.   If  "dns"  is given,  name  resolution  of
              backend   host   name   at  start   up,   or   reloading
              configuration is skipped.

              If "redirect-if-not-tls" parameter  is used, the matched
              backend  requires   that  frontend  connection   is  TLS
              encrypted.  If it isn't, nghttpx responds to the request
              with 308  status code, and  https URI the  client should
              use instead  is included in Location  header field.  The
              port number in  redirect URI is 443 by  default, and can
              be  changed using  --redirect-https-port option.   If at
              least one  backend has  "redirect-if-not-tls" parameter,
              this feature is enabled  for all backend servers sharing
              the   same   <PATTERN>.    It    is   advised   to   set
              "redirect-if-no-tls"    parameter   to    all   backends
              explicitly if this feature is desired.

              If "upgrade-scheme"  parameter is used along  with "tls"
              parameter, HTTP/2 :scheme pseudo header field is changed
              to "https" from "http" when forwarding a request to this
              particular backend.  This is  a workaround for a backend
              server  which  requires  "https" :scheme  pseudo  header
              field on TLS encrypted connection.

              "mruby=<PATH>"  parameter  specifies  a  path  to  mruby
              script  file  which  is  invoked when  this  pattern  is
              matched.  All backends which share the same pattern must
              have the same mruby path.

              "read-timeout=<DURATION>" and "write-timeout=<DURATION>"
              parameters  specify the  read and  write timeout  of the
              backend connection  when this  pattern is  matched.  All
              backends which share the same pattern must have the same
              timeouts.  If these timeouts  are entirely omitted for a
              pattern,            --backend-read-timeout           and
              --backend-write-timeout are used.

              "group=<GROUP>"  parameter specifies  the name  of group
              this backend address belongs to.  By default, it belongs
              to  the unnamed  default group.   The name  of group  is
              unique   per   pattern.   "group-weight=<N>"   parameter
              specifies the  weight of  the group.  The  higher weight
              gets  more frequently  selected  by  the load  balancing
              algorithm.  <N> must be  [1, 256] inclusive.  The weight
              8 has 4 times more weight  than 2.  <N> must be the same
              for  all addresses  which  share the  same <GROUP>.   If
              "group-weight" is  omitted in an address,  but the other
              address  which  belongs  to  the  same  group  specifies
              "group-weight",   its    weight   is   used.     If   no
              "group-weight"  is  specified  for  all  addresses,  the
              weight of a group becomes 1.  "group" and "group-weight"
              are ignored if session affinity is enabled.

              "weight=<N>"  parameter  specifies  the  weight  of  the
              backend  address  inside  a  group  which  this  address
              belongs  to.  The  higher  weight  gets more  frequently
              selected by  the load balancing algorithm.   <N> must be
              [1,  256] inclusive.   The  weight 8  has  4 times  more
              weight  than weight  2.  If  this parameter  is omitted,
              weight  becomes  1.   "weight"  is  ignored  if  session
              affinity is enabled.

              If "dnf" parameter is  specified, an incoming request is
              not forwarded to a backend  and just consumed along with
              the  request body  (actually a  backend server  never be
              contacted).  It  is expected  that the HTTP  response is
              generated by mruby  script (see "mruby=<PATH>" parameter
              above).  "dnf" is an abbreviation of "do not forward".

              Since ";" and ":" are  used as delimiter, <PATTERN> must
              not contain  these characters.  In order  to include ":"
              in  <PATTERN>,  one  has  to  specify  "%3A"  (which  is
              percent-encoded  from of  ":") instead.   Since ";"  has
              special  meaning  in shell,  the  option  value must  be
              quoted.

              Default: )"
      << DEFAULT_DOWNSTREAM_HOST << "," << DEFAULT_DOWNSTREAM_PORT << R"(
  -f, --frontend=(<HOST>,<PORT>|unix:<PATH>)[[;<PARAM>]...]
              Set  frontend  host and  port.   If  <HOST> is  '*',  it
              assumes  all addresses  including  both  IPv4 and  IPv6.
              UNIX domain  socket can  be specified by  prefixing path
              name  with  "unix:" (e.g.,  unix:/var/run/nghttpx.sock).
              This  option can  be used  multiple times  to listen  to
              multiple addresses.

              This option  can take  0 or  more parameters,  which are
              described  below.   Note   that  "api"  and  "healthmon"
              parameters are mutually exclusive.

              Optionally, TLS  can be disabled by  specifying "no-tls"
              parameter.  TLS is enabled by default.

              If "sni-fwd" parameter is  used, when performing a match
              to select a backend server,  SNI host name received from
              the client  is used  instead of  the request  host.  See
              --backend option about the pattern match.

              To  make this  frontend as  API endpoint,  specify "api"
              parameter.   This   is  disabled  by  default.    It  is
              important  to  limit the  access  to  the API  frontend.
              Otherwise, someone  may change  the backend  server, and
              break your services,  or expose confidential information
              to the outside the world.

              To  make  this  frontend  as  health  monitor  endpoint,
              specify  "healthmon"  parameter.   This is  disabled  by
              default.  Any  requests which come through  this address
              are replied with 200 HTTP status, without no body.

              To accept  PROXY protocol  version 1  and 2  on frontend
              connection,  specify  "proxyproto" parameter.   This  is
              disabled by default.

              To  receive   HTTP/3  (QUIC)  traffic,   specify  "quic"
              parameter.  It  makes nghttpx listen on  UDP port rather
              than  TCP   port.   UNIX   domain  socket,   "api",  and
              "healthmon"  parameters  cannot   be  used  with  "quic"
              parameter.

              Default: *,3000
  --backlog=<N>
              Set listen backlog size.
              Default: )"
      << config->conn.listener.backlog << R"(
  --backend-address-family=(auto|IPv4|IPv6)
              Specify  address  family  of  backend  connections.   If
              "auto" is given, both IPv4  and IPv6 are considered.  If
              "IPv4" is  given, only  IPv4 address is  considered.  If
              "IPv6" is given, only IPv6 address is considered.
              Default: auto
  --backend-http-proxy-uri=<URI>
              Specify      proxy       URI      in       the      form
              http://[<USER>:<PASS>@]<PROXY>:<PORT>.    If   a   proxy
              requires  authentication,  specify  <USER>  and  <PASS>.
              Note that  they must be properly  percent-encoded.  This
              proxy  is used  when the  backend connection  is HTTP/2.
              First,  make  a CONNECT  request  to  the proxy  and  it
              connects  to the  backend  on behalf  of nghttpx.   This
              forms  tunnel.   After  that, nghttpx  performs  SSL/TLS
              handshake with  the downstream through the  tunnel.  The
              timeouts when connecting and  making CONNECT request can
              be     specified    by     --backend-read-timeout    and
              --backend-write-timeout options.

Performance:
  -n, --workers=<N>
              Set the number of worker threads.
              Default: )"
      << config->num_worker << R"(
  --single-thread
              Run everything in one  thread inside the worker process.
              This   feature   is   provided  for   better   debugging
              experience,  or  for  the platforms  which  lack  thread
              support.   If  threading  is disabled,  this  option  is
              always enabled.
  --read-rate=<SIZE>
              Set maximum  average read  rate on  frontend connection.
              Setting 0 to this option means read rate is unlimited.
              Default: )"
      << config->conn.upstream.ratelimit.read.rate << R"(
  --read-burst=<SIZE>
              Set  maximum read  burst  size  on frontend  connection.
              Setting  0  to this  option  means  read burst  size  is
              unlimited.
              Default: )"
      << config->conn.upstream.ratelimit.read.burst << R"(
  --write-rate=<SIZE>
              Set maximum  average write rate on  frontend connection.
              Setting 0 to this option means write rate is unlimited.
              Default: )"
      << config->conn.upstream.ratelimit.write.rate << R"(
  --write-burst=<SIZE>
              Set  maximum write  burst size  on frontend  connection.
              Setting  0 to  this  option means  write  burst size  is
              unlimited.
              Default: )"
      << config->conn.upstream.ratelimit.write.burst << R"(
  --worker-read-rate=<SIZE>
              Set maximum average read rate on frontend connection per
              worker.  Setting  0 to  this option  means read  rate is
              unlimited.  Not implemented yet.
              Default: 0
  --worker-read-burst=<SIZE>
              Set maximum  read burst size on  frontend connection per
              worker.  Setting 0 to this  option means read burst size
              is unlimited.  Not implemented yet.
              Default: 0
  --worker-write-rate=<SIZE>
              Set maximum  average write  rate on  frontend connection
              per worker.  Setting  0 to this option  means write rate
              is unlimited.  Not implemented yet.
              Default: 0
  --worker-write-burst=<SIZE>
              Set maximum write burst  size on frontend connection per
              worker.  Setting 0 to this option means write burst size
              is unlimited.  Not implemented yet.
              Default: 0
  --worker-frontend-connections=<N>
              Set maximum number  of simultaneous connections frontend
              accepts.  Setting 0 means unlimited.
              Default: )"
      << config->conn.upstream.worker_connections << R"(
  --backend-connections-per-host=<N>
              Set  maximum number  of  backend concurrent  connections
              (and/or  streams in  case  of HTTP/2)  per origin  host.
              This option  is meaningful when --http2-proxy  option is
              used.   The  origin  host  is  determined  by  authority
              portion of  request URI (or :authority  header field for
              HTTP/2).   To  limit  the   number  of  connections  per
              frontend        for       default        mode,       use
              --backend-connections-per-frontend.
              Default: )"
      << config->conn.downstream->connections_per_host << R"(
  --backend-connections-per-frontend=<N>
              Set  maximum number  of  backend concurrent  connections
              (and/or streams  in case of HTTP/2)  per frontend.  This
              option  is   only  used  for  default   mode.   0  means
              unlimited.  To limit the  number of connections per host
              with          --http2-proxy         option,          use
              --backend-connections-per-host.
              Default: )"
      << config->conn.downstream->connections_per_frontend << R"(
  --rlimit-nofile=<N>
              Set maximum number of open files (RLIMIT_NOFILE) to <N>.
              If 0 is given, nghttpx does not set the limit.
              Default: )"
      << config->rlimit_nofile << R"(
  --rlimit-memlock=<N>
              Set maximum number of bytes of memory that may be locked
              into  RAM.  If  0 is  given,  nghttpx does  not set  the
              limit.
              Default: )"
      << config->rlimit_memlock << R"(
  --backend-request-buffer=<SIZE>
              Set buffer size used to store backend request.
              Default: )"
      << util::utos_unit(config->conn.downstream->request_buffer_size) << R"(
  --backend-response-buffer=<SIZE>
              Set buffer size used to store backend response.
              Default: )"
      << util::utos_unit(config->conn.downstream->response_buffer_size) << R"(
  --fastopen=<N>
              Enables  "TCP Fast  Open" for  the listening  socket and
              limits the  maximum length for the  queue of connections
              that have not yet completed the three-way handshake.  If
              value is 0 then fast open is disabled.
              Default: )"
      << config->conn.listener.fastopen << R"(
  --no-kqueue Don't use  kqueue.  This  option is only  applicable for
              the platforms  which have kqueue.  For  other platforms,
              this option will be simply ignored.

Timeout:
  --frontend-http2-idle-timeout=<DURATION>
              Specify idle timeout for HTTP/2 frontend connection.  If
              no active streams exist for this duration, connection is
              closed.
              Default: )"
      << util::duration_str(config->conn.upstream.timeout.http2_idle) << R"(
  --frontend-http3-idle-timeout=<DURATION>
              Specify idle timeout for HTTP/3 frontend connection.  If
              no active streams exist for this duration, connection is
              closed.
              Default: )"
      << util::duration_str(config->conn.upstream.timeout.http3_idle) << R"(
  --frontend-write-timeout=<DURATION>
              Specify write timeout for all frontend connections.
              Default: )"
      << util::duration_str(config->conn.upstream.timeout.write) << R"(
  --frontend-keep-alive-timeout=<DURATION>
              Specify   keep-alive   timeout   for   frontend   HTTP/1
              connection.
              Default: )"
      << util::duration_str(config->conn.upstream.timeout.idle) << R"(
  --frontend-header-timeout=<DURATION>
              Specify  duration  that the  server  waits  for an  HTTP
              request  header fields  to be  received completely.   On
              timeout, HTTP/1 and HTTP/2  connections are closed.  For
              HTTP/3,  the  stream  is shutdown,  and  the  connection
              itself is left intact.
              Default: )"
      << util::duration_str(config->http.timeout.header) << R"(
  --stream-read-timeout=<DURATION>
              Specify  read timeout  for HTTP/2  streams.  0  means no
              timeout.
              Default: )"
      << util::duration_str(config->http2.timeout.stream_read) << R"(
  --stream-write-timeout=<DURATION>
              Specify write  timeout for  HTTP/2 streams.  0  means no
              timeout.
              Default: )"
      << util::duration_str(config->http2.timeout.stream_write) << R"(
  --backend-read-timeout=<DURATION>
              Specify read timeout for backend connection.
              Default: )"
      << util::duration_str(config->conn.downstream->timeout.read) << R"(
  --backend-write-timeout=<DURATION>
              Specify write timeout for backend connection.
              Default: )"
      << util::duration_str(config->conn.downstream->timeout.write) << R"(
  --backend-connect-timeout=<DURATION>
              Specify  timeout before  establishing TCP  connection to
              backend.
              Default: )"
      << util::duration_str(config->conn.downstream->timeout.connect) << R"(
  --backend-keep-alive-timeout=<DURATION>
              Specify   keep-alive   timeout    for   backend   HTTP/1
              connection.
              Default: )"
      << util::duration_str(config->conn.downstream->timeout.idle_read) << R"(
  --listener-disable-timeout=<DURATION>
              After accepting  connection failed,  connection listener
              is disabled  for a given  amount of time.   Specifying 0
              disables this feature.
              Default: )"
      << util::duration_str(config->conn.listener.timeout.sleep) << R"(
  --frontend-http2-setting-timeout=<DURATION>
              Specify  timeout before  SETTINGS ACK  is received  from
              client.
              Default: )"
      << util::duration_str(config->http2.upstream.timeout.settings) << R"(
  --backend-http2-settings-timeout=<DURATION>
              Specify  timeout before  SETTINGS ACK  is received  from
              backend server.
              Default: )"
      << util::duration_str(config->http2.downstream.timeout.settings) << R"(
  --backend-max-backoff=<DURATION>
              Specify  maximum backoff  interval.  This  is used  when
              doing health  check against offline backend  (see "fail"
              parameter  in --backend  option).   It is  also used  to
              limit  the  maximum   interval  to  temporarily  disable
              backend  when nghttpx  failed to  connect to  it.  These
              intervals are calculated  using exponential backoff, and
              consecutive failed attempts increase the interval.  This
              option caps its maximum value.
              Default: )"
      << util::duration_str(config->conn.downstream->timeout.max_backoff) << R"(

SSL/TLS:
  --ciphers=<SUITE>
              Set allowed  cipher list  for frontend  connection.  The
              format of the string is described in OpenSSL ciphers(1).
              This option  sets cipher suites for  TLSv1.2 or earlier.
              Use --tls13-ciphers for TLSv1.3.
              Default: )"
      << config->tls.ciphers << R"(
  --tls13-ciphers=<SUITE>
              Set allowed  cipher list  for frontend  connection.  The
              format of the string is described in OpenSSL ciphers(1).
              This  option  sets  cipher   suites  for  TLSv1.3.   Use
              --ciphers for TLSv1.2 or earlier.
              Default: )"
      << config->tls.tls13_ciphers << R"(
  --client-ciphers=<SUITE>
              Set  allowed cipher  list for  backend connection.   The
              format of the string is described in OpenSSL ciphers(1).
              This option  sets cipher suites for  TLSv1.2 or earlier.
              Use --tls13-client-ciphers for TLSv1.3.
              Default: )"
      << config->tls.client.ciphers << R"(
  --tls13-client-ciphers=<SUITE>
              Set  allowed cipher  list for  backend connection.   The
              format of the string is described in OpenSSL ciphers(1).
              This  option  sets  cipher   suites  for  TLSv1.3.   Use
              --tls13-client-ciphers for TLSv1.2 or earlier.
              Default: )"
      << config->tls.client.tls13_ciphers << R"(
  --ecdh-curves=<LIST>
              Set  supported  curve  list  for  frontend  connections.
              <LIST> is a  colon separated list of curve  NID or names
              in the preference order.  The supported curves depend on
              the  linked  OpenSSL  library.  This  function  requires
              OpenSSL >= 1.0.2.
              Default: )"
      << config->tls.ecdh_curves << R"(
  -k, --insecure
              Don't  verify backend  server's  certificate  if TLS  is
              enabled for backend connections.
  --cacert=<PATH>
              Set path to trusted CA  certificate file.  It is used in
              backend  TLS connections  to verify  peer's certificate.
              It is also used to  verify OCSP response from the script
              set by --fetch-ocsp-response-file.  The  file must be in
              PEM format.   It can contain multiple  certificates.  If
              the  linked OpenSSL  is configured  to load  system wide
              certificates, they  are loaded at startup  regardless of
              this option.
  --private-key-passwd-file=<PATH>
              Path  to file  that contains  password for  the server's
              private key.   If none is  given and the private  key is
              password protected it'll be requested interactively.
  --subcert=<KEYPATH>:<CERTPATH>[[;<PARAM>]...]
              Specify  additional certificate  and  private key  file.
              nghttpx will  choose certificates based on  the hostname
              indicated by client using TLS SNI extension.  If nghttpx
              is  built with  OpenSSL  >= 1.0.2,  the shared  elliptic
              curves (e.g., P-256) between  client and server are also
              taken into  consideration.  This allows nghttpx  to send
              ECDSA certificate  to modern clients, while  sending RSA
              based certificate to older  clients.  This option can be
              used  multiple  times.   To  make  OCSP  stapling  work,
              <CERTPATH> must be absolute path.

              Additional parameter  can be specified in  <PARAM>.  The
              available <PARAM> is "sct-dir=<DIR>".

              "sct-dir=<DIR>"  specifies the  path to  directory which
              contains        *.sct        files        for        TLS
              signed_certificate_timestamp extension (RFC 6962).  This
              feature   requires   OpenSSL   >=   1.0.2.    See   also
              --tls-sct-dir option.
  --dh-param-file=<PATH>
              Path to file that contains  DH parameters in PEM format.
              Without  this   option,  DHE   cipher  suites   are  not
              available.
  --alpn-list=<LIST>
              Comma delimited list of  ALPN protocol identifier sorted
              in the  order of preference.  That  means most desirable
              protocol comes  first.  The parameter must  be delimited
              by a single comma only  and any white spaces are treated
              as a part of protocol string.
              Default: )"
      << DEFAULT_ALPN_LIST
      << R"(
  --verify-client
              Require and verify client certificate.
  --verify-client-cacert=<PATH>
              Path  to file  that contains  CA certificates  to verify
              client certificate.  The file must be in PEM format.  It
              can contain multiple certificates.
  --verify-client-tolerate-expired
              Accept  expired  client  certificate.   Operator  should
              handle  the expired  client  certificate  by some  means
              (e.g.,  mruby  script).   Otherwise, this  option  might
              cause a security risk.
  --client-private-key-file=<PATH>
              Path to  file that contains  client private key  used in
              backend client authentication.
  --client-cert-file=<PATH>
              Path to  file that  contains client certificate  used in
              backend client authentication.
  --tls-min-proto-version=<VER>
              Specify minimum SSL/TLS protocol.   The name matching is
              done in  case-insensitive manner.  The  versions between
              --tls-min-proto-version and  --tls-max-proto-version are
              enabled.  If the protocol list advertised by client does
              not  overlap  this range,  you  will  receive the  error
              message "unknown protocol".  If a protocol version lower
              than TLSv1.2 is specified, make sure that the compatible
              ciphers are  included in --ciphers option.   The default
              cipher  list  only   includes  ciphers  compatible  with
              TLSv1.2 or above.  The available versions are:
              )"
#ifdef TLS1_3_VERSION
         "TLSv1.3, "
#endif // TLS1_3_VERSION
         "TLSv1.2, TLSv1.1, and TLSv1.0"
         R"(
              Default: )"
      << DEFAULT_TLS_MIN_PROTO_VERSION
      << R"(
  --tls-max-proto-version=<VER>
              Specify maximum SSL/TLS protocol.   The name matching is
              done in  case-insensitive manner.  The  versions between
              --tls-min-proto-version and  --tls-max-proto-version are
              enabled.  If the protocol list advertised by client does
              not  overlap  this range,  you  will  receive the  error
              message "unknown protocol".  The available versions are:
              )"
#ifdef TLS1_3_VERSION
         "TLSv1.3, "
#endif // TLS1_3_VERSION
         "TLSv1.2, TLSv1.1, and TLSv1.0"
         R"(
              Default: )"
      << DEFAULT_TLS_MAX_PROTO_VERSION << R"(
  --tls-ticket-key-file=<PATH>
              Path to file that contains  random data to construct TLS
              session ticket  parameters.  If aes-128-cbc is  given in
              --tls-ticket-key-cipher, the  file must  contain exactly
              48    bytes.     If     aes-256-cbc    is    given    in
              --tls-ticket-key-cipher, the  file must  contain exactly
              80  bytes.   This  options  can be  used  repeatedly  to
              specify  multiple ticket  parameters.  If  several files
              are given,  only the  first key is  used to  encrypt TLS
              session  tickets.  Other  keys are  accepted but  server
              will  issue new  session  ticket with  first key.   This
              allows  session  key  rotation.  Please  note  that  key
              rotation  does  not  occur automatically.   User  should
              rearrange  files or  change options  values and  restart
              nghttpx gracefully.   If opening  or reading  given file
              fails, all loaded  keys are discarded and  it is treated
              as if none  of this option is given.  If  this option is
              not given or an error  occurred while opening or reading
              a file,  key is  generated every  1 hour  internally and
              they are  valid for  12 hours.   This is  recommended if
              ticket  key sharing  between  nghttpx  instances is  not
              required.
  --tls-ticket-key-memcached=<HOST>,<PORT>[;tls]
              Specify address  of memcached  server to get  TLS ticket
              keys for  session resumption.   This enables  shared TLS
              ticket key between  multiple nghttpx instances.  nghttpx
              does not set TLS ticket  key to memcached.  The external
              ticket key generator is required.  nghttpx just gets TLS
              ticket  keys  from  memcached, and  use  them,  possibly
              replacing current set  of keys.  It is up  to extern TLS
              ticket  key generator  to rotate  keys frequently.   See
              "TLS SESSION  TICKET RESUMPTION" section in  manual page
              to know the data format in memcached entry.  Optionally,
              memcached  connection  can  be  encrypted  with  TLS  by
              specifying "tls" parameter.
  --tls-ticket-key-memcached-address-family=(auto|IPv4|IPv6)
              Specify address  family of memcached connections  to get
              TLS ticket keys.  If "auto" is given, both IPv4 and IPv6
              are considered.   If "IPv4" is given,  only IPv4 address
              is considered.  If "IPv6" is given, only IPv6 address is
              considered.
              Default: auto
  --tls-ticket-key-memcached-interval=<DURATION>
              Set interval to get TLS ticket keys from memcached.
              Default: )"
      << util::duration_str(config->tls.ticket.memcached.interval) << R"(
  --tls-ticket-key-memcached-max-retry=<N>
              Set  maximum   number  of  consecutive   retries  before
              abandoning TLS ticket key  retrieval.  If this number is
              reached,  the  attempt  is considered  as  failure,  and
              "failure" count  is incremented by 1,  which contributed
              to            the            value            controlled
              --tls-ticket-key-memcached-max-fail option.
              Default: )"
      << config->tls.ticket.memcached.max_retry << R"(
  --tls-ticket-key-memcached-max-fail=<N>
              Set  maximum   number  of  consecutive   failure  before
              disabling TLS ticket until next scheduled key retrieval.
              Default: )"
      << config->tls.ticket.memcached.max_fail << R"(
  --tls-ticket-key-cipher=<CIPHER>
              Specify cipher  to encrypt TLS session  ticket.  Specify
              either   aes-128-cbc   or  aes-256-cbc.    By   default,
              aes-128-cbc is used.
  --tls-ticket-key-memcached-cert-file=<PATH>
              Path to client certificate  for memcached connections to
              get TLS ticket keys.
  --tls-ticket-key-memcached-private-key-file=<PATH>
              Path to client private  key for memcached connections to
              get TLS ticket keys.
  --fetch-ocsp-response-file=<PATH>
              Path to  fetch-ocsp-response script file.  It  should be
              absolute path.
              Default: )"
      << config->tls.ocsp.fetch_ocsp_response_file << R"(
  --ocsp-update-interval=<DURATION>
              Set interval to update OCSP response cache.
              Default: )"
      << util::duration_str(config->tls.ocsp.update_interval) << R"(
  --ocsp-startup
              Start  accepting connections  after initial  attempts to
              get OCSP responses  finish.  It does not  matter some of
              the  attempts  fail.  This  feature  is  useful if  OCSP
              responses   must    be   available    before   accepting
              connections.
  --no-verify-ocsp
              nghttpx does not verify OCSP response.
  --no-ocsp   Disable OCSP stapling.
  --tls-session-cache-memcached=<HOST>,<PORT>[;tls]
              Specify  address of  memcached server  to store  session
              cache.   This  enables   shared  session  cache  between
              multiple   nghttpx  instances.    Optionally,  memcached
              connection can be encrypted with TLS by specifying "tls"
              parameter.
  --tls-session-cache-memcached-address-family=(auto|IPv4|IPv6)
              Specify address family of memcached connections to store
              session cache.  If  "auto" is given, both  IPv4 and IPv6
              are considered.   If "IPv4" is given,  only IPv4 address
              is considered.  If "IPv6" is given, only IPv6 address is
              considered.
              Default: auto
  --tls-session-cache-memcached-cert-file=<PATH>
              Path to client certificate  for memcached connections to
              store session cache.
  --tls-session-cache-memcached-private-key-file=<PATH>
              Path to client private  key for memcached connections to
              store session cache.
  --tls-dyn-rec-warmup-threshold=<SIZE>
              Specify the  threshold size for TLS  dynamic record size
              behaviour.  During  a TLS  session, after  the threshold
              number of bytes  have been written, the  TLS record size
              will be increased to the maximum allowed (16K).  The max
              record size will  continue to be used on  the active TLS
              session.  After  --tls-dyn-rec-idle-timeout has elapsed,
              the record size is reduced  to 1300 bytes.  Specify 0 to
              always use  the maximum record size,  regardless of idle
              period.   This  behaviour  applies   to  all  TLS  based
              frontends, and TLS HTTP/2 backends.
              Default: )"
      << util::utos_unit(config->tls.dyn_rec.warmup_threshold) << R"(
  --tls-dyn-rec-idle-timeout=<DURATION>
              Specify TLS dynamic record  size behaviour timeout.  See
              --tls-dyn-rec-warmup-threshold  for   more  information.
              This behaviour  applies to all TLS  based frontends, and
              TLS HTTP/2 backends.
              Default: )"
      << util::duration_str(config->tls.dyn_rec.idle_timeout) << R"(
  --no-http2-cipher-block-list
              Allow  block  listed  cipher suite  on  frontend  HTTP/2
              connection.                                          See
              https://tools.ietf.org/html/rfc7540#appendix-A  for  the
              complete HTTP/2 cipher suites block list.
  --client-no-http2-cipher-block-list
              Allow  block  listed  cipher  suite  on  backend  HTTP/2
              connection.                                          See
              https://tools.ietf.org/html/rfc7540#appendix-A  for  the
              complete HTTP/2 cipher suites block list.
  --tls-sct-dir=<DIR>
              Specifies the  directory where  *.sct files  exist.  All
              *.sct   files   in  <DIR>   are   read,   and  sent   as
              extension_data of  TLS signed_certificate_timestamp (RFC
              6962)  to  client.   These   *.sct  files  are  for  the
              certificate   specified   in   positional   command-line
              argument <CERT>, or  certificate option in configuration
              file.   For   additional  certificates,   use  --subcert
              option.  This option requires OpenSSL >= 1.0.2.
  --psk-secrets=<PATH>
              Read list of PSK identity and secrets from <PATH>.  This
              is used for frontend connection.  The each line of input
              file  is  formatted  as  <identity>:<hex-secret>,  where
              <identity> is  PSK identity, and <hex-secret>  is secret
              in hex.  An  empty line, and line which  starts with '#'
              are skipped.  The default  enabled cipher list might not
              contain any PSK cipher suite.  In that case, desired PSK
              cipher suites  must be  enabled using  --ciphers option.
              The  desired PSK  cipher suite  may be  block listed  by
              HTTP/2.   To  use  those   cipher  suites  with  HTTP/2,
              consider  to  use  --no-http2-cipher-block-list  option.
              But be aware its implications.
  --client-psk-secrets=<PATH>
              Read PSK identity and secrets from <PATH>.  This is used
              for backend connection.  The each  line of input file is
              formatted  as <identity>:<hex-secret>,  where <identity>
              is PSK identity, and <hex-secret>  is secret in hex.  An
              empty line, and line which  starts with '#' are skipped.
              The first identity and  secret pair encountered is used.
              The default  enabled cipher  list might not  contain any
              PSK  cipher suite.   In  that case,  desired PSK  cipher
              suites  must be  enabled using  --client-ciphers option.
              The  desired PSK  cipher suite  may be  block listed  by
              HTTP/2.   To  use  those   cipher  suites  with  HTTP/2,
              consider   to  use   --client-no-http2-cipher-block-list
              option.  But be aware its implications.
  --tls-no-postpone-early-data
              By  default,   except  for  QUIC   connections,  nghttpx
              postpones forwarding  HTTP requests sent in  early data,
              including  those  sent in  partially  in  it, until  TLS
              handshake  finishes.  If  all backend  server recognizes
              "Early-Data"  header  field,  using  this  option  makes
              nghttpx  not postpone  forwarding request  and get  full
              potential of 0-RTT data.
  --tls-max-early-data=<SIZE>
              Sets  the  maximum  amount  of 0-RTT  data  that  server
              accepts.
              Default: )"
      << util::utos_unit(config->tls.max_early_data) << R"(
  --tls-ktls  Enable   ktls.    For   server,  ktls   is   enable   if
              --tls-session-cache-memcached is not configured.

HTTP/2:
  -c, --frontend-http2-max-concurrent-streams=<N>
              Set the maximum number of  the concurrent streams in one
              frontend HTTP/2 session.
              Default: )"
      << config->http2.upstream.max_concurrent_streams << R"(
  --backend-http2-max-concurrent-streams=<N>
              Set the maximum number of  the concurrent streams in one
              backend  HTTP/2 session.   This sets  maximum number  of
              concurrent opened pushed streams.  The maximum number of
              concurrent requests are set by a remote server.
              Default: )"
      << config->http2.downstream.max_concurrent_streams << R"(
  --frontend-http2-window-size=<SIZE>
              Sets  the  per-stream  initial  window  size  of  HTTP/2
              frontend connection.
              Default: )"
      << config->http2.upstream.window_size << R"(
  --frontend-http2-connection-window-size=<SIZE>
              Sets the  per-connection window size of  HTTP/2 frontend
              connection.
              Default: )"
      << config->http2.upstream.connection_window_size << R"(
  --backend-http2-window-size=<SIZE>
              Sets  the   initial  window   size  of   HTTP/2  backend
              connection.
              Default: )"
      << config->http2.downstream.window_size << R"(
  --backend-http2-connection-window-size=<SIZE>
              Sets the  per-connection window  size of  HTTP/2 backend
              connection.
              Default: )"
      << config->http2.downstream.connection_window_size << R"(
  --http2-no-cookie-crumbling
              Don't crumble cookie header field.
  --padding=<N>
              Add  at most  <N> bytes  to  a HTTP/2  frame payload  as
              padding.  Specify 0 to  disable padding.  This option is
              meant for debugging purpose  and not intended to enhance
              protocol security.
  --no-server-push
              Disable HTTP/2 server push.  Server push is supported by
              default mode and HTTP/2  frontend via Link header field.
              It is  also supported if  both frontend and  backend are
              HTTP/2 in default mode.  In  this case, server push from
              backend session is relayed  to frontend, and server push
              via Link header field is also supported.
  --frontend-http2-optimize-write-buffer-size
              (Experimental) Enable write  buffer size optimization in
              frontend HTTP/2 TLS  connection.  This optimization aims
              to reduce  write buffer  size so  that it  only contains
              bytes  which can  send immediately.   This makes  server
              more responsive to prioritized HTTP/2 stream because the
              buffering  of lower  priority stream  is reduced.   This
              option is only effective on recent Linux platform.
  --frontend-http2-optimize-window-size
              (Experimental)   Automatically  tune   connection  level
              window size of frontend  HTTP/2 TLS connection.  If this
              feature is  enabled, connection window size  starts with
              the   default  window   size,   65535  bytes.    nghttpx
              automatically  adjusts connection  window size  based on
              TCP receiving  window size.  The maximum  window size is
              capped      by      the     value      specified      by
              --frontend-http2-connection-window-size.     Since   the
              stream is subject to stream level window size, it should
              be adjusted using --frontend-http2-window-size option as
              well.   This option  is only  effective on  recent Linux
              platform.
  --frontend-http2-encoder-dynamic-table-size=<SIZE>
              Specify the maximum dynamic  table size of HPACK encoder
              in the frontend HTTP/2 connection.  The decoder (client)
              specifies  the maximum  dynamic table  size it  accepts.
              Then the negotiated dynamic table size is the minimum of
              this option value and the value which client specified.
              Default: )"
      << util::utos_unit(config->http2.upstream.encoder_dynamic_table_size)
      << R"(
  --frontend-http2-decoder-dynamic-table-size=<SIZE>
              Specify the maximum dynamic  table size of HPACK decoder
              in the frontend HTTP/2 connection.
              Default: )"
      << util::utos_unit(config->http2.upstream.decoder_dynamic_table_size)
      << R"(
  --backend-http2-encoder-dynamic-table-size=<SIZE>
              Specify the maximum dynamic  table size of HPACK encoder
              in the backend HTTP/2 connection.  The decoder (backend)
              specifies  the maximum  dynamic table  size it  accepts.
              Then the negotiated dynamic table size is the minimum of
              this option value and the value which backend specified.
              Default: )"
      << util::utos_unit(config->http2.downstream.encoder_dynamic_table_size)
      << R"(
  --backend-http2-decoder-dynamic-table-size=<SIZE>
              Specify the maximum dynamic  table size of HPACK decoder
              in the backend HTTP/2 connection.
              Default: )"
      << util::utos_unit(config->http2.downstream.decoder_dynamic_table_size)
      << R"(

Mode:
  (default mode)
              Accept  HTTP/2,  and  HTTP/1.1 over  SSL/TLS.   "no-tls"
              parameter is  used in  --frontend option,  accept HTTP/2
              and HTTP/1.1 over cleartext  TCP.  The incoming HTTP/1.1
              connection  can  be  upgraded  to  HTTP/2  through  HTTP
              Upgrade.
  -s, --http2-proxy
              Like default mode, but enable forward proxy.  This is so
              called HTTP/2 proxy mode.

Logging:
  -L, --log-level=<LEVEL>
              Set the severity  level of log output.   <LEVEL> must be
              one of INFO, NOTICE, WARN, ERROR and FATAL.
              Default: NOTICE
  --accesslog-file=<PATH>
              Set path to write access log.  To reopen file, send USR1
              signal to nghttpx.
  --accesslog-syslog
              Send  access log  to syslog.   If this  option is  used,
              --accesslog-file option is ignored.
  --accesslog-format=<FORMAT>
              Specify  format  string  for access  log.   The  default
              format is combined format.   The following variables are
              available:

              * $remote_addr: client IP address.
              * $time_local: local time in Common Log format.
              * $time_iso8601: local time in ISO 8601 format.
              * $request: HTTP request line.
              * $status: HTTP response status code.
              * $body_bytes_sent: the  number of bytes sent  to client
                as response body.
              * $http_<VAR>: value of HTTP  request header <VAR> where
                '_' in <VAR> is replaced with '-'.
              * $remote_port: client  port.
              * $server_port: server port.
              * $request_time: request processing time in seconds with
                milliseconds resolution.
              * $pid: PID of the running process.
              * $alpn: ALPN identifier of the protocol which generates
                the response.   For HTTP/1,  ALPN is  always http/1.1,
                regardless of minor version.
              * $tls_cipher: cipher used for SSL/TLS connection.
              * $tls_client_fingerprint_sha256: SHA-256 fingerprint of
                client certificate.
              * $tls_client_fingerprint_sha1:  SHA-1   fingerprint  of
                client certificate.
              * $tls_client_subject_name:   subject  name   in  client
                certificate.
              * $tls_client_issuer_name:   issuer   name   in   client
                certificate.
              * $tls_client_serial:    serial    number   in    client
                certificate.
              * $tls_protocol: protocol for SSL/TLS connection.
              * $tls_session_id: session ID for SSL/TLS connection.
              * $tls_session_reused:  "r"   if  SSL/TLS   session  was
                reused.  Otherwise, "."
              * $tls_sni: SNI server name for SSL/TLS connection.
              * $backend_host:  backend  host   used  to  fulfill  the
                request.  "-" if backend host is not available.
              * $backend_port:  backend  port   used  to  fulfill  the
                request.  "-" if backend host is not available.
              * $method: HTTP method
              * $path:  Request  path  including query.   For  CONNECT
                request, authority is recorded.
              * $path_without_query:  $path   up  to  the   first  '?'
                character.    For   CONNECT  request,   authority   is
                recorded.
              * $protocol_version:   HTTP  version   (e.g.,  HTTP/1.1,
                HTTP/2)

              The  variable  can  be  enclosed  by  "{"  and  "}"  for
              disambiguation (e.g., ${remote_addr}).

              Default: )"
      << DEFAULT_ACCESSLOG_FORMAT << R"(
  --accesslog-write-early
              Write  access  log  when   response  header  fields  are
              received   from  backend   rather   than  when   request
              transaction finishes.
  --errorlog-file=<PATH>
              Set path to write error  log.  To reopen file, send USR1
              signal  to nghttpx.   stderr will  be redirected  to the
              error log file unless --errorlog-syslog is used.
              Default: )"
      << config->logging.error.file << R"(
  --errorlog-syslog
              Send  error log  to  syslog.  If  this  option is  used,
              --errorlog-file option is ignored.
  --syslog-facility=<FACILITY>
              Set syslog facility to <FACILITY>.
              Default: )"
      << str_syslog_facility(config->logging.syslog_facility) << R"(

HTTP:
  --add-x-forwarded-for
              Append  X-Forwarded-For header  field to  the downstream
              request.
  --strip-incoming-x-forwarded-for
              Strip X-Forwarded-For  header field from  inbound client
              requests.
  --no-add-x-forwarded-proto
              Don't append  additional X-Forwarded-Proto  header field
              to  the   backend  request.   If  inbound   client  sets
              X-Forwarded-Proto,                                   and
              --no-strip-incoming-x-forwarded-proto  option  is  used,
              they are passed to the backend.
  --no-strip-incoming-x-forwarded-proto
              Don't strip X-Forwarded-Proto  header field from inbound
              client requests.
  --add-forwarded=<LIST>
              Append RFC  7239 Forwarded header field  with parameters
              specified in comma delimited list <LIST>.  The supported
              parameters  are "by",  "for", "host",  and "proto".   By
              default,  the value  of  "by" and  "for" parameters  are
              obfuscated     string.     See     --forwarded-by    and
              --forwarded-for options respectively.  Note that nghttpx
              does  not  translate non-standard  X-Forwarded-*  header
              fields into Forwarded header field, and vice versa.
  --strip-incoming-forwarded
              Strip  Forwarded   header  field  from   inbound  client
              requests.
  --forwarded-by=(obfuscated|ip|<VALUE>)
              Specify the parameter value sent out with "by" parameter
              of Forwarded  header field.   If "obfuscated"  is given,
              the string is randomly generated at startup.  If "ip" is
              given,   the  interface   address  of   the  connection,
              including port number, is  sent with "by" parameter.  In
              case of UNIX domain  socket, "localhost" is used instead
              of address and  port.  User can also  specify the static
              obfuscated string.  The limitation is that it must start
              with   "_",  and   only   consists   of  character   set
              [A-Za-z0-9._-], as described in RFC 7239.
              Default: obfuscated
  --forwarded-for=(obfuscated|ip)
              Specify  the   parameter  value  sent  out   with  "for"
              parameter of Forwarded header field.  If "obfuscated" is
              given, the string is  randomly generated for each client
              connection.  If "ip" is given, the remote client address
              of  the connection,  without port  number, is  sent with
              "for"  parameter.   In  case   of  UNIX  domain  socket,
              "localhost" is used instead of address.
              Default: obfuscated
  --no-via    Don't append to  Via header field.  If  Via header field
              is received, it is left unaltered.
  --no-strip-incoming-early-data
              Don't strip Early-Data header  field from inbound client
              requests.
  --no-location-rewrite
              Don't  rewrite location  header field  in default  mode.
              When --http2-proxy  is used, location header  field will
              not be altered regardless of this option.
  --host-rewrite
              Rewrite  host and  :authority header  fields in  default
              mode.  When  --http2-proxy is  used, these  headers will
              not be altered regardless of this option.
  --altsvc=<PROTOID,PORT[,HOST,[ORIGIN[,PARAMS]]]>
              Specify   protocol  ID,   port,  host   and  origin   of
              alternative service.  <HOST>,  <ORIGIN> and <PARAMS> are
              optional.   Empty <HOST>  and <ORIGIN>  are allowed  and
              they  are treated  as  nothing is  specified.  They  are
              advertised  in alt-svc  header  field  only in  HTTP/1.1
              frontend.   This option  can be  used multiple  times to
              specify multiple alternative services.
              Example: --altsvc="h2,443,,,ma=3600; persist=1"
  --http2-altsvc=<PROTOID,PORT[,HOST,[ORIGIN[,PARAMS]]]>
              Just like --altsvc option, but  this altsvc is only sent
              in HTTP/2 frontend.
  --add-request-header=<HEADER>
              Specify additional header field to add to request header
              set.   The field  name must  be lowercase.   This option
              just  appends header  field and  won't replace  anything
              already set.  This  option can be used  several times to
              specify multiple header fields.
              Example: --add-request-header="foo: bar"
  --add-response-header=<HEADER>
              Specify  additional  header  field to  add  to  response
              header  set.  The  field name  must be  lowercase.  This
              option  just  appends  header field  and  won't  replace
              anything already  set.  This option can  be used several
              times to specify multiple header fields.
              Example: --add-response-header="foo: bar"
  --request-header-field-buffer=<SIZE>
              Set maximum buffer size for incoming HTTP request header
              field list.  This is the sum of header name and value in
              bytes.   If  trailer  fields  exist,  they  are  counted
              towards this number.
              Default: )"
      << util::utos_unit(config->http.request_header_field_buffer) << R"(
  --max-request-header-fields=<N>
              Set  maximum  number  of incoming  HTTP  request  header
              fields.   If  trailer  fields exist,  they  are  counted
              towards this number.
              Default: )"
      << config->http.max_request_header_fields << R"(
  --response-header-field-buffer=<SIZE>
              Set  maximum  buffer  size for  incoming  HTTP  response
              header field list.   This is the sum of  header name and
              value  in  bytes.  If  trailer  fields  exist, they  are
              counted towards this number.
              Default: )"
      << util::utos_unit(config->http.response_header_field_buffer) << R"(
  --max-response-header-fields=<N>
              Set  maximum number  of  incoming  HTTP response  header
              fields.   If  trailer  fields exist,  they  are  counted
              towards this number.
              Default: )"
      << config->http.max_response_header_fields << R"(
  --error-page=(<CODE>|*)=<PATH>
              Set file path  to custom error page  served when nghttpx
              originally  generates  HTTP  error status  code  <CODE>.
              <CODE> must be greater than or equal to 400, and at most
              599.  If "*"  is used instead of <CODE>,  it matches all
              HTTP  status  code.  If  error  status  code comes  from
              backend server, the custom error pages are not used.
  --server-name=<NAME>
              Change server response header field value to <NAME>.
              Default: )"
      << config->http.server_name << R"(
  --no-server-rewrite
              Don't rewrite server header field in default mode.  When
              --http2-proxy is used, these headers will not be altered
              regardless of this option.
  --redirect-https-port=<PORT>
              Specify the port number which appears in Location header
              field  when  redirect  to  HTTPS  URI  is  made  due  to
              "redirect-if-not-tls" parameter in --backend option.
              Default: )"
      << config->http.redirect_https_port << R"(
  --require-http-scheme
              Always require http or https scheme in HTTP request.  It
              also  requires that  https scheme  must be  used for  an
              encrypted  connection.  Otherwise,  http scheme  must be
              used.   This   option  is   recommended  for   a  server
              deployment which directly faces clients and the services
              it provides only require http or https scheme.

API:
  --api-max-request-body=<SIZE>
              Set the maximum size of request body for API request.
              Default: )"
      << util::utos_unit(config->api.max_request_body) << R"(

DNS:
  --dns-cache-timeout=<DURATION>
              Set duration that cached DNS results remain valid.  Note
              that nghttpx caches the unsuccessful results as well.
              Default: )"
      << util::duration_str(config->dns.timeout.cache) << R"(
  --dns-lookup-timeout=<DURATION>
              Set timeout that  DNS server is given to  respond to the
              initial  DNS  query.  For  the  2nd  and later  queries,
              server is  given time based  on this timeout, and  it is
              scaled linearly.
              Default: )"
      << util::duration_str(config->dns.timeout.lookup) << R"(
  --dns-max-try=<N>
              Set the number of DNS query before nghttpx gives up name
              lookup.
              Default: )"
      << config->dns.max_try << R"(
  --frontend-max-requests=<N>
              The number  of requests that single  frontend connection
              can process.  For HTTP/2, this  is the number of streams
              in  one  HTTP/2 connection.   For  HTTP/1,  this is  the
              number of keep alive requests.  This is hint to nghttpx,
              and it  may allow additional few  requests.  The default
              value is unlimited.

Debug:
  --frontend-http2-dump-request-header=<PATH>
              Dumps request headers received by HTTP/2 frontend to the
              file denoted  in <PATH>.  The  output is done  in HTTP/1
              header field format and each header block is followed by
              an empty line.  This option  is not thread safe and MUST
              NOT be used with option -n<N>, where <N> >= 2.
  --frontend-http2-dump-response-header=<PATH>
              Dumps response headers sent  from HTTP/2 frontend to the
              file denoted  in <PATH>.  The  output is done  in HTTP/1
              header field format and each header block is followed by
              an empty line.  This option  is not thread safe and MUST
              NOT be used with option -n<N>, where <N> >= 2.
  -o, --frontend-frame-debug
              Print HTTP/2 frames in  frontend to stderr.  This option
              is  not thread  safe and  MUST NOT  be used  with option
              -n=N, where N >= 2.

Process:
  -D, --daemon
              Run in a background.  If -D is used, the current working
              directory is changed to '/'.
  --pid-file=<PATH>
              Set path to save PID of this program.
  --user=<USER>
              Run this program as <USER>.   This option is intended to
              be used to drop root privileges.
  --single-process
              Run this program in a  single process mode for debugging
              purpose.  Without this option,  nghttpx creates at least
              2 processes: main and  worker processes.  If this option
              is  used, main  and  worker are  unified  into a  single
              process.   nghttpx still  spawns  additional process  if
              neverbleed  is used.   In the  single process  mode, the
              signal handling feature is disabled.
  --max-worker-processes=<N>
              The maximum number of  worker processes.  nghttpx spawns
              new worker  process when  it reloads  its configuration.
              The previous worker  process enters graceful termination
              period and will terminate  when it finishes handling the
              existing    connections.     However,    if    reloading
              configurations  happen   very  frequently,   the  worker
              processes might be piled up if they take a bit long time
              to finish  the existing connections.  With  this option,
              if  the number  of  worker processes  exceeds the  given
              value,   the  oldest   worker   process  is   terminated
              immediately.  Specifying 0 means no  limit and it is the
              default behaviour.
  --worker-process-grace-shutdown-period=<DURATION>
              Maximum  period  for  a   worker  process  to  terminate
              gracefully.  When  a worker  process enters  in graceful
              shutdown   period  (e.g.,   when  nghttpx   reloads  its
              configuration)  and  it  does not  finish  handling  the
              existing connections in the given  period of time, it is
              immediately terminated.  Specifying 0 means no limit and
              it is the default behaviour.

Scripting:
  --mruby-file=<PATH>
              Set mruby script file
  --ignore-per-pattern-mruby-error
              Ignore mruby compile error  for per-pattern mruby script
              file.  If error  occurred, it is treated as  if no mruby
              file were specified for the pattern.
)";

#ifdef ENABLE_HTTP3
  out << R"(
HTTP/3 and QUIC:
  --frontend-quic-idle-timeout=<DURATION>
              Specify an idle timeout for QUIC connection.
              Default: )"
      << util::duration_str(config->quic.upstream.timeout.idle) << R"(
  --frontend-quic-debug-log
              Output QUIC debug log to /dev/stderr.
  --quic-bpf-program-file=<PATH>
              Specify a path to  eBPF program file reuseport_kern.o to
              direct  an  incoming  QUIC  UDP datagram  to  a  correct
              socket.
              Default: )"
      << config->quic.bpf.prog_file << R"(
  --frontend-quic-early-data
              Enable early data on frontend QUIC connections.  nghttpx
              sends "Early-Data" header field to a backend server if a
              request is received in early  data and handshake has not
              finished.  All backend servers should deal with possibly
              replayed requests.
  --frontend-quic-qlog-dir=<DIR>
              Specify a  directory where  a qlog  file is  written for
              frontend QUIC  connections.  A qlog file  is created per
              each QUIC  connection.  The  file name is  ISO8601 basic
              format, followed by "-", server Source Connection ID and
              ".sqlog".
  --frontend-quic-require-token
              Require an address validation  token for a frontend QUIC
              connection.   Server sends  a token  in Retry  packet or
              NEW_TOKEN frame in the previous connection.
  --frontend-quic-congestion-controller=<CC>
              Specify a congestion controller algorithm for a frontend
              QUIC  connection.   <CC>  should be  either  "cubic"  or
              "bbr".
              Default: )"
      << (config->quic.upstream.congestion_controller == NGTCP2_CC_ALGO_CUBIC
              ? "cubic"
              : "bbr")
      << R"(
  --frontend-quic-secret-file=<PATH>
              Path to file that contains secure random data to be used
              as QUIC keying materials.  It is used to derive keys for
              encrypting tokens and Connection IDs.  It is not used to
              encrypt  QUIC  packets.  Each  line  of  this file  must
              contain  exactly  136  bytes  hex-encoded  string  (when
              decoded the byte string is  68 bytes long).  The first 3
              bits of  decoded byte  string are  used to  identify the
              keying material.  An  empty line or a  line which starts
              '#'  is ignored.   The file  can contain  more than  one
              keying materials.  Because the  identifier is 3 bits, at
              most 8 keying materials are  read and the remaining data
              is discarded.  The first keying  material in the file is
              primarily  used for  encryption and  decryption for  new
              connection.  The other ones are used to decrypt data for
              the  existing connections.   Specifying multiple  keying
              materials enables  key rotation.   Please note  that key
              rotation  does  not  occur automatically.   User  should
              update  files  or  change  options  values  and  restart
              nghttpx gracefully.   If opening  or reading  given file
              fails, all loaded keying  materials are discarded and it
              is treated as if none of  this option is given.  If this
              option is not  given or an error  occurred while opening
              or  reading  a  file,  a keying  material  is  generated
              internally on startup and reload.
  --quic-server-id=<HEXSTRING>
              Specify server  ID encoded in Connection  ID to identify
              this  particular  server  instance.   Connection  ID  is
              encrypted and  this part is  not visible in  public.  It
              must be 4  bytes long and must be encoded  in hex string
              (which is 8  bytes long).  If this option  is omitted, a
              random   server  ID   is   generated   on  startup   and
              configuration reload.
  --frontend-quic-initial-rtt=<DURATION>
              Specify the initial RTT of the frontend QUIC connection.
              Default: )"
      << util::duration_str(config->quic.upstream.initial_rtt) << R"(
  --no-quic-bpf
              Disable eBPF.
  --frontend-http3-window-size=<SIZE>
              Sets  the  per-stream  initial  window  size  of  HTTP/3
              frontend connection.
              Default: )"
      << util::utos_unit(config->http3.upstream.window_size) << R"(
  --frontend-http3-connection-window-size=<SIZE>
              Sets the  per-connection window size of  HTTP/3 frontend
              connection.
              Default: )"
      << util::utos_unit(config->http3.upstream.connection_window_size) << R"(
  --frontend-http3-max-window-size=<SIZE>
              Sets  the  maximum  per-stream  window  size  of  HTTP/3
              frontend connection.  The window  size is adjusted based
              on the receiving rate of stream data.  The initial value
              is the  value specified  by --frontend-http3-window-size
              and the window size grows up to <SIZE> bytes.
              Default: )"
      << util::utos_unit(config->http3.upstream.max_window_size) << R"(
  --frontend-http3-max-connection-window-size=<SIZE>
              Sets the  maximum per-connection  window size  of HTTP/3
              frontend connection.  The window  size is adjusted based
              on the receiving rate of stream data.  The initial value
              is         the         value        specified         by
              --frontend-http3-connection-window-size  and the  window
              size grows up to <SIZE> bytes.
              Default: )"
      << util::utos_unit(config->http3.upstream.max_connection_window_size)
      << R"(
  --frontend-http3-max-concurrent-streams=<N>
              Set the maximum number of  the concurrent streams in one
              frontend HTTP/3 connection.
              Default: )"
      << config->http3.upstream.max_concurrent_streams << R"(
)";
#endif // ENABLE_HTTP3

  out << R"(
Misc:
  --conf=<PATH>
              Load  configuration  from   <PATH>.   Please  note  that
              nghttpx always  tries to read the  default configuration
              file if --conf is not given.
              Default: )"
      << config->conf_path << R"(
  --include=<PATH>
              Load additional configurations from <PATH>.  File <PATH>
              is  read  when  configuration  parser  encountered  this
              option.  This option can be used multiple times, or even
              recursively.
  -v, --version
              Print version and exit.
  -h, --help  Print this help and exit.

--

  The <SIZE> argument is an integer and an optional unit (e.g., 10K is
  10 * 1024).  Units are K, M and G (powers of 1024).

  The <DURATION> argument is an integer and an optional unit (e.g., 1s
  is 1 second and 500ms is 500 milliseconds).  Units are h, m, s or ms
  (hours, minutes, seconds and milliseconds, respectively).  If a unit
  is omitted, a second is used as unit.)"
      << std::endl;
}
} // namespace

namespace {
int process_options(Config *config,
                    std::vector<std::pair<StringRef, StringRef>> &cmdcfgs) {
  std::array<char, STRERROR_BUFSIZE> errbuf;
  std::map<StringRef, size_t> pattern_addr_indexer;
  if (conf_exists(config->conf_path.data())) {
    LOG(NOTICE) << "Loading configuration from " << config->conf_path;
    std::set<StringRef> include_set;
    if (load_config(config, config->conf_path.data(), include_set,
                    pattern_addr_indexer) == -1) {
      LOG(FATAL) << "Failed to load configuration from " << config->conf_path;
      return -1;
    }
    assert(include_set.empty());
  }

  // Reopen log files using configurations in file
  reopen_log_files(config->logging);

  {
    std::set<StringRef> include_set;

    for (auto &p : cmdcfgs) {
      if (parse_config(config, p.first, p.second, include_set,
                       pattern_addr_indexer) == -1) {
        LOG(FATAL) << "Failed to parse command-line argument.";
        return -1;
      }
    }

    assert(include_set.empty());
  }

  Log::set_severity_level(config->logging.severity);

  auto &loggingconf = config->logging;

  if (loggingconf.access.syslog || loggingconf.error.syslog) {
    openlog("nghttpx", LOG_NDELAY | LOG_NOWAIT | LOG_PID,
            loggingconf.syslog_facility);
  }

  if (reopen_log_files(config->logging) != 0) {
    LOG(FATAL) << "Failed to open log file";
    return -1;
  }

  redirect_stderr_to_errorlog(loggingconf);

  if (config->uid != 0) {
    if (log_config()->accesslog_fd != -1 &&
        fchown(log_config()->accesslog_fd, config->uid, config->gid) == -1) {
      auto error = errno;
      LOG(WARN) << "Changing owner of access log file failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
    if (log_config()->errorlog_fd != -1 &&
        fchown(log_config()->errorlog_fd, config->uid, config->gid) == -1) {
      auto error = errno;
      LOG(WARN) << "Changing owner of error log file failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
  }

  if (config->single_thread) {
    LOG(WARN) << "single-thread: Set workers to 1";
    config->num_worker = 1;
  }

  auto &http2conf = config->http2;
  {
    auto &dumpconf = http2conf.upstream.debug.dump;

    if (!dumpconf.request_header_file.empty()) {
      auto path = dumpconf.request_header_file.data();
      auto f = open_file_for_write(path);

      if (f == nullptr) {
        LOG(FATAL) << "Failed to open http2 upstream request header file: "
                   << path;
        return -1;
      }

      dumpconf.request_header = f;

      if (config->uid != 0) {
        if (chown(path, config->uid, config->gid) == -1) {
          auto error = errno;
          LOG(WARN) << "Changing owner of http2 upstream request header file "
                    << path << " failed: "
                    << xsi_strerror(error, errbuf.data(), errbuf.size());
        }
      }
    }

    if (!dumpconf.response_header_file.empty()) {
      auto path = dumpconf.response_header_file.data();
      auto f = open_file_for_write(path);

      if (f == nullptr) {
        LOG(FATAL) << "Failed to open http2 upstream response header file: "
                   << path;
        return -1;
      }

      dumpconf.response_header = f;

      if (config->uid != 0) {
        if (chown(path, config->uid, config->gid) == -1) {
          auto error = errno;
          LOG(WARN) << "Changing owner of http2 upstream response header file"
                    << " " << path << " failed: "
                    << xsi_strerror(error, errbuf.data(), errbuf.size());
        }
      }
    }
  }

  auto &tlsconf = config->tls;

  if (tlsconf.alpn_list.empty()) {
    tlsconf.alpn_list = util::split_str(DEFAULT_ALPN_LIST, ',');
  }

  if (!tlsconf.tls_proto_list.empty()) {
    tlsconf.tls_proto_mask = tls::create_tls_proto_mask(tlsconf.tls_proto_list);
  }

  // TODO We depends on the ordering of protocol version macro in
  // OpenSSL.
  if (tlsconf.min_proto_version > tlsconf.max_proto_version) {
    LOG(ERROR) << "tls-max-proto-version must be equal to or larger than "
                  "tls-min-proto-version";
    return -1;
  }

  if (tls::set_alpn_prefs(tlsconf.alpn_prefs, tlsconf.alpn_list) != 0) {
    return -1;
  }

  tlsconf.bio_method = create_bio_method();

  auto &listenerconf = config->conn.listener;
  auto &upstreamconf = config->conn.upstream;

  if (listenerconf.addrs.empty()) {
    UpstreamAddr addr{};
    addr.host = "*"_sr;
    addr.port = 3000;
    addr.tls = true;
    addr.family = AF_INET;
    addr.index = 0;
    listenerconf.addrs.push_back(addr);
    addr.family = AF_INET6;
    addr.index = 1;
    listenerconf.addrs.push_back(std::move(addr));
  }

  if (upstreamconf.worker_connections == 0) {
    upstreamconf.worker_connections = std::numeric_limits<size_t>::max();
  }

  if (tls::upstream_tls_enabled(config->conn) &&
      (tlsconf.private_key_file.empty() || tlsconf.cert_file.empty())) {
    LOG(FATAL) << "TLS private key and certificate files are required.  "
                  "Specify them in command-line, or in configuration file "
                  "using private-key-file and certificate-file options.";
    return -1;
  }

  if (tls::upstream_tls_enabled(config->conn) && !tlsconf.ocsp.disabled) {
    struct stat buf;
    if (stat(tlsconf.ocsp.fetch_ocsp_response_file.data(), &buf) != 0) {
      tlsconf.ocsp.disabled = true;
      LOG(WARN) << "--fetch-ocsp-response-file: "
                << tlsconf.ocsp.fetch_ocsp_response_file
                << " not found.  OCSP stapling has been disabled.";
    }
  }

  if (configure_downstream_group(config, config->http2_proxy, false, tlsconf) !=
      0) {
    return -1;
  }

  std::array<char, util::max_hostport> hostport_buf;

  auto &proxy = config->downstream_http_proxy;
  if (!proxy.host.empty()) {
    auto hostport = util::make_hostport(std::begin(hostport_buf),
                                        StringRef{proxy.host}, proxy.port);
    if (resolve_hostname(&proxy.addr, proxy.host.data(), proxy.port,
                         AF_UNSPEC) == -1) {
      LOG(FATAL) << "Resolving backend HTTP proxy address failed: " << hostport;
      return -1;
    }
    LOG(NOTICE) << "Backend HTTP proxy address: " << hostport << " -> "
                << util::to_numeric_addr(&proxy.addr);
  }

  {
    auto &memcachedconf = tlsconf.session_cache.memcached;
    if (!memcachedconf.host.empty()) {
      auto hostport = util::make_hostport(std::begin(hostport_buf),
                                          StringRef{memcachedconf.host},
                                          memcachedconf.port);
      if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.data(),
                           memcachedconf.port, memcachedconf.family) == -1) {
        LOG(FATAL)
            << "Resolving memcached address for TLS session cache failed: "
            << hostport;
        return -1;
      }
      LOG(NOTICE) << "Memcached address for TLS session cache: " << hostport
                  << " -> " << util::to_numeric_addr(&memcachedconf.addr);
      if (memcachedconf.tls) {
        LOG(NOTICE) << "Connection to memcached for TLS session cache will be "
                       "encrypted by TLS";
      }
    }
  }

  {
    auto &memcachedconf = tlsconf.ticket.memcached;
    if (!memcachedconf.host.empty()) {
      auto hostport = util::make_hostport(std::begin(hostport_buf),
                                          StringRef{memcachedconf.host},
                                          memcachedconf.port);
      if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.data(),
                           memcachedconf.port, memcachedconf.family) == -1) {
        LOG(FATAL) << "Resolving memcached address for TLS ticket key failed: "
                   << hostport;
        return -1;
      }
      LOG(NOTICE) << "Memcached address for TLS ticket key: " << hostport
                  << " -> " << util::to_numeric_addr(&memcachedconf.addr);
      if (memcachedconf.tls) {
        LOG(NOTICE) << "Connection to memcached for TLS ticket key will be "
                       "encrypted by TLS";
      }
    }
  }

  if (config->rlimit_nofile) {
    struct rlimit lim = {static_cast<rlim_t>(config->rlimit_nofile),
                         static_cast<rlim_t>(config->rlimit_nofile)};
    if (setrlimit(RLIMIT_NOFILE, &lim) != 0) {
      auto error = errno;
      LOG(WARN) << "Setting rlimit-nofile failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
  }

#ifdef RLIMIT_MEMLOCK
  if (config->rlimit_memlock) {
    struct rlimit lim = {static_cast<rlim_t>(config->rlimit_memlock),
                         static_cast<rlim_t>(config->rlimit_memlock)};
    if (setrlimit(RLIMIT_MEMLOCK, &lim) != 0) {
      auto error = errno;
      LOG(WARN) << "Setting rlimit-memlock failed: "
                << xsi_strerror(error, errbuf.data(), errbuf.size());
    }
  }
#endif // RLIMIT_MEMLOCK

  auto &fwdconf = config->http.forwarded;

  if (fwdconf.by_node_type == ForwardedNode::OBFUSCATED &&
      fwdconf.by_obfuscated.empty()) {
    // 2 for '_' and terminal NULL
    auto iov = make_byte_ref(config->balloc, SHRPX_OBFUSCATED_NODE_LENGTH + 2);
    auto p = std::begin(iov);
    *p++ = '_';
    auto gen = util::make_mt19937();
    p = util::random_alpha_digit(p, p + SHRPX_OBFUSCATED_NODE_LENGTH, gen);
    *p = '\0';
    fwdconf.by_obfuscated = StringRef{std::span{std::begin(iov), p}};
  }

  if (config->http2.upstream.debug.frame_debug) {
    // To make it sync to logging
    set_output(stderr);
    if (isatty(fileno(stdout))) {
      set_color_output(true);
    }
    reset_timer();
  }

  config->http2.upstream.callbacks = create_http2_upstream_callbacks();
  config->http2.downstream.callbacks = create_http2_downstream_callbacks();

  if (!config->http.altsvcs.empty()) {
    config->http.altsvc_header_value =
        http::create_altsvc_header_value(config->balloc, config->http.altsvcs);
  }

  if (!config->http.http2_altsvcs.empty()) {
    config->http.http2_altsvc_header_value = http::create_altsvc_header_value(
        config->balloc, config->http.http2_altsvcs);
  }

  return 0;
}
} // namespace

namespace {
// Closes file descriptor which are opened for listeners in config,
// and are not inherited from |iaddrs|.
void close_not_inherited_fd(Config *config,
                            const std::vector<InheritedAddr> &iaddrs) {
  auto &listenerconf = config->conn.listener;

  for (auto &addr : listenerconf.addrs) {
    auto inherited = std::find_if(
        std::begin(iaddrs), std::end(iaddrs),
        [&addr](const InheritedAddr &iaddr) { return addr.fd == iaddr.fd; });

    if (inherited != std::end(iaddrs)) {
      continue;
    }

    close(addr.fd);
  }
}
} // namespace

namespace {
void reload_config() {
  int rv;

  LOG(NOTICE) << "Reloading configuration";

  auto cur_config = mod_config();
  auto new_config = std::make_unique<Config>();

  fill_default_config(new_config.get());

  new_config->conf_path =
      make_string_ref(new_config->balloc, cur_config->conf_path);
  // daemon option is ignored here.
  new_config->daemon = cur_config->daemon;
  // loop is reused, and ev_loop_flags gets ignored
  new_config->ev_loop_flags = cur_config->ev_loop_flags;
  new_config->config_revision = cur_config->config_revision + 1;

  rv = process_options(new_config.get(), suconfig.cmdcfgs);
  if (rv != 0) {
    LOG(ERROR) << "Failed to process new configuration";
    return;
  }

  auto iaddrs = get_inherited_addr_from_config(new_config->balloc, cur_config);

  if (create_acceptor_socket(new_config.get(), iaddrs) != 0) {
    close_not_inherited_fd(new_config.get(), iaddrs);
    return;
  }

  // According to libev documentation, flags are ignored since we have
  // already created first default loop.
  auto loop = ev_default_loop(new_config->ev_loop_flags);

  int ipc_fd = 0;
#ifdef ENABLE_HTTP3
  int quic_ipc_fd = 0;

  auto quic_lwps = collect_quic_lingering_worker_processes();

  std::vector<WorkerID> worker_ids;

  if (generate_worker_id(worker_ids, worker_process_seq, new_config.get()) !=
      0) {
    close_not_inherited_fd(new_config.get(), iaddrs);
    return;
  }
#endif // ENABLE_HTTP3

  // fork_worker_process and forked child process assumes new
  // configuration can be obtained from get_config().

  auto old_config = replace_config(std::move(new_config));

  auto pid = fork_worker_process(ipc_fd
#ifdef ENABLE_HTTP3
                                 ,
                                 quic_ipc_fd
#endif // ENABLE_HTTP3

                                 ,
                                 iaddrs
#ifdef ENABLE_HTTP3
                                 ,
                                 worker_ids, std::move(quic_lwps)
#endif // ENABLE_HTTP3
  );

  if (pid == -1) {
    LOG(ERROR) << "Failed to process new configuration";

    new_config = replace_config(std::move(old_config));
    close_not_inherited_fd(new_config.get(), iaddrs);

    return;
  }

  close_unused_inherited_addr(iaddrs);

  worker_process_add(std::make_unique<WorkerProcess>(
      loop, pid, ipc_fd
#ifdef ENABLE_HTTP3
      ,
      quic_ipc_fd, std::move(worker_ids), worker_process_seq++
#endif // ENABLE_HTTP3
      ));

  worker_process_adjust_limit();

  if (!get_config()->pid_file.empty()) {
    save_pid();
  }
}
} // namespace

int main(int argc, char **argv) {
  int rv;
  std::array<char, STRERROR_BUFSIZE> errbuf;

#ifdef HAVE_LIBBPF
  libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
#endif // HAVE_LIBBPF

  Log::set_severity_level(NOTICE);
  create_config();
  fill_default_config(mod_config());

  // make copy of stderr
  store_original_fds();

  // First open log files with default configuration, so that we can
  // log errors/warnings while reading configuration files.
  reopen_log_files(get_config()->logging);

  suconfig.original_argv = argv;

  // We have to copy argv, since getopt_long may change its content.
  suconfig.argc = argc;
  suconfig.argv = new char *[argc];

  for (int i = 0; i < argc; ++i) {
    suconfig.argv[i] = strdup(argv[i]);
    if (suconfig.argv[i] == nullptr) {
      auto error = errno;
      LOG(FATAL) << "failed to copy argv: "
                 << xsi_strerror(error, errbuf.data(), errbuf.size());
      exit(EXIT_FAILURE);
    }
  }

  suconfig.cwd = getcwd(nullptr, 0);
  if (suconfig.cwd == nullptr) {
    auto error = errno;
    LOG(FATAL) << "failed to get current working directory: errno=" << error;
    exit(EXIT_FAILURE);
  }

  auto &cmdcfgs = suconfig.cmdcfgs;

  while (1) {
    static int flag = 0;
    static constexpr option long_options[] = {
        {SHRPX_OPT_DAEMON.data(), no_argument, nullptr, 'D'},
        {SHRPX_OPT_LOG_LEVEL.data(), required_argument, nullptr, 'L'},
        {SHRPX_OPT_BACKEND.data(), required_argument, nullptr, 'b'},
        {SHRPX_OPT_HTTP2_MAX_CONCURRENT_STREAMS.data(), required_argument,
         nullptr, 'c'},
        {SHRPX_OPT_FRONTEND.data(), required_argument, nullptr, 'f'},
        {"help", no_argument, nullptr, 'h'},
        {SHRPX_OPT_INSECURE.data(), no_argument, nullptr, 'k'},
        {SHRPX_OPT_WORKERS.data(), required_argument, nullptr, 'n'},
        {SHRPX_OPT_CLIENT_PROXY.data(), no_argument, nullptr, 'p'},
        {SHRPX_OPT_HTTP2_PROXY.data(), no_argument, nullptr, 's'},
        {"version", no_argument, nullptr, 'v'},
        {SHRPX_OPT_FRONTEND_FRAME_DEBUG.data(), no_argument, nullptr, 'o'},
        {SHRPX_OPT_ADD_X_FORWARDED_FOR.data(), no_argument, &flag, 1},
        {SHRPX_OPT_FRONTEND_HTTP2_READ_TIMEOUT.data(), required_argument, &flag,
         2},
        {SHRPX_OPT_FRONTEND_READ_TIMEOUT.data(), required_argument, &flag, 3},
        {SHRPX_OPT_FRONTEND_WRITE_TIMEOUT.data(), required_argument, &flag, 4},
        {SHRPX_OPT_BACKEND_READ_TIMEOUT.data(), required_argument, &flag, 5},
        {SHRPX_OPT_BACKEND_WRITE_TIMEOUT.data(), required_argument, &flag, 6},
        {SHRPX_OPT_ACCESSLOG_FILE.data(), required_argument, &flag, 7},
        {SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT.data(), required_argument, &flag,
         8},
        {SHRPX_OPT_FRONTEND_HTTP2_WINDOW_BITS.data(), required_argument, &flag,
         9},
        {SHRPX_OPT_PID_FILE.data(), required_argument, &flag, 10},
        {SHRPX_OPT_USER.data(), required_argument, &flag, 11},
        {"conf", required_argument, &flag, 12},
        {SHRPX_OPT_SYSLOG_FACILITY.data(), required_argument, &flag, 14},
        {SHRPX_OPT_BACKLOG.data(), required_argument, &flag, 15},
        {SHRPX_OPT_CIPHERS.data(), required_argument, &flag, 16},
        {SHRPX_OPT_CLIENT.data(), no_argument, &flag, 17},
        {SHRPX_OPT_BACKEND_HTTP2_WINDOW_BITS.data(), required_argument, &flag,
         18},
        {SHRPX_OPT_CACERT.data(), required_argument, &flag, 19},
        {SHRPX_OPT_BACKEND_IPV4.data(), no_argument, &flag, 20},
        {SHRPX_OPT_BACKEND_IPV6.data(), no_argument, &flag, 21},
        {SHRPX_OPT_PRIVATE_KEY_PASSWD_FILE.data(), required_argument, &flag,
         22},
        {SHRPX_OPT_NO_VIA.data(), no_argument, &flag, 23},
        {SHRPX_OPT_SUBCERT.data(), required_argument, &flag, 24},
        {SHRPX_OPT_HTTP2_BRIDGE.data(), no_argument, &flag, 25},
        {SHRPX_OPT_BACKEND_HTTP_PROXY_URI.data(), required_argument, &flag, 26},
        {SHRPX_OPT_BACKEND_NO_TLS.data(), no_argument, &flag, 27},
        {SHRPX_OPT_OCSP_STARTUP.data(), no_argument, &flag, 28},
        {SHRPX_OPT_FRONTEND_NO_TLS.data(), no_argument, &flag, 29},
        {SHRPX_OPT_NO_VERIFY_OCSP.data(), no_argument, &flag, 30},
        {SHRPX_OPT_BACKEND_TLS_SNI_FIELD.data(), required_argument, &flag, 31},
        {SHRPX_OPT_DH_PARAM_FILE.data(), required_argument, &flag, 33},
        {SHRPX_OPT_READ_RATE.data(), required_argument, &flag, 34},
        {SHRPX_OPT_READ_BURST.data(), required_argument, &flag, 35},
        {SHRPX_OPT_WRITE_RATE.data(), required_argument, &flag, 36},
        {SHRPX_OPT_WRITE_BURST.data(), required_argument, &flag, 37},
        {SHRPX_OPT_NPN_LIST.data(), required_argument, &flag, 38},
        {SHRPX_OPT_VERIFY_CLIENT.data(), no_argument, &flag, 39},
        {SHRPX_OPT_VERIFY_CLIENT_CACERT.data(), required_argument, &flag, 40},
        {SHRPX_OPT_CLIENT_PRIVATE_KEY_FILE.data(), required_argument, &flag,
         41},
        {SHRPX_OPT_CLIENT_CERT_FILE.data(), required_argument, &flag, 42},
        {SHRPX_OPT_FRONTEND_HTTP2_DUMP_REQUEST_HEADER.data(), required_argument,
         &flag, 43},
        {SHRPX_OPT_FRONTEND_HTTP2_DUMP_RESPONSE_HEADER.data(),
         required_argument, &flag, 44},
        {SHRPX_OPT_HTTP2_NO_COOKIE_CRUMBLING.data(), no_argument, &flag, 45},
        {SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_BITS.data(),
         required_argument, &flag, 46},
        {SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_BITS.data(),
         required_argument, &flag, 47},
        {SHRPX_OPT_TLS_PROTO_LIST.data(), required_argument, &flag, 48},
        {SHRPX_OPT_PADDING.data(), required_argument, &flag, 49},
        {SHRPX_OPT_WORKER_READ_RATE.data(), required_argument, &flag, 50},
        {SHRPX_OPT_WORKER_READ_BURST.data(), required_argument, &flag, 51},
        {SHRPX_OPT_WORKER_WRITE_RATE.data(), required_argument, &flag, 52},
        {SHRPX_OPT_WORKER_WRITE_BURST.data(), required_argument, &flag, 53},
        {SHRPX_OPT_ALTSVC.data(), required_argument, &flag, 54},
        {SHRPX_OPT_ADD_RESPONSE_HEADER.data(), required_argument, &flag, 55},
        {SHRPX_OPT_WORKER_FRONTEND_CONNECTIONS.data(), required_argument, &flag,
         56},
        {SHRPX_OPT_ACCESSLOG_SYSLOG.data(), no_argument, &flag, 57},
        {SHRPX_OPT_ERRORLOG_FILE.data(), required_argument, &flag, 58},
        {SHRPX_OPT_ERRORLOG_SYSLOG.data(), no_argument, &flag, 59},
        {SHRPX_OPT_STREAM_READ_TIMEOUT.data(), required_argument, &flag, 60},
        {SHRPX_OPT_STREAM_WRITE_TIMEOUT.data(), required_argument, &flag, 61},
        {SHRPX_OPT_NO_LOCATION_REWRITE.data(), no_argument, &flag, 62},
        {SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_HOST.data(), required_argument,
         &flag, 63},
        {SHRPX_OPT_LISTENER_DISABLE_TIMEOUT.data(), required_argument, &flag,
         64},
        {SHRPX_OPT_STRIP_INCOMING_X_FORWARDED_FOR.data(), no_argument, &flag,
         65},
        {SHRPX_OPT_ACCESSLOG_FORMAT.data(), required_argument, &flag, 66},
        {SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_FRONTEND.data(),
         required_argument, &flag, 67},
        {SHRPX_OPT_TLS_TICKET_KEY_FILE.data(), required_argument, &flag, 68},
        {SHRPX_OPT_RLIMIT_NOFILE.data(), required_argument, &flag, 69},
        {SHRPX_OPT_BACKEND_RESPONSE_BUFFER.data(), required_argument, &flag,
         71},
        {SHRPX_OPT_BACKEND_REQUEST_BUFFER.data(), required_argument, &flag, 72},
        {SHRPX_OPT_NO_HOST_REWRITE.data(), no_argument, &flag, 73},
        {SHRPX_OPT_NO_SERVER_PUSH.data(), no_argument, &flag, 74},
        {SHRPX_OPT_BACKEND_HTTP2_CONNECTIONS_PER_WORKER.data(),
         required_argument, &flag, 76},
        {SHRPX_OPT_FETCH_OCSP_RESPONSE_FILE.data(), required_argument, &flag,
         77},
        {SHRPX_OPT_OCSP_UPDATE_INTERVAL.data(), required_argument, &flag, 78},
        {SHRPX_OPT_NO_OCSP.data(), no_argument, &flag, 79},
        {SHRPX_OPT_HEADER_FIELD_BUFFER.data(), required_argument, &flag, 80},
        {SHRPX_OPT_MAX_HEADER_FIELDS.data(), required_argument, &flag, 81},
        {SHRPX_OPT_ADD_REQUEST_HEADER.data(), required_argument, &flag, 82},
        {SHRPX_OPT_INCLUDE.data(), required_argument, &flag, 83},
        {SHRPX_OPT_TLS_TICKET_KEY_CIPHER.data(), required_argument, &flag, 84},
        {SHRPX_OPT_HOST_REWRITE.data(), no_argument, &flag, 85},
        {SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED.data(), required_argument, &flag,
         86},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED.data(), required_argument, &flag,
         87},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_INTERVAL.data(), required_argument,
         &flag, 88},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_MAX_RETRY.data(), required_argument,
         &flag, 89},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_MAX_FAIL.data(), required_argument,
         &flag, 90},
        {SHRPX_OPT_MRUBY_FILE.data(), required_argument, &flag, 91},
        {SHRPX_OPT_ACCEPT_PROXY_PROTOCOL.data(), no_argument, &flag, 93},
        {SHRPX_OPT_FASTOPEN.data(), required_argument, &flag, 94},
        {SHRPX_OPT_TLS_DYN_REC_WARMUP_THRESHOLD.data(), required_argument,
         &flag, 95},
        {SHRPX_OPT_TLS_DYN_REC_IDLE_TIMEOUT.data(), required_argument, &flag,
         96},
        {SHRPX_OPT_ADD_FORWARDED.data(), required_argument, &flag, 97},
        {SHRPX_OPT_STRIP_INCOMING_FORWARDED.data(), no_argument, &flag, 98},
        {SHRPX_OPT_FORWARDED_BY.data(), required_argument, &flag, 99},
        {SHRPX_OPT_FORWARDED_FOR.data(), required_argument, &flag, 100},
        {SHRPX_OPT_RESPONSE_HEADER_FIELD_BUFFER.data(), required_argument,
         &flag, 101},
        {SHRPX_OPT_MAX_RESPONSE_HEADER_FIELDS.data(), required_argument, &flag,
         102},
        {SHRPX_OPT_NO_HTTP2_CIPHER_BLACK_LIST.data(), no_argument, &flag, 103},
        {SHRPX_OPT_REQUEST_HEADER_FIELD_BUFFER.data(), required_argument, &flag,
         104},
        {SHRPX_OPT_MAX_REQUEST_HEADER_FIELDS.data(), required_argument, &flag,
         105},
        {SHRPX_OPT_BACKEND_HTTP1_TLS.data(), no_argument, &flag, 106},
        {SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_TLS.data(), no_argument, &flag,
         108},
        {SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_CERT_FILE.data(),
         required_argument, &flag, 109},
        {SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_PRIVATE_KEY_FILE.data(),
         required_argument, &flag, 110},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_TLS.data(), no_argument, &flag,
         111},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_CERT_FILE.data(), required_argument,
         &flag, 112},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_PRIVATE_KEY_FILE.data(),
         required_argument, &flag, 113},
        {SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_ADDRESS_FAMILY.data(),
         required_argument, &flag, 114},
        {SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_ADDRESS_FAMILY.data(),
         required_argument, &flag, 115},
        {SHRPX_OPT_BACKEND_ADDRESS_FAMILY.data(), required_argument, &flag,
         116},
        {SHRPX_OPT_FRONTEND_HTTP2_MAX_CONCURRENT_STREAMS.data(),
         required_argument, &flag, 117},
        {SHRPX_OPT_BACKEND_HTTP2_MAX_CONCURRENT_STREAMS.data(),
         required_argument, &flag, 118},
        {SHRPX_OPT_BACKEND_CONNECTIONS_PER_FRONTEND.data(), required_argument,
         &flag, 119},
        {SHRPX_OPT_BACKEND_TLS.data(), no_argument, &flag, 120},
        {SHRPX_OPT_BACKEND_CONNECTIONS_PER_HOST.data(), required_argument,
         &flag, 121},
        {SHRPX_OPT_ERROR_PAGE.data(), required_argument, &flag, 122},
        {SHRPX_OPT_NO_KQUEUE.data(), no_argument, &flag, 123},
        {SHRPX_OPT_FRONTEND_HTTP2_SETTINGS_TIMEOUT.data(), required_argument,
         &flag, 124},
        {SHRPX_OPT_BACKEND_HTTP2_SETTINGS_TIMEOUT.data(), required_argument,
         &flag, 125},
        {SHRPX_OPT_API_MAX_REQUEST_BODY.data(), required_argument, &flag, 126},
        {SHRPX_OPT_BACKEND_MAX_BACKOFF.data(), required_argument, &flag, 127},
        {SHRPX_OPT_SERVER_NAME.data(), required_argument, &flag, 128},
        {SHRPX_OPT_NO_SERVER_REWRITE.data(), no_argument, &flag, 129},
        {SHRPX_OPT_FRONTEND_HTTP2_OPTIMIZE_WRITE_BUFFER_SIZE.data(),
         no_argument, &flag, 130},
        {SHRPX_OPT_FRONTEND_HTTP2_OPTIMIZE_WINDOW_SIZE.data(), no_argument,
         &flag, 131},
        {SHRPX_OPT_FRONTEND_HTTP2_WINDOW_SIZE.data(), required_argument, &flag,
         132},
        {SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_SIZE.data(),
         required_argument, &flag, 133},
        {SHRPX_OPT_BACKEND_HTTP2_WINDOW_SIZE.data(), required_argument, &flag,
         134},
        {SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_SIZE.data(),
         required_argument, &flag, 135},
        {SHRPX_OPT_FRONTEND_HTTP2_ENCODER_DYNAMIC_TABLE_SIZE.data(),
         required_argument, &flag, 136},
        {SHRPX_OPT_FRONTEND_HTTP2_DECODER_DYNAMIC_TABLE_SIZE.data(),
         required_argument, &flag, 137},
        {SHRPX_OPT_BACKEND_HTTP2_ENCODER_DYNAMIC_TABLE_SIZE.data(),
         required_argument, &flag, 138},
        {SHRPX_OPT_BACKEND_HTTP2_DECODER_DYNAMIC_TABLE_SIZE.data(),
         required_argument, &flag, 139},
        {SHRPX_OPT_ECDH_CURVES.data(), required_argument, &flag, 140},
        {SHRPX_OPT_TLS_SCT_DIR.data(), required_argument, &flag, 141},
        {SHRPX_OPT_BACKEND_CONNECT_TIMEOUT.data(), required_argument, &flag,
         142},
        {SHRPX_OPT_DNS_CACHE_TIMEOUT.data(), required_argument, &flag, 143},
        {SHRPX_OPT_DNS_LOOKUP_TIMEOUT.data(), required_argument, &flag, 144},
        {SHRPX_OPT_DNS_MAX_TRY.data(), required_argument, &flag, 145},
        {SHRPX_OPT_FRONTEND_KEEP_ALIVE_TIMEOUT.data(), required_argument, &flag,
         146},
        {SHRPX_OPT_PSK_SECRETS.data(), required_argument, &flag, 147},
        {SHRPX_OPT_CLIENT_PSK_SECRETS.data(), required_argument, &flag, 148},
        {SHRPX_OPT_CLIENT_NO_HTTP2_CIPHER_BLACK_LIST.data(), no_argument, &flag,
         149},
        {SHRPX_OPT_CLIENT_CIPHERS.data(), required_argument, &flag, 150},
        {SHRPX_OPT_ACCESSLOG_WRITE_EARLY.data(), no_argument, &flag, 151},
        {SHRPX_OPT_TLS_MIN_PROTO_VERSION.data(), required_argument, &flag, 152},
        {SHRPX_OPT_TLS_MAX_PROTO_VERSION.data(), required_argument, &flag, 153},
        {SHRPX_OPT_REDIRECT_HTTPS_PORT.data(), required_argument, &flag, 154},
        {SHRPX_OPT_FRONTEND_MAX_REQUESTS.data(), required_argument, &flag, 155},
        {SHRPX_OPT_SINGLE_THREAD.data(), no_argument, &flag, 156},
        {SHRPX_OPT_NO_ADD_X_FORWARDED_PROTO.data(), no_argument, &flag, 157},
        {SHRPX_OPT_NO_STRIP_INCOMING_X_FORWARDED_PROTO.data(), no_argument,
         &flag, 158},
        {SHRPX_OPT_SINGLE_PROCESS.data(), no_argument, &flag, 159},
        {SHRPX_OPT_VERIFY_CLIENT_TOLERATE_EXPIRED.data(), no_argument, &flag,
         160},
        {SHRPX_OPT_IGNORE_PER_PATTERN_MRUBY_ERROR.data(), no_argument, &flag,
         161},
        {SHRPX_OPT_TLS_NO_POSTPONE_EARLY_DATA.data(), no_argument, &flag, 162},
        {SHRPX_OPT_TLS_MAX_EARLY_DATA.data(), required_argument, &flag, 163},
        {SHRPX_OPT_TLS13_CIPHERS.data(), required_argument, &flag, 164},
        {SHRPX_OPT_TLS13_CLIENT_CIPHERS.data(), required_argument, &flag, 165},
        {SHRPX_OPT_NO_STRIP_INCOMING_EARLY_DATA.data(), no_argument, &flag,
         166},
        {SHRPX_OPT_NO_HTTP2_CIPHER_BLOCK_LIST.data(), no_argument, &flag, 167},
        {SHRPX_OPT_CLIENT_NO_HTTP2_CIPHER_BLOCK_LIST.data(), no_argument, &flag,
         168},
        {SHRPX_OPT_QUIC_BPF_PROGRAM_FILE.data(), required_argument, &flag, 169},
        {SHRPX_OPT_NO_QUIC_BPF.data(), no_argument, &flag, 170},
        {SHRPX_OPT_HTTP2_ALTSVC.data(), required_argument, &flag, 171},
        {SHRPX_OPT_FRONTEND_HTTP3_READ_TIMEOUT.data(), required_argument, &flag,
         172},
        {SHRPX_OPT_FRONTEND_QUIC_IDLE_TIMEOUT.data(), required_argument, &flag,
         173},
        {SHRPX_OPT_FRONTEND_QUIC_DEBUG_LOG.data(), no_argument, &flag, 174},
        {SHRPX_OPT_FRONTEND_HTTP3_WINDOW_SIZE.data(), required_argument, &flag,
         175},
        {SHRPX_OPT_FRONTEND_HTTP3_CONNECTION_WINDOW_SIZE.data(),
         required_argument, &flag, 176},
        {SHRPX_OPT_FRONTEND_HTTP3_MAX_WINDOW_SIZE.data(), required_argument,
         &flag, 177},
        {SHRPX_OPT_FRONTEND_HTTP3_MAX_CONNECTION_WINDOW_SIZE.data(),
         required_argument, &flag, 178},
        {SHRPX_OPT_FRONTEND_HTTP3_MAX_CONCURRENT_STREAMS.data(),
         required_argument, &flag, 179},
        {SHRPX_OPT_FRONTEND_QUIC_EARLY_DATA.data(), no_argument, &flag, 180},
        {SHRPX_OPT_FRONTEND_QUIC_QLOG_DIR.data(), required_argument, &flag,
         181},
        {SHRPX_OPT_FRONTEND_QUIC_REQUIRE_TOKEN.data(), no_argument, &flag, 182},
        {SHRPX_OPT_FRONTEND_QUIC_CONGESTION_CONTROLLER.data(),
         required_argument, &flag, 183},
        {SHRPX_OPT_QUIC_SERVER_ID.data(), required_argument, &flag, 185},
        {SHRPX_OPT_FRONTEND_QUIC_SECRET_FILE.data(), required_argument, &flag,
         186},
        {SHRPX_OPT_RLIMIT_MEMLOCK.data(), required_argument, &flag, 187},
        {SHRPX_OPT_MAX_WORKER_PROCESSES.data(), required_argument, &flag, 188},
        {SHRPX_OPT_WORKER_PROCESS_GRACE_SHUTDOWN_PERIOD.data(),
         required_argument, &flag, 189},
        {SHRPX_OPT_FRONTEND_QUIC_INITIAL_RTT.data(), required_argument, &flag,
         190},
        {SHRPX_OPT_REQUIRE_HTTP_SCHEME.data(), no_argument, &flag, 191},
        {SHRPX_OPT_TLS_KTLS.data(), no_argument, &flag, 192},
        {SHRPX_OPT_ALPN_LIST.data(), required_argument, &flag, 193},
        {SHRPX_OPT_FRONTEND_HEADER_TIMEOUT.data(), required_argument, &flag,
         194},
        {SHRPX_OPT_FRONTEND_HTTP2_IDLE_TIMEOUT.data(), required_argument, &flag,
         195},
        {SHRPX_OPT_FRONTEND_HTTP3_IDLE_TIMEOUT.data(), required_argument, &flag,
         196},
        {nullptr, 0, nullptr, 0}};

    int option_index = 0;
    int c = getopt_long(argc, argv, "DL:b:c:f:hkn:opsv", long_options,
                        &option_index);
    if (c == -1) {
      break;
    }
    switch (c) {
    case 'D':
      cmdcfgs.emplace_back(SHRPX_OPT_DAEMON, "yes"_sr);
      break;
    case 'L':
      cmdcfgs.emplace_back(SHRPX_OPT_LOG_LEVEL, StringRef{optarg});
      break;
    case 'b':
      cmdcfgs.emplace_back(SHRPX_OPT_BACKEND, StringRef{optarg});
      break;
    case 'c':
      cmdcfgs.emplace_back(SHRPX_OPT_HTTP2_MAX_CONCURRENT_STREAMS,
                           StringRef{optarg});
      break;
    case 'f':
      cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND, StringRef{optarg});
      break;
    case 'h':
      print_help(std::cout);
      exit(EXIT_SUCCESS);
    case 'k':
      cmdcfgs.emplace_back(SHRPX_OPT_INSECURE, "yes"_sr);
      break;
    case 'n':
      cmdcfgs.emplace_back(SHRPX_OPT_WORKERS, StringRef{optarg});
      break;
    case 'o':
      cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_FRAME_DEBUG, "yes"_sr);
      break;
    case 'p':
      cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_PROXY, "yes"_sr);
      break;
    case 's':
      cmdcfgs.emplace_back(SHRPX_OPT_HTTP2_PROXY, "yes"_sr);
      break;
    case 'v':
      print_version(std::cout);
      exit(EXIT_SUCCESS);
    case '?':
      util::show_candidates(argv[optind - 1], long_options);
      exit(EXIT_FAILURE);
    case 0:
      switch (flag) {
      case 1:
        // --add-x-forwarded-for
        cmdcfgs.emplace_back(SHRPX_OPT_ADD_X_FORWARDED_FOR, "yes"_sr);
        break;
      case 2:
        // --frontend-http2-read-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_READ_TIMEOUT,
                             StringRef{optarg});
        break;
      case 3:
        // --frontend-read-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_READ_TIMEOUT,
                             StringRef{optarg});
        break;
      case 4:
        // --frontend-write-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_WRITE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 5:
        // --backend-read-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_READ_TIMEOUT, StringRef{optarg});
        break;
      case 6:
        // --backend-write-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_WRITE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 7:
        cmdcfgs.emplace_back(SHRPX_OPT_ACCESSLOG_FILE, StringRef{optarg});
        break;
      case 8:
        // --backend-keep-alive-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 9:
        // --frontend-http2-window-bits
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_WINDOW_BITS,
                             StringRef{optarg});
        break;
      case 10:
        cmdcfgs.emplace_back(SHRPX_OPT_PID_FILE, StringRef{optarg});
        break;
      case 11:
        cmdcfgs.emplace_back(SHRPX_OPT_USER, StringRef{optarg});
        break;
      case 12:
        // --conf
        mod_config()->conf_path =
            make_string_ref(mod_config()->balloc, StringRef{optarg});
        break;
      case 14:
        // --syslog-facility
        cmdcfgs.emplace_back(SHRPX_OPT_SYSLOG_FACILITY, StringRef{optarg});
        break;
      case 15:
        // --backlog
        cmdcfgs.emplace_back(SHRPX_OPT_BACKLOG, StringRef{optarg});
        break;
      case 16:
        // --ciphers
        cmdcfgs.emplace_back(SHRPX_OPT_CIPHERS, StringRef{optarg});
        break;
      case 17:
        // --client
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT, "yes"_sr);
        break;
      case 18:
        // --backend-http2-window-bits
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_WINDOW_BITS,
                             StringRef{optarg});
        break;
      case 19:
        // --cacert
        cmdcfgs.emplace_back(SHRPX_OPT_CACERT, StringRef{optarg});
        break;
      case 20:
        // --backend-ipv4
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_IPV4, "yes"_sr);
        break;
      case 21:
        // --backend-ipv6
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_IPV6, "yes"_sr);
        break;
      case 22:
        // --private-key-passwd-file
        cmdcfgs.emplace_back(SHRPX_OPT_PRIVATE_KEY_PASSWD_FILE,
                             StringRef{optarg});
        break;
      case 23:
        // --no-via
        cmdcfgs.emplace_back(SHRPX_OPT_NO_VIA, "yes"_sr);
        break;
      case 24:
        // --subcert
        cmdcfgs.emplace_back(SHRPX_OPT_SUBCERT, StringRef{optarg});
        break;
      case 25:
        // --http2-bridge
        cmdcfgs.emplace_back(SHRPX_OPT_HTTP2_BRIDGE, "yes"_sr);
        break;
      case 26:
        // --backend-http-proxy-uri
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP_PROXY_URI,
                             StringRef{optarg});
        break;
      case 27:
        // --backend-no-tls
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_NO_TLS, "yes"_sr);
        break;
      case 28:
        // --ocsp-startup
        cmdcfgs.emplace_back(SHRPX_OPT_OCSP_STARTUP, "yes"_sr);
        break;
      case 29:
        // --frontend-no-tls
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_NO_TLS, "yes"_sr);
        break;
      case 30:
        // --no-verify-ocsp
        cmdcfgs.emplace_back(SHRPX_OPT_NO_VERIFY_OCSP, "yes"_sr);
        break;
      case 31:
        // --backend-tls-sni-field
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_TLS_SNI_FIELD,
                             StringRef{optarg});
        break;
      case 33:
        // --dh-param-file
        cmdcfgs.emplace_back(SHRPX_OPT_DH_PARAM_FILE, StringRef{optarg});
        break;
      case 34:
        // --read-rate
        cmdcfgs.emplace_back(SHRPX_OPT_READ_RATE, StringRef{optarg});
        break;
      case 35:
        // --read-burst
        cmdcfgs.emplace_back(SHRPX_OPT_READ_BURST, StringRef{optarg});
        break;
      case 36:
        // --write-rate
        cmdcfgs.emplace_back(SHRPX_OPT_WRITE_RATE, StringRef{optarg});
        break;
      case 37:
        // --write-burst
        cmdcfgs.emplace_back(SHRPX_OPT_WRITE_BURST, StringRef{optarg});
        break;
      case 38:
        // --npn-list
        cmdcfgs.emplace_back(SHRPX_OPT_NPN_LIST, StringRef{optarg});
        break;
      case 39:
        // --verify-client
        cmdcfgs.emplace_back(SHRPX_OPT_VERIFY_CLIENT, "yes"_sr);
        break;
      case 40:
        // --verify-client-cacert
        cmdcfgs.emplace_back(SHRPX_OPT_VERIFY_CLIENT_CACERT, StringRef{optarg});
        break;
      case 41:
        // --client-private-key-file
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_PRIVATE_KEY_FILE,
                             StringRef{optarg});
        break;
      case 42:
        // --client-cert-file
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_CERT_FILE, StringRef{optarg});
        break;
      case 43:
        // --frontend-http2-dump-request-header
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_DUMP_REQUEST_HEADER,
                             StringRef{optarg});
        break;
      case 44:
        // --frontend-http2-dump-response-header
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_DUMP_RESPONSE_HEADER,
                             StringRef{optarg});
        break;
      case 45:
        // --http2-no-cookie-crumbling
        cmdcfgs.emplace_back(SHRPX_OPT_HTTP2_NO_COOKIE_CRUMBLING, "yes"_sr);
        break;
      case 46:
        // --frontend-http2-connection-window-bits
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_BITS,
                             StringRef{optarg});
        break;
      case 47:
        // --backend-http2-connection-window-bits
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_BITS,
                             StringRef{optarg});
        break;
      case 48:
        // --tls-proto-list
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_PROTO_LIST, StringRef{optarg});
        break;
      case 49:
        // --padding
        cmdcfgs.emplace_back(SHRPX_OPT_PADDING, StringRef{optarg});
        break;
      case 50:
        // --worker-read-rate
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_READ_RATE, StringRef{optarg});
        break;
      case 51:
        // --worker-read-burst
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_READ_BURST, StringRef{optarg});
        break;
      case 52:
        // --worker-write-rate
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_WRITE_RATE, StringRef{optarg});
        break;
      case 53:
        // --worker-write-burst
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_WRITE_BURST, StringRef{optarg});
        break;
      case 54:
        // --altsvc
        cmdcfgs.emplace_back(SHRPX_OPT_ALTSVC, StringRef{optarg});
        break;
      case 55:
        // --add-response-header
        cmdcfgs.emplace_back(SHRPX_OPT_ADD_RESPONSE_HEADER, StringRef{optarg});
        break;
      case 56:
        // --worker-frontend-connections
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_FRONTEND_CONNECTIONS,
                             StringRef{optarg});
        break;
      case 57:
        // --accesslog-syslog
        cmdcfgs.emplace_back(SHRPX_OPT_ACCESSLOG_SYSLOG, "yes"_sr);
        break;
      case 58:
        // --errorlog-file
        cmdcfgs.emplace_back(SHRPX_OPT_ERRORLOG_FILE, StringRef{optarg});
        break;
      case 59:
        // --errorlog-syslog
        cmdcfgs.emplace_back(SHRPX_OPT_ERRORLOG_SYSLOG, "yes"_sr);
        break;
      case 60:
        // --stream-read-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_STREAM_READ_TIMEOUT, StringRef{optarg});
        break;
      case 61:
        // --stream-write-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_STREAM_WRITE_TIMEOUT, StringRef{optarg});
        break;
      case 62:
        // --no-location-rewrite
        cmdcfgs.emplace_back(SHRPX_OPT_NO_LOCATION_REWRITE, "yes"_sr);
        break;
      case 63:
        // --backend-http1-connections-per-host
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_HOST,
                             StringRef{optarg});
        break;
      case 64:
        // --listener-disable-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_LISTENER_DISABLE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 65:
        // --strip-incoming-x-forwarded-for
        cmdcfgs.emplace_back(SHRPX_OPT_STRIP_INCOMING_X_FORWARDED_FOR,
                             "yes"_sr);
        break;
      case 66:
        // --accesslog-format
        cmdcfgs.emplace_back(SHRPX_OPT_ACCESSLOG_FORMAT, StringRef{optarg});
        break;
      case 67:
        // --backend-http1-connections-per-frontend
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_FRONTEND,
                             StringRef{optarg});
        break;
      case 68:
        // --tls-ticket-key-file
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_FILE, StringRef{optarg});
        break;
      case 69:
        // --rlimit-nofile
        cmdcfgs.emplace_back(SHRPX_OPT_RLIMIT_NOFILE, StringRef{optarg});
        break;
      case 71:
        // --backend-response-buffer
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_RESPONSE_BUFFER,
                             StringRef{optarg});
        break;
      case 72:
        // --backend-request-buffer
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_REQUEST_BUFFER,
                             StringRef{optarg});
        break;
      case 73:
        // --no-host-rewrite
        cmdcfgs.emplace_back(SHRPX_OPT_NO_HOST_REWRITE, "yes"_sr);
        break;
      case 74:
        // --no-server-push
        cmdcfgs.emplace_back(SHRPX_OPT_NO_SERVER_PUSH, "yes"_sr);
        break;
      case 76:
        // --backend-http2-connections-per-worker
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_CONNECTIONS_PER_WORKER,
                             StringRef{optarg});
        break;
      case 77:
        // --fetch-ocsp-response-file
        cmdcfgs.emplace_back(SHRPX_OPT_FETCH_OCSP_RESPONSE_FILE,
                             StringRef{optarg});
        break;
      case 78:
        // --ocsp-update-interval
        cmdcfgs.emplace_back(SHRPX_OPT_OCSP_UPDATE_INTERVAL, StringRef{optarg});
        break;
      case 79:
        // --no-ocsp
        cmdcfgs.emplace_back(SHRPX_OPT_NO_OCSP, "yes"_sr);
        break;
      case 80:
        // --header-field-buffer
        cmdcfgs.emplace_back(SHRPX_OPT_HEADER_FIELD_BUFFER, StringRef{optarg});
        break;
      case 81:
        // --max-header-fields
        cmdcfgs.emplace_back(SHRPX_OPT_MAX_HEADER_FIELDS, StringRef{optarg});
        break;
      case 82:
        // --add-request-header
        cmdcfgs.emplace_back(SHRPX_OPT_ADD_REQUEST_HEADER, StringRef{optarg});
        break;
      case 83:
        // --include
        cmdcfgs.emplace_back(SHRPX_OPT_INCLUDE, StringRef{optarg});
        break;
      case 84:
        // --tls-ticket-key-cipher
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_CIPHER,
                             StringRef{optarg});
        break;
      case 85:
        // --host-rewrite
        cmdcfgs.emplace_back(SHRPX_OPT_HOST_REWRITE, "yes"_sr);
        break;
      case 86:
        // --tls-session-cache-memcached
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED,
                             StringRef{optarg});
        break;
      case 87:
        // --tls-ticket-key-memcached
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED,
                             StringRef{optarg});
        break;
      case 88:
        // --tls-ticket-key-memcached-interval
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_INTERVAL,
                             StringRef{optarg});
        break;
      case 89:
        // --tls-ticket-key-memcached-max-retry
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_MAX_RETRY,
                             StringRef{optarg});
        break;
      case 90:
        // --tls-ticket-key-memcached-max-fail
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_MAX_FAIL,
                             StringRef{optarg});
        break;
      case 91:
        // --mruby-file
        cmdcfgs.emplace_back(SHRPX_OPT_MRUBY_FILE, StringRef{optarg});
        break;
      case 93:
        // --accept-proxy-protocol
        cmdcfgs.emplace_back(SHRPX_OPT_ACCEPT_PROXY_PROTOCOL, "yes"_sr);
        break;
      case 94:
        // --fastopen
        cmdcfgs.emplace_back(SHRPX_OPT_FASTOPEN, StringRef{optarg});
        break;
      case 95:
        // --tls-dyn-rec-warmup-threshold
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_DYN_REC_WARMUP_THRESHOLD,
                             StringRef{optarg});
        break;
      case 96:
        // --tls-dyn-rec-idle-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_DYN_REC_IDLE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 97:
        // --add-forwarded
        cmdcfgs.emplace_back(SHRPX_OPT_ADD_FORWARDED, StringRef{optarg});
        break;
      case 98:
        // --strip-incoming-forwarded
        cmdcfgs.emplace_back(SHRPX_OPT_STRIP_INCOMING_FORWARDED, "yes"_sr);
        break;
      case 99:
        // --forwarded-by
        cmdcfgs.emplace_back(SHRPX_OPT_FORWARDED_BY, StringRef{optarg});
        break;
      case 100:
        // --forwarded-for
        cmdcfgs.emplace_back(SHRPX_OPT_FORWARDED_FOR, StringRef{optarg});
        break;
      case 101:
        // --response-header-field-buffer
        cmdcfgs.emplace_back(SHRPX_OPT_RESPONSE_HEADER_FIELD_BUFFER,
                             StringRef{optarg});
        break;
      case 102:
        // --max-response-header-fields
        cmdcfgs.emplace_back(SHRPX_OPT_MAX_RESPONSE_HEADER_FIELDS,
                             StringRef{optarg});
        break;
      case 103:
        // --no-http2-cipher-black-list
        cmdcfgs.emplace_back(SHRPX_OPT_NO_HTTP2_CIPHER_BLACK_LIST, "yes"_sr);
        break;
      case 104:
        // --request-header-field-buffer
        cmdcfgs.emplace_back(SHRPX_OPT_REQUEST_HEADER_FIELD_BUFFER,
                             StringRef{optarg});
        break;
      case 105:
        // --max-request-header-fields
        cmdcfgs.emplace_back(SHRPX_OPT_MAX_REQUEST_HEADER_FIELDS,
                             StringRef{optarg});
        break;
      case 106:
        // --backend-http1-tls
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP1_TLS, "yes"_sr);
        break;
      case 108:
        // --tls-session-cache-memcached-tls
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_TLS,
                             "yes"_sr);
        break;
      case 109:
        // --tls-session-cache-memcached-cert-file
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_CERT_FILE,
                             StringRef{optarg});
        break;
      case 110:
        // --tls-session-cache-memcached-private-key-file
        cmdcfgs.emplace_back(
            SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_PRIVATE_KEY_FILE,
            StringRef{optarg});
        break;
      case 111:
        // --tls-ticket-key-memcached-tls
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_TLS, "yes"_sr);
        break;
      case 112:
        // --tls-ticket-key-memcached-cert-file
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_CERT_FILE,
                             StringRef{optarg});
        break;
      case 113:
        // --tls-ticket-key-memcached-private-key-file
        cmdcfgs.emplace_back(
            SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_PRIVATE_KEY_FILE,
            StringRef{optarg});
        break;
      case 114:
        // --tls-ticket-key-memcached-address-family
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_TICKET_KEY_MEMCACHED_ADDRESS_FAMILY,
                             StringRef{optarg});
        break;
      case 115:
        // --tls-session-cache-memcached-address-family
        cmdcfgs.emplace_back(
            SHRPX_OPT_TLS_SESSION_CACHE_MEMCACHED_ADDRESS_FAMILY,
            StringRef{optarg});
        break;
      case 116:
        // --backend-address-family
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_ADDRESS_FAMILY,
                             StringRef{optarg});
        break;
      case 117:
        // --frontend-http2-max-concurrent-streams
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_MAX_CONCURRENT_STREAMS,
                             StringRef{optarg});
        break;
      case 118:
        // --backend-http2-max-concurrent-streams
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_MAX_CONCURRENT_STREAMS,
                             StringRef{optarg});
        break;
      case 119:
        // --backend-connections-per-frontend
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_CONNECTIONS_PER_FRONTEND,
                             StringRef{optarg});
        break;
      case 120:
        // --backend-tls
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_TLS, "yes"_sr);
        break;
      case 121:
        // --backend-connections-per-host
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_CONNECTIONS_PER_HOST,
                             StringRef{optarg});
        break;
      case 122:
        // --error-page
        cmdcfgs.emplace_back(SHRPX_OPT_ERROR_PAGE, StringRef{optarg});
        break;
      case 123:
        // --no-kqueue
        cmdcfgs.emplace_back(SHRPX_OPT_NO_KQUEUE, "yes"_sr);
        break;
      case 124:
        // --frontend-http2-settings-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_SETTINGS_TIMEOUT,
                             StringRef{optarg});
        break;
      case 125:
        // --backend-http2-settings-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_SETTINGS_TIMEOUT,
                             StringRef{optarg});
        break;
      case 126:
        // --api-max-request-body
        cmdcfgs.emplace_back(SHRPX_OPT_API_MAX_REQUEST_BODY, StringRef{optarg});
        break;
      case 127:
        // --backend-max-backoff
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_MAX_BACKOFF, StringRef{optarg});
        break;
      case 128:
        // --server-name
        cmdcfgs.emplace_back(SHRPX_OPT_SERVER_NAME, StringRef{optarg});
        break;
      case 129:
        // --no-server-rewrite
        cmdcfgs.emplace_back(SHRPX_OPT_NO_SERVER_REWRITE, "yes"_sr);
        break;
      case 130:
        // --frontend-http2-optimize-write-buffer-size
        cmdcfgs.emplace_back(
            SHRPX_OPT_FRONTEND_HTTP2_OPTIMIZE_WRITE_BUFFER_SIZE, "yes"_sr);
        break;
      case 131:
        // --frontend-http2-optimize-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_OPTIMIZE_WINDOW_SIZE,
                             "yes"_sr);
        break;
      case 132:
        // --frontend-http2-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 133:
        // --frontend-http2-connection-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 134:
        // --backend-http2-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 135:
        // --backend-http2-connection-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 136:
        // --frontend-http2-encoder-dynamic-table-size
        cmdcfgs.emplace_back(
            SHRPX_OPT_FRONTEND_HTTP2_ENCODER_DYNAMIC_TABLE_SIZE,
            StringRef{optarg});
        break;
      case 137:
        // --frontend-http2-decoder-dynamic-table-size
        cmdcfgs.emplace_back(
            SHRPX_OPT_FRONTEND_HTTP2_DECODER_DYNAMIC_TABLE_SIZE,
            StringRef{optarg});
        break;
      case 138:
        // --backend-http2-encoder-dynamic-table-size
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_ENCODER_DYNAMIC_TABLE_SIZE,
                             StringRef{optarg});
        break;
      case 139:
        // --backend-http2-decoder-dynamic-table-size
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_HTTP2_DECODER_DYNAMIC_TABLE_SIZE,
                             StringRef{optarg});
        break;
      case 140:
        // --ecdh-curves
        cmdcfgs.emplace_back(SHRPX_OPT_ECDH_CURVES, StringRef{optarg});
        break;
      case 141:
        // --tls-sct-dir
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_SCT_DIR, StringRef{optarg});
        break;
      case 142:
        // --backend-connect-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_CONNECT_TIMEOUT,
                             StringRef{optarg});
        break;
      case 143:
        // --dns-cache-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_DNS_CACHE_TIMEOUT, StringRef{optarg});
        break;
      case 144:
        // --dns-lookup-timeou
        cmdcfgs.emplace_back(SHRPX_OPT_DNS_LOOKUP_TIMEOUT, StringRef{optarg});
        break;
      case 145:
        // --dns-max-try
        cmdcfgs.emplace_back(SHRPX_OPT_DNS_MAX_TRY, StringRef{optarg});
        break;
      case 146:
        // --frontend-keep-alive-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_KEEP_ALIVE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 147:
        // --psk-secrets
        cmdcfgs.emplace_back(SHRPX_OPT_PSK_SECRETS, StringRef{optarg});
        break;
      case 148:
        // --client-psk-secrets
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_PSK_SECRETS, StringRef{optarg});
        break;
      case 149:
        // --client-no-http2-cipher-black-list
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_NO_HTTP2_CIPHER_BLACK_LIST,
                             "yes"_sr);
        break;
      case 150:
        // --client-ciphers
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_CIPHERS, StringRef{optarg});
        break;
      case 151:
        // --accesslog-write-early
        cmdcfgs.emplace_back(SHRPX_OPT_ACCESSLOG_WRITE_EARLY, "yes"_sr);
        break;
      case 152:
        // --tls-min-proto-version
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_MIN_PROTO_VERSION,
                             StringRef{optarg});
        break;
      case 153:
        // --tls-max-proto-version
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_MAX_PROTO_VERSION,
                             StringRef{optarg});
        break;
      case 154:
        // --redirect-https-port
        cmdcfgs.emplace_back(SHRPX_OPT_REDIRECT_HTTPS_PORT, StringRef{optarg});
        break;
      case 155:
        // --frontend-max-requests
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_MAX_REQUESTS,
                             StringRef{optarg});
        break;
      case 156:
        // --single-thread
        cmdcfgs.emplace_back(SHRPX_OPT_SINGLE_THREAD, "yes"_sr);
        break;
      case 157:
        // --no-add-x-forwarded-proto
        cmdcfgs.emplace_back(SHRPX_OPT_NO_ADD_X_FORWARDED_PROTO, "yes"_sr);
        break;
      case 158:
        // --no-strip-incoming-x-forwarded-proto
        cmdcfgs.emplace_back(SHRPX_OPT_NO_STRIP_INCOMING_X_FORWARDED_PROTO,
                             "yes"_sr);
        break;
      case 159:
        // --single-process
        cmdcfgs.emplace_back(SHRPX_OPT_SINGLE_PROCESS, "yes"_sr);
        break;
      case 160:
        // --verify-client-tolerate-expired
        cmdcfgs.emplace_back(SHRPX_OPT_VERIFY_CLIENT_TOLERATE_EXPIRED,
                             "yes"_sr);
        break;
      case 161:
        // --ignore-per-pattern-mruby-error
        cmdcfgs.emplace_back(SHRPX_OPT_IGNORE_PER_PATTERN_MRUBY_ERROR,
                             "yes"_sr);
        break;
      case 162:
        // --tls-no-postpone-early-data
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_NO_POSTPONE_EARLY_DATA, "yes"_sr);
        break;
      case 163:
        // --tls-max-early-data
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_MAX_EARLY_DATA, StringRef{optarg});
        break;
      case 164:
        // --tls13-ciphers
        cmdcfgs.emplace_back(SHRPX_OPT_TLS13_CIPHERS, StringRef{optarg});
        break;
      case 165:
        // --tls13-client-ciphers
        cmdcfgs.emplace_back(SHRPX_OPT_TLS13_CLIENT_CIPHERS, StringRef{optarg});
        break;
      case 166:
        // --no-strip-incoming-early-data
        cmdcfgs.emplace_back(SHRPX_OPT_NO_STRIP_INCOMING_EARLY_DATA, "yes"_sr);
        break;
      case 167:
        // --no-http2-cipher-block-list
        cmdcfgs.emplace_back(SHRPX_OPT_NO_HTTP2_CIPHER_BLOCK_LIST, "yes"_sr);
        break;
      case 168:
        // --client-no-http2-cipher-block-list
        cmdcfgs.emplace_back(SHRPX_OPT_CLIENT_NO_HTTP2_CIPHER_BLOCK_LIST,
                             "yes"_sr);
        break;
      case 169:
        // --quic-bpf-program-file
        cmdcfgs.emplace_back(SHRPX_OPT_QUIC_BPF_PROGRAM_FILE,
                             StringRef{optarg});
        break;
      case 170:
        // --no-quic-bpf
        cmdcfgs.emplace_back(SHRPX_OPT_NO_QUIC_BPF, "yes"_sr);
        break;
      case 171:
        // --http2-altsvc
        cmdcfgs.emplace_back(SHRPX_OPT_HTTP2_ALTSVC, StringRef{optarg});
        break;
      case 172:
        // --frontend-http3-read-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_READ_TIMEOUT,
                             StringRef{optarg});
        break;
      case 173:
        // --frontend-quic-idle-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_IDLE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 174:
        // --frontend-quic-debug-log
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_DEBUG_LOG, "yes"_sr);
        break;
      case 175:
        // --frontend-http3-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 176:
        // --frontend-http3-connection-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_CONNECTION_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 177:
        // --frontend-http3-max-window-size
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_MAX_WINDOW_SIZE,
                             StringRef{optarg});
        break;
      case 178:
        // --frontend-http3-max-connection-window-size
        cmdcfgs.emplace_back(
            SHRPX_OPT_FRONTEND_HTTP3_MAX_CONNECTION_WINDOW_SIZE,
            StringRef{optarg});
        break;
      case 179:
        // --frontend-http3-max-concurrent-streams
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_MAX_CONCURRENT_STREAMS,
                             StringRef{optarg});
        break;
      case 180:
        // --frontend-quic-early-data
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_EARLY_DATA, "yes"_sr);
        break;
      case 181:
        // --frontend-quic-qlog-dir
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_QLOG_DIR,
                             StringRef{optarg});
        break;
      case 182:
        // --frontend-quic-require-token
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_REQUIRE_TOKEN, "yes"_sr);
        break;
      case 183:
        // --frontend-quic-congestion-controller
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_CONGESTION_CONTROLLER,
                             StringRef{optarg});
        break;
      case 185:
        // --quic-server-id
        cmdcfgs.emplace_back(SHRPX_OPT_QUIC_SERVER_ID, StringRef{optarg});
        break;
      case 186:
        // --frontend-quic-secret-file
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_SECRET_FILE,
                             StringRef{optarg});
        break;
      case 187:
        // --rlimit-memlock
        cmdcfgs.emplace_back(SHRPX_OPT_RLIMIT_MEMLOCK, StringRef{optarg});
        break;
      case 188:
        // --max-worker-processes
        cmdcfgs.emplace_back(SHRPX_OPT_MAX_WORKER_PROCESSES, StringRef{optarg});
        break;
      case 189:
        // --worker-process-grace-shutdown-period
        cmdcfgs.emplace_back(SHRPX_OPT_WORKER_PROCESS_GRACE_SHUTDOWN_PERIOD,
                             StringRef{optarg});
        break;
      case 190:
        // --frontend-quic-initial-rtt
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_QUIC_INITIAL_RTT,
                             StringRef{optarg});
        break;
      case 191:
        // --require-http-scheme
        cmdcfgs.emplace_back(SHRPX_OPT_REQUIRE_HTTP_SCHEME, "yes"_sr);
        break;
      case 192:
        // --tls-ktls
        cmdcfgs.emplace_back(SHRPX_OPT_TLS_KTLS, "yes"_sr);
        break;
      case 193:
        // --alpn-list
        cmdcfgs.emplace_back(SHRPX_OPT_ALPN_LIST, StringRef{optarg});
        break;
      case 194:
        // --frontend-header-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HEADER_TIMEOUT,
                             StringRef{optarg});
        break;
      case 195:
        // --frontend-http2-idle-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP2_IDLE_TIMEOUT,
                             StringRef{optarg});
        break;
      case 196:
        // --frontend-http3-idle-timeout
        cmdcfgs.emplace_back(SHRPX_OPT_FRONTEND_HTTP3_IDLE_TIMEOUT,
                             StringRef{optarg});
        break;
      default:
        break;
      }
      break;
    default:
      break;
    }
  }

  if (argc - optind >= 2) {
    cmdcfgs.emplace_back(SHRPX_OPT_PRIVATE_KEY_FILE, StringRef{argv[optind++]});
    cmdcfgs.emplace_back(SHRPX_OPT_CERTIFICATE_FILE, StringRef{argv[optind++]});
  }

  rv = process_options(mod_config(), cmdcfgs);
  if (rv != 0) {
    return -1;
  }

  if (event_loop() != 0) {
    return -1;
  }

  LOG(NOTICE) << "Shutdown momentarily";

  delete_log_config();

  return 0;
}

} // namespace shrpx

int main(int argc, char **argv) { return run_app(shrpx::main, argc, argv); }