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
|
/*
drbdadm_main.c
This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
Copyright (C) 2002-2008, LINBIT Information Technologies GmbH.
Copyright (C) 2002-2008, Philipp Reisner <philipp.reisner@linbit.com>.
Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
drbd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
drbd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with drbd; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _GNU_SOURCE
#define _XOPEN_SOURCE 600
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <search.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <time.h>
#include "linux/drbd.h"
#include "linux/drbd_limits.h"
#include "drbdtool_common.h"
#include "drbdadm.h"
#include "registry.h"
#include "config_flags.h"
#include "drbdadm_dump.h"
#include "shared_main.h"
#include "drbdadm_parser.h"
#define MAX_ARGS 40
char *progname;
struct deferred_cmd {
struct cfg_ctx ctx;
STAILQ_ENTRY(deferred_cmd) link;
};
struct option general_admopt[] = {
{"stacked", no_argument, 0, 'S'},
{"dry-run", no_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{"config-file", required_argument, 0, 'c'},
{"config-to-test", required_argument, 0, 't'},
{"config-to-exclude", required_argument, 0, 'E'},
{"drbdsetup", required_argument, 0, 's'},
{"drbdmeta", required_argument, 0, 'm'},
{"drbd-proxy-ctl", required_argument, 0, 'p'},
{"sh-varname", required_argument, 0, 'n'},
{"peer", required_argument, 0, 'P'},
{"version", no_argument, 0, 'V'},
{"setup-option", required_argument, 0, 'W'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
struct option *admopt = general_admopt;
extern int yydebug;
extern FILE *yyin;
static int adm_adjust(const struct cfg_ctx *ctx);
static int adm_new_minor(const struct cfg_ctx *ctx);
static int adm_resource(const struct cfg_ctx *);
static int adm_attach(const struct cfg_ctx *);
static int adm_connect(const struct cfg_ctx *);
static int adm_new_peer(const struct cfg_ctx *);
static int adm_path(const struct cfg_ctx *);
static int adm_resize(const struct cfg_ctx *);
static int adm_up(const struct cfg_ctx *);
static int adm_wait_c(const struct cfg_ctx *);
static int adm_wait_ci(const struct cfg_ctx *);
static int adm_proxy_up(const struct cfg_ctx *);
static int adm_proxy_down(const struct cfg_ctx *);
static int sh_nop(const struct cfg_ctx *);
static int sh_resources(const struct cfg_ctx *);
static int sh_resource(const struct cfg_ctx *);
static int sh_mod_parms(const struct cfg_ctx *);
static int sh_dev(const struct cfg_ctx *);
static int sh_udev(const struct cfg_ctx *);
static int sh_minor(const struct cfg_ctx *);
static int sh_ip(const struct cfg_ctx *);
static int sh_lres(const struct cfg_ctx *);
static int sh_ll_dev(const struct cfg_ctx *);
static int sh_md_dev(const struct cfg_ctx *);
static int sh_md_idx(const struct cfg_ctx *);
static int adm_drbdmeta(const struct cfg_ctx *);
static int adm_khelper(const struct cfg_ctx *);
static int adm_setup_and_meta(const struct cfg_ctx *);
static int hidden_cmds(const struct cfg_ctx *);
static int adm_outdate(const struct cfg_ctx *);
static int adm_chk_resize(const struct cfg_ctx *);
static int adm_drbdsetup(const struct cfg_ctx *);
static int adm_invalidate(const struct cfg_ctx *);
static int __adm_drbdsetup_silent(const struct cfg_ctx *ctx);
static int adm_forget_peer(const struct cfg_ctx *);
static int adm_peer_device(const struct cfg_ctx *);
static int do_proxy_conn_up(const struct cfg_ctx *ctx);
static int do_proxy_conn_down(const struct cfg_ctx *ctx);
static int do_proxy_conn_plugins(const struct cfg_ctx *ctx);
int ctx_by_name(struct cfg_ctx *ctx, const char *id, checks check);
int was_file_already_seen(char *fn);
static char *get_opt_val(struct options *, const char *, char *);
char ss_buffer[1024];
const char *hostname;
int line = 1;
int fline;
char *config_file = NULL;
char *config_save = NULL;
char *config_test = NULL;
struct resources config = STAILQ_HEAD_INITIALIZER(config);
struct d_resource *common = NULL;
struct ifreq *ifreq_list = NULL;
int is_drbd_top;
enum { NORMAL, STACKED, IGNORED, __N_RESOURCE_TYPES };
int nr_resources[__N_RESOURCE_TYPES];
int nr_volumes[__N_RESOURCE_TYPES];
int number_of_minors = 0;
int config_from_stdin = 0;
int config_valid = 1;
int no_tty;
int dry_run = 0;
int verbose = 0;
int adjust_with_progress = 0;
bool help;
int do_verify_ips = 0;
int do_register = 1;
/* if we want to adjust more than one resource,
* instead of iteratively calling "drbdsetup show" for each of them,
* call "drbdsetup show" once for all of them. */
int adjust_more_than_one_resource = 0;
char *drbdsetup = NULL;
char *drbdmeta = NULL;
char *drbdadm_83 = NULL;
char *drbdadm_84 = NULL;
char *drbd_proxy_ctl;
char *sh_varname = NULL;
struct names backend_options = STAILQ_HEAD_INITIALIZER(backend_options);
char *connect_to_host = NULL;
STAILQ_HEAD(deferred_cmds, deferred_cmd) deferred_cmds[__CFG_LAST];
int adm_adjust_wp(const struct cfg_ctx *ctx)
{
if (!verbose && !dry_run)
adjust_with_progress = 1;
return adm_adjust(ctx);
}
/* DRBD adm_cmd flags shortcuts,
* to avoid merge conflicts and unreadable diffs
* when we add the next flag */
#define ACF1_DEFAULT \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 1, \
.verify_ips = 0, \
.uc_dialog = 1, \
#define ACF1_MINOR_ONLY \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 0, \
.iterate_volumes = 1, \
.verify_ips = 0, \
.uc_dialog = 1, \
.disk_required = 1, \
#define ACF1_RESNAME \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.uc_dialog = 1, \
#define ACF1_RESNAME_VERIFY_IPS \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 1, \
.uc_dialog = 1, \
#define ACF1_CONNECT \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 0, \
.verify_ips = 1, \
.need_peer = 1, \
.uc_dialog = 1, \
#define ACF1_DISCONNECT \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.need_peer = 1, \
.uc_dialog = 1, \
#define ACF1_DEFNET \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 1, \
.verify_ips = 1, \
.need_peer = 1, \
.uc_dialog = 1, \
#define ACF1_WAIT \
.show_in_usage = 1, \
.res_name_required = 1, \
.vol_id_optional = 1, \
.verify_ips = 1, \
.uc_dialog = 1, \
#define ACF1_PEER_DEVICE \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 1, \
.need_peer = 1, \
.verify_ips = 0, \
.uc_dialog = 1, \
#define ACF3_RES_HANDLER \
.show_in_usage = 3, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 0, \
.vol_id_required = 0, \
.verify_ips = 0, \
.use_cached_config_file = 1, \
#define ACF4_ADVANCED \
.show_in_usage = 4, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 1, \
.verify_ips = 0, \
.uc_dialog = 1, \
#define ACF4_ADVANCED_NEED_VOL \
.show_in_usage = 4, \
.res_name_required = 1, \
.backend_res_name = 1, \
.iterate_volumes = 1, \
.vol_id_required = 1, \
.verify_ips = 0, \
.uc_dialog = 1, \
#define ACF1_DUMP \
.show_in_usage = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 1, \
.uc_dialog = 1, \
.test_config = 1, \
#define ACF2_SHELL \
.show_in_usage = 2, \
.iterate_volumes = 1, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 0, \
#define ACF2_SH_RESNAME \
.show_in_usage = 2, \
.iterate_volumes = 0, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 0, \
#define ACF2_PROXY \
.show_in_usage = 2, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 0, \
.need_peer = 1, \
.is_proxy_cmd = 1, \
#define ACF2_HOOK \
.show_in_usage = 2, \
.res_name_required = 1, \
.backend_res_name = 1, \
.verify_ips = 0, \
.use_cached_config_file = 1, \
#define ACF2_GEN_SHELL \
.show_in_usage = 2, \
.res_name_required = 0, \
.verify_ips = 0, \
/* */ struct adm_cmd attach_cmd = {"attach", adm_attach, &attach_cmd_ctx, ACF1_MINOR_ONLY };
/* */ struct adm_cmd disk_options_cmd = {"disk-options", adm_attach, &attach_cmd_ctx, ACF1_MINOR_ONLY };
/* */ struct adm_cmd detach_cmd = {"detach", adm_drbdsetup, &detach_cmd_ctx, .takes_long = 1, ACF1_MINOR_ONLY };
/* */ struct adm_cmd new_peer_cmd = {"new-peer", adm_new_peer, &new_peer_cmd_ctx, ACF1_CONNECT};
/* */ struct adm_cmd del_peer_cmd = {"del-peer", adm_drbdsetup, &disconnect_cmd_ctx, ACF1_CONNECT};
/* */ struct adm_cmd new_path_cmd = {"new-path", adm_path, &path_cmd_ctx, ACF1_CONNECT .iterate_paths = 1};
/* */ struct adm_cmd del_path_cmd = {"del-path", adm_path, &path_cmd_ctx, ACF1_CONNECT .iterate_paths = 1};
/* */ struct adm_cmd connect_cmd = {"connect", adm_connect, &connect_cmd_ctx, ACF1_CONNECT};
/* */ struct adm_cmd net_options_cmd = {"net-options", adm_new_peer, &net_options_ctx, ACF1_CONNECT};
/* */ struct adm_cmd disconnect_cmd = {"disconnect", adm_drbdsetup, &disconnect_cmd_ctx, ACF1_DISCONNECT};
static struct adm_cmd up_cmd = {"up", adm_up, ACF1_RESNAME_VERIFY_IPS };
/* */ struct adm_cmd res_options_cmd = {"resource-options", adm_resource, &resource_options_ctx, ACF1_RESNAME};
static struct adm_cmd down_cmd = {"down", adm_drbdsetup, ACF1_RESNAME .takes_long = 1};
static struct adm_cmd primary_cmd = {"primary", adm_drbdsetup, &primary_cmd_ctx, ACF1_RESNAME .takes_long = 1};
static struct adm_cmd secondary_cmd = {"secondary", adm_drbdsetup, ACF1_RESNAME .takes_long = 1};
static struct adm_cmd invalidate_cmd = {"invalidate", adm_invalidate, ACF1_MINOR_ONLY };
static struct adm_cmd invalidate_remote_cmd = {"invalidate-remote", adm_drbdsetup, ACF1_PEER_DEVICE .takes_long = 1};
static struct adm_cmd outdate_cmd = {"outdate", adm_outdate, ACF1_DEFAULT};
/* */ struct adm_cmd resize_cmd = {"resize", adm_resize, &resize_cmd_ctx, ACF1_DEFAULT .disk_required = 1};
static struct adm_cmd verify_cmd = {"verify", adm_drbdsetup, &verify_cmd_ctx, ACF1_PEER_DEVICE};
static struct adm_cmd pause_sync_cmd = {"pause-sync", adm_drbdsetup, ACF1_PEER_DEVICE};
static struct adm_cmd resume_sync_cmd = {"resume-sync", adm_drbdsetup, ACF1_PEER_DEVICE};
static struct adm_cmd adjust_cmd = {"adjust", adm_adjust, &adjust_ctx, ACF1_RESNAME_VERIFY_IPS .vol_id_optional = 1};
static struct adm_cmd adjust_wp_cmd = {"adjust-with-progress", adm_adjust_wp, ACF1_RESNAME_VERIFY_IPS};
static struct adm_cmd wait_c_cmd = {"wait-connect", adm_wait_c, ACF1_WAIT};
static struct adm_cmd wait_sync_cmd = {"wait-sync", adm_wait_c, ACF1_WAIT};
static struct adm_cmd wait_ci_cmd = {"wait-con-int", adm_wait_ci, .show_in_usage = 1,.verify_ips = 1,};
static struct adm_cmd role_cmd = {"role", adm_drbdsetup, ACF1_RESNAME};
static struct adm_cmd cstate_cmd = {"cstate", adm_drbdsetup, ACF1_DISCONNECT};
static struct adm_cmd dstate_cmd = {"dstate", adm_setup_and_meta, ACF1_MINOR_ONLY };
static struct adm_cmd status_cmd = {"status", adm_drbdsetup, .show_in_usage = 1, .uc_dialog = 1, .backend_res_name=1};
static struct adm_cmd peer_device_options_cmd = {"peer-device-options", adm_peer_device,
&peer_device_options_ctx, ACF1_PEER_DEVICE};
static struct adm_cmd dump_cmd = {"dump", adm_dump, ACF1_DUMP};
static struct adm_cmd dump_xml_cmd = {"dump-xml", adm_dump_xml, ACF1_DUMP};
static struct adm_cmd create_md_cmd = {"create-md", adm_create_md, &create_md_ctx, ACF1_MINOR_ONLY };
static struct adm_cmd show_gi_cmd = {"show-gi", adm_setup_and_meta, ACF1_PEER_DEVICE .disk_required = 1};
static struct adm_cmd get_gi_cmd = {"get-gi", adm_setup_and_meta, ACF1_PEER_DEVICE .disk_required = 1};
static struct adm_cmd dump_md_cmd = {"dump-md", adm_drbdmeta, &dump_md_ctx, ACF1_MINOR_ONLY };
static struct adm_cmd wipe_md_cmd = {"wipe-md", adm_drbdmeta, ACF1_MINOR_ONLY };
static struct adm_cmd apply_al_cmd = {"apply-al", adm_drbdmeta, ACF1_MINOR_ONLY };
static struct adm_cmd forget_peer_cmd = {"forget-peer", adm_forget_peer, ACF1_DISCONNECT };
static struct adm_cmd hidden_cmd = {"hidden-commands", hidden_cmds,.show_in_usage = 1,};
static struct adm_cmd sh_nop_cmd = {"sh-nop", sh_nop, ACF2_GEN_SHELL .uc_dialog = 1, .test_config = 1};
static struct adm_cmd sh_resources_cmd = {"sh-resources", sh_resources, ACF2_GEN_SHELL};
static struct adm_cmd sh_resource_cmd = {"sh-resource", sh_resource, ACF2_SH_RESNAME};
static struct adm_cmd sh_mod_parms_cmd = {"sh-mod-parms", sh_mod_parms, ACF2_GEN_SHELL};
static struct adm_cmd sh_dev_cmd = {"sh-dev", sh_dev, ACF2_SHELL};
static struct adm_cmd sh_udev_cmd = {"sh-udev", sh_udev, .vol_id_required = 1, ACF2_HOOK};
static struct adm_cmd sh_minor_cmd = {"sh-minor", sh_minor, ACF2_SHELL};
static struct adm_cmd sh_ll_dev_cmd = {"sh-ll-dev", sh_ll_dev, ACF2_SHELL .disk_required = 1};
static struct adm_cmd sh_md_dev_cmd = {"sh-md-dev", sh_md_dev, ACF2_SHELL .disk_required = 1};
static struct adm_cmd sh_md_idx_cmd = {"sh-md-idx", sh_md_idx, ACF2_SHELL .disk_required = 1};
static struct adm_cmd sh_ip_cmd = {"sh-ip", sh_ip, ACF2_SHELL};
static struct adm_cmd sh_lr_of_cmd = {"sh-lr-of", sh_lres, ACF2_SHELL};
static struct adm_cmd proxy_up_cmd = {"proxy-up", adm_proxy_up, ACF2_PROXY};
static struct adm_cmd proxy_down_cmd = {"proxy-down", adm_proxy_down, ACF2_PROXY};
/* */ struct adm_cmd new_resource_cmd = {"new-resource", adm_resource, &resource_options_ctx, ACF2_SH_RESNAME};
/* */ struct adm_cmd new_minor_cmd = {"new-minor", adm_new_minor, &device_options_ctx, ACF4_ADVANCED};
/* */ struct adm_cmd del_minor_cmd = {"del-minor", adm_drbdsetup, ACF1_MINOR_ONLY .show_in_usage = 4, .disk_required = 0, };
static struct adm_cmd khelper01_cmd = {"before-resync-target", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper02_cmd = {"after-resync-target", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper03_cmd = {"before-resync-source", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper04_cmd = {"pri-on-incon-degr", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper05_cmd = {"pri-lost-after-sb", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper06_cmd = {"fence-peer", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper07_cmd = {"local-io-error", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper08_cmd = {"pri-lost", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper09_cmd = {"initial-split-brain", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper10_cmd = {"split-brain", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper11_cmd = {"out-of-sync", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper12_cmd = {"unfence-peer", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd khelper13_cmd = {"quorum-lost", adm_khelper, ACF3_RES_HANDLER};
static struct adm_cmd suspend_io_cmd = {"suspend-io", adm_drbdsetup, ACF4_ADVANCED .backend_res_name = 0 };
static struct adm_cmd resume_io_cmd = {"resume-io", adm_drbdsetup, ACF4_ADVANCED .backend_res_name = 0 };
static struct adm_cmd set_gi_cmd = {"set-gi", adm_drbdmeta, &wildcard_ctx, .disk_required = 1, .need_peer = 1, ACF4_ADVANCED_NEED_VOL};
static struct adm_cmd new_current_uuid_cmd = {"new-current-uuid", adm_drbdsetup, &new_current_uuid_cmd_ctx, ACF4_ADVANCED_NEED_VOL .backend_res_name = 0};
static struct adm_cmd check_resize_cmd = {"check-resize", adm_chk_resize, ACF4_ADVANCED};
struct adm_cmd *cmds[] = {
/* name, function, flags
* sort order:
* - normal config commands,
* - normal meta data manipulation
* - sh-*
* - handler
* - advanced
***/
&attach_cmd,
&disk_options_cmd,
&detach_cmd,
&new_peer_cmd,
&del_peer_cmd,
&new_path_cmd,
&del_path_cmd,
&connect_cmd,
&net_options_cmd,
&disconnect_cmd,
&up_cmd,
&res_options_cmd,
&peer_device_options_cmd,
&down_cmd,
&primary_cmd,
&secondary_cmd,
&invalidate_cmd,
&invalidate_remote_cmd,
&outdate_cmd,
&resize_cmd,
&verify_cmd,
&pause_sync_cmd,
&resume_sync_cmd,
&adjust_cmd,
&adjust_wp_cmd,
&wait_c_cmd,
&wait_sync_cmd,
&wait_ci_cmd,
&role_cmd,
&cstate_cmd,
&dstate_cmd,
&status_cmd,
&dump_cmd,
&dump_xml_cmd,
&create_md_cmd,
&show_gi_cmd,
&get_gi_cmd,
&dump_md_cmd,
&wipe_md_cmd,
&apply_al_cmd,
&forget_peer_cmd,
&hidden_cmd,
&sh_nop_cmd,
&sh_resources_cmd,
&sh_resource_cmd,
&sh_mod_parms_cmd,
&sh_dev_cmd,
&sh_udev_cmd,
&sh_minor_cmd,
&sh_ll_dev_cmd,
&sh_md_dev_cmd,
&sh_md_idx_cmd,
&sh_ip_cmd,
&sh_lr_of_cmd,
&proxy_up_cmd,
&proxy_down_cmd,
&new_resource_cmd,
&new_minor_cmd,
&del_minor_cmd,
&khelper01_cmd,
&khelper02_cmd,
&khelper03_cmd,
&khelper04_cmd,
&khelper05_cmd,
&khelper06_cmd,
&khelper07_cmd,
&khelper08_cmd,
&khelper09_cmd,
&khelper10_cmd,
&khelper11_cmd,
&khelper12_cmd,
&khelper13_cmd,
&suspend_io_cmd,
&resume_io_cmd,
&set_gi_cmd,
&new_current_uuid_cmd,
&check_resize_cmd,
};
/* internal commands: */
/* */ struct adm_cmd res_options_defaults_cmd = {
"resource-options",
adm_resource,
&resource_options_ctx,
ACF1_RESNAME
};
/* */ struct adm_cmd disk_options_defaults_cmd = {
"disk-options",
adm_attach,
&attach_cmd_ctx,
ACF1_MINOR_ONLY
};
/* */ struct adm_cmd net_options_defaults_cmd = {
"net-options",
adm_new_peer,
&net_options_ctx,
ACF1_CONNECT
};
/* */ struct adm_cmd peer_device_options_defaults_cmd = {
"peer-device-options",
adm_peer_device,
&peer_device_options_ctx,
ACF1_CONNECT
};
/* */ struct adm_cmd proxy_conn_down_cmd = { "", do_proxy_conn_down, ACF2_PROXY };
/* */ struct adm_cmd proxy_conn_up_cmd = { "", do_proxy_conn_up, ACF2_PROXY };
/* */ struct adm_cmd proxy_conn_plugins_cmd = { "", do_proxy_conn_plugins, ACF2_PROXY };
static const struct adm_cmd invalidate_setup_cmd = {
"invalidate",
__adm_drbdsetup_silent,
ACF1_MINOR_ONLY
};
static const struct adm_cmd forget_peer_setup_cmd = {
"forget-peer",
__adm_drbdsetup_silent,
ACF1_DISCONNECT
};
static void initialize_deferred_cmds()
{
enum drbd_cfg_stage stage;
for (stage = CFG_PREREQ; stage < __CFG_LAST; stage++)
STAILQ_INIT(&deferred_cmds[stage]);
}
void schedule_deferred_cmd(struct adm_cmd *cmd,
const struct cfg_ctx *ctx,
enum drbd_cfg_stage stage)
{
struct deferred_cmd *d;
if (stage & SCHEDULE_ONCE) {
stage &= ~SCHEDULE_ONCE;
STAILQ_FOREACH(d, &deferred_cmds[stage], link) {
if (d->ctx.cmd == cmd &&
d->ctx.res == ctx->res &&
d->ctx.conn == ctx->conn &&
d->ctx.vol->vnr == ctx->vol->vnr)
return;
}
}
d = calloc(1, sizeof(struct deferred_cmd));
if (d == NULL) {
perror("calloc");
exit(E_EXEC_ERROR);
}
d->ctx = *ctx;
d->ctx.cmd = cmd;
STAILQ_INSERT_TAIL(&deferred_cmds[stage], d, link);
}
enum on_error { KEEP_RUNNING, EXIT_ON_FAIL };
static int __call_cmd_fn(const struct cfg_ctx *ctx, enum on_error on_error)
{
struct d_volume *vol = ctx->vol;
bool iterate_paths;
int rv = 0;
iterate_paths = ctx->path ? 0 : ctx->cmd->iterate_paths;
if (ctx->cmd->disk_required &&
(!vol->disk || !vol->meta_disk || !vol->meta_index)) {
rv = 10;
err("The %s command requires a local disk, but the configuration gives none.\n",
ctx->cmd->name);
if (on_error == EXIT_ON_FAIL)
exit(rv);
return rv;
}
if (iterate_paths) {
struct cfg_ctx tmp_ctx = *ctx;
struct path *path;
for_each_path(path, &tmp_ctx.conn->paths) {
tmp_ctx.path = path;
rv = tmp_ctx.cmd->function(&tmp_ctx);
if (rv >= 20) {
if (on_error == EXIT_ON_FAIL)
exit(rv);
}
}
} else {
rv = ctx->cmd->function(ctx);
if (rv >= 20) {
if (on_error == EXIT_ON_FAIL)
exit(rv);
}
}
return rv;
}
static int call_cmd_fn(struct adm_cmd *cmd, const struct cfg_ctx *ctx, enum on_error on_error)
{
struct cfg_ctx tmp_ctx = *ctx;
tmp_ctx.cmd = cmd;
return __call_cmd_fn(&tmp_ctx, on_error);
}
/* If ctx->vol is NULL, and cmd->iterate_volumes is set,
* iterate over all volumes in ctx->res.
* Else, just pass it on.
* */
int call_cmd(const struct adm_cmd *cmd, const struct cfg_ctx *ctx,
enum on_error on_error)
{
struct cfg_ctx tmp_ctx = *ctx;
struct d_resource *res = ctx->res;
struct d_volume *vol;
struct connection *conn;
bool iterate_vols, iterate_conns;
int ret = 0;
if (!res->peers_addrs_set && cmd->need_peer)
set_peer_in_resource(res, cmd->need_peer);
iterate_vols = ctx->vol ? 0 : cmd->iterate_volumes;
iterate_conns = ctx->conn ? 0 : cmd->need_peer;
tmp_ctx.cmd = cmd;
if (iterate_vols && iterate_conns) {
for_each_volume(vol, &res->me->volumes) {
tmp_ctx.vol = vol;
for_each_connection(conn, &res->connections) {
if (conn->ignore)
continue;
tmp_ctx.conn = conn;
ret = __call_cmd_fn(&tmp_ctx, on_error);
if (ret)
goto out;
}
}
} else if (iterate_vols) {
for_each_volume(vol, &res->me->volumes) {
tmp_ctx.vol = vol;
ret = __call_cmd_fn(&tmp_ctx, on_error);
if (ret)
break;
}
} else if (iterate_conns) {
for_each_connection(conn, &res->connections) {
if (conn->ignore)
continue;
tmp_ctx.conn = conn;
ret = __call_cmd_fn(&tmp_ctx, on_error);
if (ret)
break;
}
} else {
ret = __call_cmd_fn(&tmp_ctx, on_error);
}
out:
return ret;
}
static char *drbd_cfg_stage_string[] = {
[CFG_PREREQ] = "create res",
[CFG_RESOURCE] = "adjust res",
[CFG_DISK_PREP_DOWN] = "prepare disk",
[CFG_DISK_PREP_UP] = "prepare disk",
[CFG_DISK] = "adjust disk",
[CFG_NET_DISCONNECT] = "prepare net",
[CFG_NET_PREP_DOWN] = "prepare net",
[CFG_NET_PREP_UP] = "prepare net",
[CFG_NET] = "adjust net",
[CFG_PEER_DEVICE] = "adjust peer_devices",
[CFG_NET_CONNECT] = "attempt to connect",
};
int _run_deferred_cmds(enum drbd_cfg_stage stage)
{
struct d_resource *last_res = NULL;
struct deferred_cmd *d = STAILQ_FIRST(&deferred_cmds[stage]);
struct deferred_cmd *t;
int r;
int rv = 0;
if (d && adjust_with_progress) {
printf("\n%15s:", drbd_cfg_stage_string[stage]);
fflush(stdout);
}
while (d) {
if (d->ctx.res->skip_further_deferred_command) {
if (adjust_with_progress) {
if (d->ctx.res != last_res)
printf(" [skipped:%s]", d->ctx.res->name);
} else
err("%s: %s %s: skipped due to earlier error\n",
progname, d->ctx.cmd->name, d->ctx.res->name);
r = 0;
} else {
if (adjust_with_progress) {
if (d->ctx.res != last_res)
printf(" %s", d->ctx.res->name);
}
r = __call_cmd_fn(&d->ctx, KEEP_RUNNING);
if (r) {
/* If something in the "prerequisite" stages failed,
* there is no point in trying to continue.
* However if we just failed to adjust some
* options, or failed to attach, we still want
* to adjust other options, or try to connect.
*/
if (stage == CFG_PREREQ
|| stage == CFG_DISK_PREP_DOWN
|| stage == CFG_DISK_PREP_UP
|| stage == CFG_NET_PREP_DOWN
|| stage == CFG_NET_PREP_UP)
d->ctx.res->skip_further_deferred_command = 1;
if (adjust_with_progress)
printf(":failed(%s:%u)", d->ctx.cmd->name, r);
}
}
last_res = d->ctx.res;
t = STAILQ_NEXT(d, link);
free(d);
d = t;
if (r > rv)
rv = r;
}
return rv;
}
int run_deferred_cmds(void)
{
enum drbd_cfg_stage stage;
int r;
int ret = 0;
if (adjust_with_progress)
printf("[");
for (stage = CFG_PREREQ; stage < __CFG_LAST; stage++) {
r = _run_deferred_cmds(stage);
if (r) {
if (!adjust_with_progress)
return 1; /* FIXME r? */
ret = 1;
}
}
if (adjust_with_progress)
printf("\n]\n");
return ret;
}
static int adm_adjust(const struct cfg_ctx *ctx)
{
static int adjust_flags = 0;
struct d_name *opt;
if (!adjust_flags) {
opt = find_backend_option("--skip-disk");
if (opt)
STAILQ_REMOVE(&backend_options, opt, d_name, link);
else
adjust_flags |= ADJUST_DISK;
opt = find_backend_option("--skip-net");
if (opt)
STAILQ_REMOVE(&backend_options, opt, d_name, link);
else
adjust_flags |= ADJUST_NET;
adjust_flags |= ADJUST_SKIP_CHECKED;
}
return _adm_adjust(ctx, adjust_flags);
}
static int sh_nop(const struct cfg_ctx *ctx)
{
if (!config_valid)
return 10;
return 0;
}
static int sh_resources(const struct cfg_ctx *ctx)
{
struct d_resource *res;
for_each_resource(res, &config) {
if (res->ignore)
continue;
if (is_drbd_top != res->stacked)
continue;
printf("%s\n", res->name);
}
return 0;
}
static int sh_resource(const struct cfg_ctx *ctx)
{
printf("%s\n", ctx->res->name);
return 0;
}
static int sh_dev(const struct cfg_ctx *ctx)
{
printf("%s\n", ctx->vol->device);
return 0;
}
static int sh_udev(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct d_volume *vol = ctx->vol;
/* No shell escape necessary. Udev does not handle it anyways... */
if (!vol) {
err("volume not specified\n");
return 1;
}
if (!strncmp(vol->device, "/dev/drbd", 9))
printf("DEVICE=%s\n", vol->device + 5);
else
printf("DEVICE=drbd%u\n", vol->device_minor);
/* in case older udev rules are still in place,
* but do not yet have the work-around for the
* udev default change of "string_escape=none" -> "replace",
* populate plain "SYMLINK" with just the "by-res" one. */
printf("SYMLINK=");
if (vol->implicit && !global_options.udev_always_symlink_vnr)
printf("drbd/by-res/%s\n", res->name);
else
printf("drbd/by-res/%s/%u\n", res->name, vol->vnr);
/* repeat, with _BY_RES */
printf("SYMLINK_BY_RES=");
if (vol->implicit && !global_options.udev_always_symlink_vnr)
printf("drbd/by-res/%s\n", res->name);
else
printf("drbd/by-res/%s/%u\n", res->name, vol->vnr);
/* and add the _BY_DISK one explicitly */
if (vol->disk) {
printf("SYMLINK_BY_DISK=");
if (!strncmp(vol->disk, "/dev/", 5))
printf("drbd/by-disk/%s\n", vol->disk + 5);
else
printf("drbd/by-disk/%s\n", vol->disk);
}
return 0;
}
static int sh_minor(const struct cfg_ctx *ctx)
{
printf("%d\n", ctx->vol->device_minor);
return 0;
}
static int sh_ip(const struct cfg_ctx *ctx)
{
printf("%s\n", ctx->res->me->address.addr);
return 0;
}
static int sh_lres(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
if (!is_drbd_top) {
err("sh-lower-resource only available in stacked mode\n");
exit(E_USAGE);
}
if (!res->stacked) {
err("'%s' is not stacked on this host (%s)\n", res->name, hostname);
exit(E_USAGE);
}
printf("%s\n", res->me->lower->name);
return 0;
}
static int sh_ll_dev(const struct cfg_ctx *ctx)
{
printf("%s\n", ctx->vol->disk);
return 0;
}
static int sh_md_dev(const struct cfg_ctx *ctx)
{
struct d_volume *vol = ctx->vol;
char *r;
if (strcmp("internal", vol->meta_disk) == 0)
r = vol->disk;
else
r = vol->meta_disk;
printf("%s\n", r);
return 0;
}
static int sh_md_idx(const struct cfg_ctx *ctx)
{
printf("%s\n", ctx->vol->meta_index);
return 0;
}
/* FIXME this module parameter will go */
static int sh_mod_parms(const struct cfg_ctx *ctx)
{
int mc = global_options.minor_count;
if (mc == 0) {
mc = number_of_minors + 3;
if (mc > DRBD_MINOR_COUNT_MAX)
mc = DRBD_MINOR_COUNT_MAX;
if (mc < DRBD_MINOR_COUNT_DEF)
mc = DRBD_MINOR_COUNT_DEF;
}
printf("minor_count=%d\n", mc);
return 0;
}
static void free_volume(struct d_volume *vol)
{
if (!vol)
return;
free(vol->device);
free(vol->disk);
free(vol->meta_disk);
free(vol->meta_index);
free(vol);
}
static void free_host_info(struct d_host_info *hi)
{
struct d_volume *vol, *n;
if (!hi)
return;
free_names(&hi->on_hosts);
vol = STAILQ_FIRST(&hi->volumes);
while (vol) {
n = STAILQ_NEXT(vol, link);
free_volume(vol);
vol = n;
}
free(hi->address.addr);
free(hi->address.af);
free(hi->address.port);
}
static void free_options(struct options *options)
{
struct d_option *f, *option = STAILQ_FIRST(options);
while (option) {
free(option->value);
f = option;
option = STAILQ_NEXT(option, link);
free(f);
}
}
static void free_config()
{
struct d_resource *f, *t;
struct d_host_info *host, *th;
f = STAILQ_FIRST(&config);
while (f) {
free(f->name);
host = STAILQ_FIRST(&f->all_hosts);
while (host) {
th = STAILQ_NEXT(host, link);
free_host_info(host);
host = th;
}
free_options(&f->net_options);
free_options(&f->disk_options);
free_options(&f->startup_options);
free_options(&f->proxy_options);
free_options(&f->handlers);
t = STAILQ_NEXT(f, link);
free(f);
f = t;
}
if (common) {
free_options(&common->net_options);
free_options(&common->disk_options);
free_options(&common->startup_options);
free_options(&common->proxy_options);
free_options(&common->handlers);
free(common);
}
free(ifreq_list);
}
static void find_drbdcmd(char **cmd, char **pathes)
{
char **path;
path = pathes;
while (*path) {
if (access(*path, X_OK) == 0) {
*cmd = *path;
return;
}
path++;
}
err("Can not find command (drbdsetup/drbdmeta)\n");
exit(E_EXEC_ERROR);
}
#define NA(ARGC) \
({ if((ARGC) >= MAX_ARGS) { err("MAX_ARGS too small\n"); \
exit(E_THINKO); \
} \
(ARGC)++; \
})
static bool is_valid_backend_option(const char* name, const struct context_def *context_def)
{
const struct field_def *field;
int len_to_equal_sign_or_nul;
if (context_def == &wildcard_ctx)
return true;
if (!context_def || strlen(name) <= 2)
return false;
/* options have a leading "--", while field names do not have that */
name += 2;
/* compare only until first equal sign, if any */
len_to_equal_sign_or_nul = strcspn(name, "=");
for (field = context_def->fields; field->name; field++) {
if (!strncmp(name, field->name, len_to_equal_sign_or_nul))
return true;
}
return false;
}
static void add_setup_options(char **argv, int *argcp, const struct context_def *context_def)
{
struct d_name *b_opt;
int argc = *argcp;
STAILQ_FOREACH(b_opt, &backend_options, link) {
if (is_valid_backend_option(b_opt->name, context_def))
argv[NA(argc)] = b_opt->name;
}
*argcp = argc;
}
#define make_option(ARG, OPT) do { \
struct d_name *b_opt; \
bool found = false; \
STAILQ_FOREACH(b_opt, &backend_options, link) { \
if (!strncmp(OPT->name, b_opt->name+2, strlen(OPT->name))) { \
found = true; \
break; \
} \
} \
if (!found) { \
if(OPT->value) \
ARG = ssprintf("--%s=%s", OPT->name, OPT->value); \
else \
ARG = ssprintf("--%s", OPT->name); \
} \
} while (0)
#define make_options(ARG, OPTIONS) do { \
struct d_option *option; \
STAILQ_FOREACH(option, OPTIONS, link) \
make_option(ARG, option); \
} while (0)
#define ssprintf_addr(A) \
ssprintf(strcmp((A)->af, "ipv6") ? "%s:%s:%s" : "%s:[%s]:%s", \
(A)->af, (A)->addr, (A)->port);
static int adm_attach(const struct cfg_ctx *ctx)
{
struct d_volume *vol = ctx->vol;
char *argv[MAX_ARGS];
int argc = 0;
bool do_attach = (ctx->cmd == &attach_cmd);
bool reset = (ctx->cmd == &disk_options_defaults_cmd);
if (do_attach) {
int rv = call_cmd_fn(&apply_al_cmd, ctx, KEEP_RUNNING);
if (rv)
return rv;
}
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* "attach" : "disk-options"; */
argv[NA(argc)] = ssprintf("%d", vol->device_minor);
if (do_attach) {
assert(vol->disk != NULL);
assert(vol->disk[0] != '\0');
argv[NA(argc)] = vol->disk;
if (!strcmp(vol->meta_disk, "internal")) {
argv[NA(argc)] = vol->disk;
} else {
argv[NA(argc)] = vol->meta_disk;
}
argv[NA(argc)] = vol->meta_index;
}
if (reset)
argv[NA(argc)] = "--set-defaults";
if (reset || do_attach) {
if (!do_attach) {
struct d_option *option;
STAILQ_FOREACH(option, &ctx->vol->disk_options, link)
if (!option->adj_skip)
make_option(argv[NA(argc)], option);
} else {
make_options(argv[NA(argc)], &ctx->vol->disk_options);
}
}
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, SLEEPS_LONG, ctx->res->name);
}
struct d_option *find_opt(struct options *base, const char *name)
{
struct d_option *option;
STAILQ_FOREACH(option, base, link)
if (!strcmp(option->name, name))
return option;
return NULL;
}
bool del_opt(struct options *base, const char * const name)
{
struct d_option *opt;
if ((opt = find_opt(base, name))) {
STAILQ_REMOVE(base, opt, d_option, link);
free_opt(opt);
return true;
}
return false;
}
int adm_new_minor(const struct cfg_ctx *ctx)
{
char *argv[MAX_ARGS];
int argc = 0, ex;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = "new-minor";
argv[NA(argc)] = ssprintf("%s", ctx->res->name);
argv[NA(argc)] = ssprintf("%u", ctx->vol->device_minor);
argv[NA(argc)] = ssprintf("%u", ctx->vol->vnr);
if (!ctx->vol->disk)
argv[NA(argc)] = ssprintf("--diskless");
argv[NA(argc)] = NULL;
ex = m_system_ex(argv, SLEEPS_SHORT, ctx->res->name);
if (!ex && do_register)
register_minor(ctx->vol->device_minor, config_save);
return ex;
}
static int adm_resource(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
char *argv[MAX_ARGS];
int argc = 0, ex;
bool do_new_resource = (ctx->cmd == &new_resource_cmd);
bool reset = (ctx->cmd == &res_options_defaults_cmd);
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* "new-resource" or "resource-options" */
argv[NA(argc)] = ssprintf("%s", res->name);
if (do_new_resource)
argv[NA(argc)] = ctx->res->me->node_id;
if (reset)
argv[NA(argc)] = "--set-defaults";
if (reset || do_new_resource)
make_options(argv[NA(argc)], &res->res_options);
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = NULL;
ex = m_system_ex(argv, SLEEPS_SHORT, res->name);
if (!ex && do_new_resource && do_register)
register_resource(res->name, config_save);
return ex;
}
static off64_t read_drbd_dev_size(int minor)
{
char *path;
FILE *file;
off64_t val;
int r;
m_asprintf(&path, "/sys/block/drbd%d/size", minor);
file = fopen(path, "r");
if (file) {
r = fscanf(file, "%" SCNd64, &val);
fclose(file);
if (r != 1)
val = -1;
} else
val = -1;
return val;
}
int adm_resize(const struct cfg_ctx *ctx)
{
char *argv[MAX_ARGS];
struct d_option *opt;
bool is_resize = !strcmp(ctx->cmd->name, "resize");
off64_t old_size = -1;
off64_t target_size = 0;
off64_t new_size;
int argc = 0;
int silent;
int ex;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = "resize"; /* first execute resize, even if called from check-resize context */
argv[NA(argc)] = ssprintf("%d", ctx->vol->device_minor);
opt = find_opt(&ctx->vol->disk_options, "size");
if (!opt)
opt = find_opt(&ctx->res->disk_options, "size");
if (opt) {
argv[NA(argc)] = ssprintf("--%s=%s", opt->name, opt->value);
target_size = m_strtoll(opt->value, 's');
/* FIXME: what if "add_setup_options" below overrides target_size
* with an explicit, on-command-line target_size? */
}
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
if (is_resize && !dry_run)
old_size = read_drbd_dev_size(ctx->vol->device_minor);
/* if this is a "resize" triggered by "check-resize", be silent! */
silent = is_resize ? 0 : SUPRESS_STDERR;
ex = m_system_ex(argv, SLEEPS_LONG | silent, ctx->res->name);
if (ex && target_size) {
new_size = read_drbd_dev_size(ctx->vol->device_minor);
if (new_size == target_size) {
fprintf(stderr, "Current size of drbd%u equals target size (%llu byte), exit code %d ignored.\n",
ctx->vol->device_minor, (unsigned long long)new_size, ex);
ex = 0;
}
}
if (ex)
return ex;
/* Record last-known bdev info.
* Unfortunately drbdsetup did not have enough information
* when doing the "resize", and in theory, _our_ information
* about the backing device may even be wrong.
* Call drbdsetup again, tell it to ask the kernel for
* current config, and update the last known bdev info
* according to that. */
/* argv[0] = drbdsetup; */
argv[1] = "check-resize";
/* argv[2] = minor; */
argv[3] = NULL;
/* ignore exit code */
m_system_ex(argv, SLEEPS_SHORT | silent, ctx->res->name);
/* Here comes a really uggly hack. Wait until the device size actually
changed, but only up to 10 seconds if know the target size, up to
3 seconds waiting for some change. */
if (old_size > 0) {
int timeo = target_size ? 100 : 30;
do {
new_size = read_drbd_dev_size(ctx->vol->device_minor);
if (new_size >= target_size) /* should be == , but driver ignores usize right now */
return 0;
if (new_size != old_size) {
if (target_size == 0)
return 0;
err("Size changed from %"PRId64" to %"PRId64", waiting for %"PRId64".\n",
old_size, new_size, target_size);
old_size = new_size; /* I want to see it only once.*/
}
usleep(100000);
} while (timeo-- > 0);
return 1;
}
return 0;
}
int _adm_drbdmeta(const struct cfg_ctx *ctx, int flags, char *argument)
{
struct d_volume *vol = ctx->vol;
char *argv[MAX_ARGS];
int argc = 0;
argv[NA(argc)] = drbdmeta;
argv[NA(argc)] = ssprintf("%d", vol->device_minor);
argv[NA(argc)] = "v09";
if (!strcmp(vol->meta_disk, "internal")) {
assert(vol->disk != NULL);
assert(vol->disk[0] != '\0');
argv[NA(argc)] = vol->disk;
} else {
argv[NA(argc)] = vol->meta_disk;
}
if (!strcmp(vol->meta_index, "flexible")) {
if (!strcmp(vol->meta_disk, "internal")) {
argv[NA(argc)] = "flex-internal";
} else {
argv[NA(argc)] = "flex-external";
}
} else {
argv[NA(argc)] = vol->meta_index;
}
if (ctx->cmd->need_peer)
argv[NA(argc)] = ssprintf("--node-id=%s", ctx->conn->peer->node_id);
argv[NA(argc)] = (char *)ctx->cmd->name;
if (argument)
argv[NA(argc)] = argument;
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, flags, ctx->res->name);
}
static int adm_drbdmeta(const struct cfg_ctx *ctx)
{
return _adm_drbdmeta(ctx, SLEEPS_VERY_LONG, NULL);
}
static void __adm_drbdsetup(const struct cfg_ctx *ctx, int flags, pid_t *pid, int *fd, int *ex)
{
char *argv[MAX_ARGS];
int argc = 0;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name;
if (ctx->cmd->backend_res_name && ctx->res)
argv[NA(argc)] = ssprintf("%s", ctx->res->name);
if (ctx->cmd->need_peer)
argv[NA(argc)] = ssprintf("%s", ctx->conn->peer->node_id);
if (ctx->vol) {
if (ctx->cmd == &detach_cmd && !ctx->vol->device)
argv[NA(argc)] = ssprintf("--diskless");
if (ctx->cmd->need_peer && ctx->cmd->iterate_volumes)
argv[NA(argc)] = ssprintf("%d", ctx->vol->vnr);
else
argv[NA(argc)] = ssprintf("%d", ctx->vol->device_minor);
}
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
if (ctx->cmd == &invalidate_setup_cmd && ctx->conn)
argv[NA(argc)] = ssprintf("--sync-from-peer-node-id=%s", ctx->conn->peer->node_id);
argv[NA(argc)] = 0;
if (ctx->res)
setenv("DRBD_RESOURCE", ctx->res->name, 1);
m__system(argv, flags, ctx->res ? ctx->res->name : NULL, pid, fd, ex);
}
static int _adm_drbdsetup(const struct cfg_ctx *ctx, int flags)
{
int ex;
__adm_drbdsetup(ctx, flags, NULL, NULL, &ex);
return ex;
}
static int adm_drbdsetup(const struct cfg_ctx *ctx)
{
return _adm_drbdsetup(ctx, ctx->cmd->takes_long ? SLEEPS_LONG : SLEEPS_SHORT);
}
static int __adm_drbdsetup_silent(const struct cfg_ctx *ctx)
{
char buffer[4096];
int fd, status, rv = 0;
pid_t pid;
ssize_t rr;
ssize_t rw __attribute((unused));
size_t s = 0;
__adm_drbdsetup(ctx, SLEEPS_SHORT | RETURN_STDERR_FD, &pid, &fd, NULL);
if (!dry_run) {
if (fd < 0) {
err("Strange: got negative fd.\n");
exit(E_THINKO);
}
while (1) {
rr = read(fd, buffer + s, 4096 - s);
if (rr <= 0)
break;
s += rr;
}
close(fd);
(void) waitpid(pid, &status, 0);
alarm(0);
if (WIFEXITED(status))
rv = WEXITSTATUS(status);
if (alarm_raised) {
rv = 0x100;
}
}
/* see drbdsetup.c, print_config_error():
* 11: some unspecific state change error. (ignore for invalidate)
* 17: SS_NO_UP_TO_DATE_DISK */
if ((strcmp(ctx->cmd->name, "invalidate") && rv == 11) || rv == 17)
rw = write(fileno(stderr), buffer, s);
return rv;
}
static int adm_outdate(const struct cfg_ctx *ctx)
{
int rv;
rv = _adm_drbdsetup(ctx, SLEEPS_SHORT | SUPRESS_STDERR);
/* special cases for outdate:
* 17: drbdsetup outdate, but is primary and thus cannot be outdated.
* 5: drbdsetup outdate, and is inconsistent or worse anyways. */
if (rv == 17)
return rv;
if (rv == 5) {
/* That might mean it is diskless. */
rv = adm_drbdmeta(ctx);
if (rv)
rv = 5;
return rv;
}
if (rv || dry_run) {
rv = adm_drbdmeta(ctx);
}
return rv;
}
/* shell equivalent:
* ( drbdsetup resize && drbdsetup check-resize ) || drbdmeta check-resize */
static int adm_chk_resize(const struct cfg_ctx *ctx)
{
/* drbdsetup resize && drbdsetup check-resize */
int ex = adm_resize(ctx);
if (ex == 0)
return 0;
/* try drbdmeta check-resize */
return adm_drbdmeta(ctx);
}
static int adm_setup_and_meta(const struct cfg_ctx *ctx)
{
int rv;
rv = __adm_drbdsetup_silent(ctx);
if (rv == 11 || rv == 17) {
/* see drbdsetup.c, print_config_error():
* 11: some unspecific state change error. (ignore for invalidate)
* 17: SS_NO_UP_TO_DATE_DISK */
return rv;
}
if (rv || dry_run)
rv = adm_drbdmeta(ctx);
return rv;
}
static int adm_invalidate(const struct cfg_ctx *ctx)
{
static const struct adm_cmd invalidate_meta_cmd = {
"invalidate",
adm_drbdmeta,
ACF1_MINOR_ONLY
};
int rv;
rv = call_cmd(&invalidate_setup_cmd, ctx, KEEP_RUNNING);
if (rv == 11 || rv == 17) {
/* see drbdsetup.c, print_config_error():
* 11: some unspecific state change error.
* Means that there are multiple peers
* 17: SS_NO_UP_TO_DATE_DISK */
return rv;
}
if (rv || dry_run == 1)
rv = call_cmd(&invalidate_meta_cmd, ctx, KEEP_RUNNING);
return rv;
}
static int adm_forget_peer(const struct cfg_ctx *ctx)
{
static const struct adm_cmd forget_peer_meta_cmd = {
"forget-peer",
adm_drbdmeta,
ACF1_PEER_DEVICE .disk_required = 1
};
int rv;
rv = call_cmd(&forget_peer_setup_cmd, ctx, KEEP_RUNNING);
if (rv == 11 || rv == 17)
return rv;
if (rv || dry_run == 1)
rv = call_cmd(&forget_peer_meta_cmd, ctx, KEEP_RUNNING);
return rv;
}
static void setenv_node_id_and_uname(struct d_resource *res)
{
char key[sizeof("DRBD_NODE_ID_32")];
int i;
struct d_host_info *host;
for (i = 0; i < DRBD_NODE_ID_MAX; i++) {
snprintf(key, sizeof(key), "DRBD_NODE_ID_%u", i);
unsetenv(key);
}
for_each_host(host, &res->all_hosts) {
if (!host->node_id)
continue;
/* range check in post parse has already clamped this */
snprintf(key, sizeof(key), "DRBD_NODE_ID_%s", host->node_id);
setenv(key, names_to_str(&host->on_hosts), 1);
}
/* Maybe we will pass it in from kernel some day */
if (!getenv("DRBD_MY_NODE_ID"))
setenv("DRBD_MY_NODE_ID", res->me->node_id, 1);
}
static int adm_khelper(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct d_volume *vol = ctx->vol;
int rv = 0;
char *sh_cmd;
char minor_string[8];
char volume_string[8];
char *argv[] = { "/bin/sh", "-c", NULL, NULL };
setenv("DRBD_CONF", config_save, 1);
setenv("DRBD_RESOURCE", res->name, 1);
setenv_node_id_and_uname(res);
if (vol) {
snprintf(minor_string, sizeof(minor_string), "%u", vol->device_minor);
snprintf(volume_string, sizeof(volume_string), "%u", vol->vnr);
setenv("DRBD_MINOR", minor_string, 1);
setenv("DRBD_VOLUME", volume_string, 1);
setenv("DRBD_LL_DISK", shell_escape(vol->disk ?: "none"), 1);
} else {
char *minor_list;
char *volume_list;
char *ll_list;
char *separator = "";
char *pos_minor;
char *pos_volume;
char *pos_ll;
int volumes = 0;
int minor_len, volume_len, ll_len = 0;
int n;
for_each_volume(vol, &res->me->volumes) {
volumes++;
ll_len += strlen(shell_escape(vol->disk ?: "none")) + 1;
}
/* max minor number is 2**20 - 1, which is 7 decimal digits.
* plus separator respective trailing zero. */
minor_len = volumes * 8 + 1;
volume_len = minor_len;
minor_list = alloca(minor_len);
volume_list = alloca(volume_len);
ll_list = alloca(ll_len);
pos_minor = minor_list;
pos_volume = volume_list;
pos_ll = ll_list;
for_each_volume(vol, &res->me->volumes) {
#define append(name, fmt, v) do { \
n = snprintf(pos_ ## name, name ## _len, "%s" fmt, \
separator, v); \
if (n >= name ## _len) { \
/* "can not happen" */ \
err("buffer too small when generating the " \
#name " list\n"); \
abort(); \
break; \
} \
name ## _len -= n; \
pos_ ## name += n; \
} while (0)
append(minor, "%d", vol->device_minor);
append(volume, "%d", vol->vnr);
append(ll, "%s", shell_escape(vol->disk ?: "none"));
#undef append
separator = " ";
}
setenv("DRBD_MINOR", minor_list, 1);
setenv("DRBD_VOLUME", volume_list, 1);
setenv("DRBD_LL_DISK", ll_list, 1);
}
if ((sh_cmd = get_opt_val(&res->handlers, ctx->cmd->name, NULL))) {
argv[2] = sh_cmd;
rv = m_system_ex(argv, SLEEPS_VERY_LONG, res->name);
}
return rv;
}
int adm_peer_device(const struct cfg_ctx *ctx)
{
bool reset = (ctx->cmd == &peer_device_options_defaults_cmd);
struct d_resource *res = ctx->res;
struct connection *conn = ctx->conn;
struct d_volume *vol = ctx->vol;
struct peer_device *peer_device;
char *argv[MAX_ARGS];
int argc = 0;
peer_device = find_peer_device(conn, vol->vnr);
if (!peer_device) {
err("Could not find peer_device object!\n");
exit(E_THINKO);
}
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* peer-device-options */
argv[NA(argc)] = ssprintf("%s", res->name);
argv[NA(argc)] = ssprintf("%s", conn->peer->node_id);
argv[NA(argc)] = ssprintf("%d", vol->vnr);
if (reset)
argv[NA(argc)] = "--set-defaults";
make_options(argv[NA(argc)], &peer_device->pd_options);
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, SLEEPS_SHORT, res->name);
}
static int adm_connect(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct connection *conn = ctx->conn;
char *argv[MAX_ARGS];
int argc = 0;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* "connect" */
argv[NA(argc)] = ssprintf("%s", res->name);
argv[NA(argc)] = ssprintf("%s", conn->peer->node_id);
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, SLEEPS_SHORT, res->name);
}
static int adm_new_peer(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct connection *conn = ctx->conn;
char *argv[MAX_ARGS];
int argc = 0;
bool reset = (ctx->cmd == &net_options_defaults_cmd);
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* "new-peer", "net-options" */
argv[NA(argc)] = ssprintf("%s", res->name);
argv[NA(argc)] = ssprintf("%s", conn->peer->node_id);
if (reset)
argv[NA(argc)] = "--set-defaults";
if (!strncmp(ctx->cmd->name, "net-options", 11))
del_opt(&conn->net_options, "transport");
make_options(argv[NA(argc)], &conn->net_options);
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, SLEEPS_SHORT, res->name);
}
static int adm_path(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct connection *conn = ctx->conn;
struct path *path = ctx->path;
char *argv[MAX_ARGS];
int argc = 0;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = (char *)ctx->cmd->name; /* add-path, del-path */
argv[NA(argc)] = ssprintf("%s", res->name);
argv[NA(argc)] = ssprintf("%s", conn->peer->node_id);
argv[NA(argc)] = ssprintf_addr(path->my_address);
argv[NA(argc)] = ssprintf_addr(path->connect_to);
add_setup_options(argv, &argc, ctx->cmd->drbdsetup_ctx);
argv[NA(argc)] = 0;
return m_system_ex(argv, SLEEPS_SHORT, res->name);
}
void free_opt(struct d_option *item)
{
free(item->value);
free(item);
}
int _proxy_connect_name_len(const struct d_resource *res, const struct connection *conn)
{
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
return (conn->name ? strlen(conn->name) : strlen(res->name)) +
strlen(names_to_str_c(&path->peer_proxy->on_hosts, '_')) +
strlen(names_to_str_c(&path->my_proxy->on_hosts, '_')) +
3 /* for the two dashes and the trailing 0 character */;
}
char *_proxy_connection_name(char *conn_name, const struct d_resource *res, const struct connection *conn)
{
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
sprintf(conn_name, "%s-%s-%s",
conn->name ?: res->name,
names_to_str_c(&path->peer_proxy->on_hosts, '_'),
names_to_str_c(&path->my_proxy->on_hosts, '_'));
return conn_name;
}
static int do_proxy_conn_up(const struct cfg_ctx *ctx)
{
char *argv[4] = { drbd_proxy_ctl, "-c", NULL, NULL };
struct connection *conn = ctx->conn;
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
char *conn_name;
if (!path->my_proxy || !path->peer_proxy)
return 0;
conn_name = proxy_connection_name(ctx->res, conn);
argv[2] = ssprintf(
"add connection %s %s:%s %s:%s %s:%s %s:%s",
conn_name,
path->my_proxy->inside.addr,
path->my_proxy->inside.port,
path->peer_proxy->outside.addr,
path->peer_proxy->outside.port,
path->my_proxy->outside.addr,
path->my_proxy->outside.port,
path->my_address->addr,
path->my_address->port);
return m_system_ex(argv, SLEEPS_SHORT, ctx->res->name);
}
static int do_proxy_conn_plugins(const struct cfg_ctx *ctx)
{
struct connection *conn = ctx->conn;
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
char *argv[MAX_ARGS];
char *conn_name;
int argc = 0;
struct d_option *opt;
int counter = 0;
if (!path->my_proxy || !path->peer_proxy)
return 0;
conn_name = proxy_connection_name(ctx->res, conn);
argc = 0;
argv[NA(argc)] = drbd_proxy_ctl;
STAILQ_FOREACH(opt, &path->my_proxy->options, link) {
argv[NA(argc)] = "-c";
argv[NA(argc)] = ssprintf("set %s %s %s",
opt->name, conn_name, opt->value);
}
/* Don't send the "set plugin ... END" line if no plugins are defined
* - that's incompatible with the drbd proxy version 1. */
if (!STAILQ_EMPTY(&path->my_proxy->plugins)) {
STAILQ_FOREACH(opt, &path->my_proxy->plugins, link) {
argv[NA(argc)] = "-c";
argv[NA(argc)] = ssprintf("set plugin %s %d %s",
conn_name, counter, opt->name);
counter++;
}
argv[NA(argc)] = ssprintf("set plugin %s %d END", conn_name, counter);
}
argv[NA(argc)] = 0;
return argc > 2 ? m_system_ex(argv, SLEEPS_SHORT, ctx->res->name) : 0;
}
static int do_proxy_conn_down(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct connection *conn = ctx->conn;
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
char *conn_name;
char *argv[4] = { drbd_proxy_ctl, "-c", NULL, NULL};
if (!path->my_proxy || !path->peer_proxy)
return 0;
conn_name = proxy_connection_name(ctx->res, conn);
argv[2] = ssprintf("del connection %s", conn_name);
return m_system_ex(argv, SLEEPS_SHORT, res->name);
}
static int check_proxy(const struct cfg_ctx *ctx, int do_up)
{
struct connection *conn = ctx->conn;
struct path *path = STAILQ_FIRST(&conn->paths); /* multiple paths via proxy, later! */
int rv;
if (STAILQ_NEXT(path, link)) {
err("Multiple paths in connection within proxy setup not allowed\n");
exit(E_CONFIG_INVALID);
}
if (!path->my_proxy) {
if (conn->on_cmdline) {
err("%s:%d: In resource '%s', no proxy config for connection %sfrom '%s' to '%s'%s.\n",
ctx->res->config_file, conn->config_line, ctx->res->name,
conn->name ? ssprintf("'%s' (", conn->name) : "",
hostname,
names_to_str(&conn->peer->on_hosts),
conn->name ? ")" : "");
exit(E_CONFIG_INVALID);
}
return 0;
}
if (!hostname_in_list(hostname, &path->my_proxy->on_hosts)) {
if (conn->on_cmdline) {
err("The proxy config in resource %s is not for %s.\n",
ctx->res->name, hostname);
exit(E_CONFIG_INVALID);
}
return 0;
}
if (!path->peer_proxy) {
if (conn->on_cmdline) {
err("There is no proxy config for the peer in resource %s.\n",
ctx->res->name);
exit(E_CONFIG_INVALID);
}
return 0;
}
if (do_up) {
rv = do_proxy_conn_up(ctx);
if (!rv)
rv = do_proxy_conn_plugins(ctx);
}
else
rv = do_proxy_conn_down(ctx);
return rv;
}
static int adm_proxy_up(const struct cfg_ctx *ctx)
{
return check_proxy(ctx, 1);
}
static int adm_proxy_down(const struct cfg_ctx *ctx)
{
return check_proxy(ctx, 0);
}
/* The "main" loop iterates over resources.
* This "sorts" the drbdsetup commands to bring those up
* so we will later first create all objects,
* then attach all local disks,
* adjust various settings,
* and then configure the network part */
static int adm_up(const struct cfg_ctx *ctx)
{
struct cfg_ctx tmp_ctx = *ctx;
struct connection *conn;
struct d_volume *vol;
schedule_deferred_cmd(&new_resource_cmd, ctx, CFG_PREREQ);
set_peer_in_resource(ctx->res, true);
for_each_connection(conn, &ctx->res->connections) {
struct peer_device *peer_device;
if (conn->ignore)
continue;
tmp_ctx.conn = conn;
schedule_deferred_cmd(&new_peer_cmd, &tmp_ctx, CFG_NET_PREP_UP);
schedule_deferred_cmd(&new_path_cmd, &tmp_ctx, CFG_NET_PATH);
schedule_deferred_cmd(&connect_cmd, &tmp_ctx, CFG_NET_CONNECT);
STAILQ_FOREACH(peer_device, &conn->peer_devices, connection_link) {
struct cfg_ctx tmp2_ctx;
if (STAILQ_EMPTY(&peer_device->pd_options))
continue;
tmp2_ctx = tmp_ctx;
tmp2_ctx.vol = volume_by_vnr(&conn->peer->volumes, peer_device->vnr);
schedule_deferred_cmd(&peer_device_options_cmd, &tmp2_ctx, CFG_PEER_DEVICE);
}
}
tmp_ctx.conn = NULL;
for_each_volume(vol, &ctx->res->me->volumes) {
tmp_ctx.vol = vol;
schedule_deferred_cmd(&new_minor_cmd, &tmp_ctx, CFG_DISK_PREP_UP);
if (vol->disk)
schedule_deferred_cmd(&attach_cmd, &tmp_ctx, CFG_DISK);
}
return 0;
}
/* The stacked-timeouts switch in the startup sections allows us
to enforce the use of the specified timeouts instead the use
of a sane value. Should only be used if the third node should
never become primary. */
static int adm_wait_c(const struct cfg_ctx *ctx)
{
struct d_resource *res = ctx->res;
struct d_volume *vol = ctx->vol;
char *argv[MAX_ARGS];
int argc = 0, rv;
argv[NA(argc)] = drbdsetup;
if (ctx->vol && ctx->conn) {
argv[NA(argc)] = ssprintf("%s-%s", ctx->cmd->name, "volume");
argv[NA(argc)] = res->name;
argv[NA(argc)] = ssprintf("%s", ctx->conn->peer->node_id);
argv[NA(argc)] = ssprintf("%d", vol->vnr);
} else if (ctx->conn) {
argv[NA(argc)] = ssprintf("%s-%s", ctx->cmd->name, "connection");
argv[NA(argc)] = res->name;
argv[NA(argc)] = ssprintf("%s", ctx->conn->peer->node_id);
} else {
argv[NA(argc)] = ssprintf("%s-%s", ctx->cmd->name, "resource");
argv[NA(argc)] = res->name;
}
if (is_drbd_top && !res->stacked_timeouts) {
struct d_option *opt;
unsigned long timeout = 20;
if ((opt = find_opt(&res->net_options, "connect-int"))) {
timeout = strtoul(opt->value, NULL, 10);
// one connect-interval? two?
timeout *= 2;
}
argv[argc++] = "--wfc-timeout";
argv[argc] = ssprintf("%lu", timeout);
argc++;
} else
make_options(argv[NA(argc)], &res->startup_options);
argv[NA(argc)] = 0;
rv = m_system_ex(argv, SLEEPS_FOREVER, res->name);
return rv;
}
int ctx_by_minor(struct cfg_ctx *ctx, const char *id)
{
struct d_resource *res;
struct d_volume *vol;
unsigned int mm;
mm = minor_by_id(id);
if (mm == -1U)
return -ENOENT;
for_each_resource(res, &config) {
if (res->ignore)
continue;
for_each_volume(vol, &res->me->volumes) {
if (mm == vol->device_minor) {
ctx->res = res;
ctx->vol = vol;
return 0;
}
}
}
return -ENOENT;
}
struct d_volume *volume_by_vnr(struct volumes *volumes, int vnr)
{
struct d_volume *vol;
for_each_volume(vol, volumes)
if (vnr == vol->vnr)
return vol;
return NULL;
}
/* if there is something to check:
* return true if check succeeds, otherwise false */
static bool set_ignore_flag(struct connection * const conn, checks check, bool ignore)
{
if (ignore == false) {
if (check == WOULD_ENABLE_DISABLED && conn->ignore_tmp)
return false;
else if (check == WOULD_ENABLE_MULTI_TIMES && !conn->ignore_tmp)
return false;
if (check == WOULD_ENABLE_MULTI_TIMES)
conn->ignore_tmp = ignore;
}
if (check == SETUP_MULTI)
conn->ignore = ignore;
return true;
}
int ctx_by_name(struct cfg_ctx *ctx, const char *id, checks check)
{
struct d_resource *res;
struct d_volume *vol;
struct connection *conn;
char *input = strdupa(id);
char *vol_id;
char *res_name, *conn_or_hostname;
unsigned vol_nr = ~0U;
res_name = input;
vol_id = strrchr(input, '/');
if (vol_id) {
*vol_id++ = '\0';
vol_nr = m_strtoll(vol_id, 0);
}
conn_or_hostname = strchr(input, ':');
if (conn_or_hostname)
*conn_or_hostname++ = '\0';
res = res_by_name(res_name);
if (!res || res->ignore)
return -ENOENT;
ctx->res = res;
set_peer_in_resource(res, 1);
/* resource name only (e.g., r0) and in check state
* this would enable all connectionst that are not ignored */
if (!conn_or_hostname && check == WOULD_ENABLE_MULTI_TIMES)
for_each_connection(conn, &res->connections)
if (!conn->ignore) {
if (!conn->ignore_tmp)
return 1;
else
conn->ignore_tmp = false;
}
if (conn_or_hostname) {
/* per se we do not know if the part after ':' is a host or a connection name */
struct d_host_info *hi;
bool valid_conns = false;
ctx->conn = NULL;
hi = find_host_info_by_name(res, conn_or_hostname);
for_each_connection(conn, &res->connections) {
if (hi) { /* it was host name */
if (res->me == hi) {
err("Host name '%s' (given with --peer option) is not a "
"peer, but the local node\n",
conn_or_hostname);
return -ENOENT;
}
if (conn->peer == hi) {
conn->on_cmdline = 1;
if (check == CTX_FIRST)
goto found;
}
if (conn->peer && !strcmp(conn->peer->node_id, hi->node_id)) {
conn->me = true;
if (!set_ignore_flag(conn, check, false))
return 1;
}
else /* a connection that should be ignored */
set_ignore_flag(conn, check, true);
} else { /* it was a connection name */
struct d_option *opt;
opt = find_opt(&conn->net_options, "_name");
if (opt && !strcmp(opt->value, conn_or_hostname)) {
if (check == CTX_FIRST)
goto found;
conn->me = true;
if (!set_ignore_flag(conn, check, false))
return 1;
}
else { /* a connection that should be ignored */
set_ignore_flag(conn, check, true);
}
}
if (!conn->ignore)
valid_conns = true;
}
if (check == SETUP_MULTI && !valid_conns) {
err("Not a valid connection (%s) for this host\n", id);
return -ENOENT;
}
}
if (check != SETUP_MULTI)
return 0;
if (0) {
found:
if (conn->ignore) {
err("Connection '%s' has the ignore flag set\n",
conn_or_hostname);
return -ENOENT;
}
ctx->conn = conn;
}
if (!vol_id) {
/* We could assign implicit volumes here.
* But that broke "drbdadm up specific-resource".
*/
ctx->vol = NULL;
return 0;
}
vol = volume_by_vnr(&res->me->volumes, vol_nr);
if (vol_nr != ~0U) {
if (vol) {
ctx->vol = vol;
return 0;
} else {
err("Resource '%s' has no volume %d\n", res_name,
vol_nr);
return -ENOENT;
}
}
return -ENOENT;
}
/* In case a child exited, or exits, its return code is stored as
negative number in the pids[i] array */
static int childs_running(pid_t * pids, int opts)
{
int i = 0, wr, rv = 0, status;
int N = nr_volumes[is_drbd_top ? STACKED : NORMAL];
for (i = 0; i < N; i++) {
if (pids[i] <= 0)
continue;
wr = waitpid(pids[i], &status, opts);
if (wr == -1) { // Wait error.
if (errno == ECHILD) {
printf("No exit code for %d\n", pids[i]);
pids[i] = 0; // Child exited before ?
continue;
}
perror("waitpid");
exit(E_EXEC_ERROR);
}
if (wr == 0)
rv = 1; // Child still running.
if (wr > 0) {
pids[i] = 0;
if (WIFEXITED(status))
pids[i] = -WEXITSTATUS(status);
if (WIFSIGNALED(status))
pids[i] = -1000;
}
}
return rv;
}
static void kill_childs(pid_t * pids)
{
int i;
int N = nr_volumes[is_drbd_top ? STACKED : NORMAL];
for (i = 0; i < N; i++) {
if (pids[i] <= 0)
continue;
kill(pids[i], SIGINT);
}
}
/*
returns:
-1 ... all childs terminated
0 ... timeout expired
1 ... a string was read
*/
int gets_timeout(pid_t * pids, char *s, int size, int timeout)
{
int pr, rr, n = 0;
struct pollfd pfd;
if (s) {
pfd.fd = fileno(stdin);
pfd.events = POLLIN | POLLHUP | POLLERR | POLLNVAL;
n = 1;
}
redo_without_fd:
if (!childs_running(pids, WNOHANG)) {
pr = -1;
goto out;
}
do {
pr = poll(&pfd, n, timeout);
if (pr == -1) { // Poll error.
if (errno == EINTR) {
if (childs_running(pids, WNOHANG))
continue;
goto out; // pr = -1 here.
}
perror("poll");
exit(E_EXEC_ERROR);
}
} while (pr == -1);
if (pr == 1 && s) { // Input available and s not NULL.
/* TODO: what should happen if s == NULL? is this correct?
* at least we check here and do not nullptr deref */
rr = read(fileno(stdin), s, size - 1);
if (rr == -1) {
perror("read");
exit(E_EXEC_ERROR);
} else if (size > 1 && rr == 0) {
/* WTF. End-of-file... avoid busy loop. */
s[0] = 0;
n = 0;
goto redo_without_fd;
}
s[rr] = 0;
}
out:
return pr;
}
static char *get_opt_val(struct options *base, const char *name, char *def)
{
struct d_option *option;
option = find_opt(base, name);
return option ? option->value : def;
}
static int check_exit_codes(pid_t * pids)
{
struct d_resource *res;
int i = 0, rv = 0;
for_each_resource(res, &config) {
if (res->ignore)
continue;
if (is_drbd_top != res->stacked)
continue;
if (pids[i] == -5 || pids[i] == -1000) {
pids[i] = 0;
}
if (pids[i] == -20)
rv = 20;
i++;
}
return rv;
}
static int adm_wait_ci(const struct cfg_ctx *ctx)
{
struct d_resource *res;
char *argv[20], answer[40];
pid_t *pids;
int rr, wtime, argc, i = 0;
time_t start;
int saved_stdin, saved_stdout, fd;
int N;
struct sigaction so, sa;
int have_tty = 1;
saved_stdin = -1;
saved_stdout = -1;
if (no_tty) {
err("WARN: stdin/stdout is not a TTY; using /dev/console");
fprintf(stdout,
"WARN: stdin/stdout is not a TTY; using /dev/console");
saved_stdin = dup(fileno(stdin));
if (saved_stdin == -1)
perror("dup(stdin)");
saved_stdout = dup(fileno(stdout));
if (saved_stdin == -1)
perror("dup(stdout)");
fd = open("/dev/console", O_RDONLY);
if (fd == -1) {
perror("open('/dev/console, O_RDONLY)");
have_tty = 0;
} else {
dup2(fd, fileno(stdin));
fd = open("/dev/console", O_WRONLY);
if (fd == -1)
perror("open('/dev/console, O_WRONLY)");
dup2(fd, fileno(stdout));
}
}
sa.sa_handler = chld_sig_hand;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &sa, &so);
N = nr_volumes[is_drbd_top ? STACKED : NORMAL];
pids = alloca(N * sizeof(pid_t));
/* alloca can not fail, it can "only" overflow the stack :)
* but it needs to be initialized anyways! */
memset(pids, 0, N * sizeof(pid_t));
for_each_resource(res, &config) {
if (res->ignore)
continue;
if (is_drbd_top != res->stacked)
continue;
/* ctx is not used */
argc = 0;
argv[NA(argc)] = drbdsetup;
argv[NA(argc)] = "wait-connect-resource";
argv[NA(argc)] = res->name;
make_options(argv[NA(argc)], &res->startup_options);
argv[NA(argc)] = 0;
m__system(argv, RETURN_PID, res->name, &pids[i++], NULL, NULL);
}
wtime = global_options.dialog_refresh ? : -1;
start = time(0);
for (i = 0; i < 10; i++) {
// no string, but timeout
rr = gets_timeout(pids, 0, 0, 1 * 1000);
if (rr < 0)
break;
putchar('.');
fflush(stdout);
check_exit_codes(pids);
}
if (rr == 0) {
/* track a "yes", as well as ctrl-d and ctrl-c,
* in case our tty is stuck in "raw" mode, and
* we get it one character a time (-icanon) */
char yes_string[] = "yes\n";
char *yes_expect = yes_string;
int ctrl_c_count = 0;
int ctrl_d_count = 0;
/* Just in case, if plymouth or usplash is running,
* tell them to step aside.
* Also try to force canonical tty mode. */
printf
("\n***************************************************************\n"
" DRBD's startup script waits for the peer node(s) to appear.\n"
" - If this node was already a degraded cluster before the\n"
" reboot, the timeout is %s seconds. [degr-wfc-timeout]\n"
" - If the peer was available before the reboot, the timeout\n"
" is %s seconds. [wfc-timeout]\n"
" (These values are for resource '%s'; 0 sec -> wait forever)\n",
get_opt_val(&STAILQ_FIRST(&config)->startup_options, "degr-wfc-timeout",
"0"), get_opt_val(&STAILQ_FIRST(&config)->startup_options,
"wfc-timeout", "0"),
STAILQ_FIRST(&config)->name);
if (!have_tty) {
printf(" To abort waiting for DRBD connections, kill this process: kill %u\n", getpid());
fflush(stdout);
/* wait untill killed, or all drbdsetup children have finished. */
do {
rr = poll(NULL, 0, -1);
if (rr == -1) {
if (errno == EINTR) {
if (childs_running(pids, WNOHANG))
continue;
break;
}
perror("poll");
exit(E_EXEC_ERROR);
}
} while (rr != -1);
kill_childs(pids);
childs_running(pids, 0);
check_exit_codes(pids);
return 0;
}
if (system("exec > /dev/null 2>&1; plymouth quit ; usplash_write QUIT ; "
"stty echo icanon icrnl"))
/* Ignore return value. Cannot do anything about it anyways. */;
printf(" To abort waiting enter 'yes' [ -- ]: ");
do {
printf("\e[s\e[31G[%4d]:\e[u", (int)(time(0) - start)); // Redraw sec.
fflush(stdout);
rr = gets_timeout(pids, answer, 40, wtime * 1000);
check_exit_codes(pids);
if (rr != 1)
continue;
/* If our tty is in "sane" or "canonical" mode,
* we get whole lines.
* If it still is in "raw" mode, even though we
* tried to set ICANON above, possibly some other
* "boot splash thingy" is in operation.
* We may be lucky to get single characters.
* If a sysadmin sees things stuck during boot,
* I expect that ctrl-c or ctrl-d will be one
* of the first things that are tried.
* In raw mode, we get these characters directly.
* But I want them to try that three times ;)
*/
if (answer[0] && answer[1] == 0) {
if (answer[0] == '\3')
++ctrl_c_count;
if (answer[0] == '\4')
++ctrl_d_count;
if (yes_expect && answer[0] == *yes_expect)
++yes_expect;
else if (answer[0] == '\n')
yes_expect = yes_string;
else
yes_expect = NULL;
}
if (!strcmp(answer, "yes\n") ||
(yes_expect && *yes_expect == '\0') ||
ctrl_c_count >= 3 ||
ctrl_d_count >= 3) {
kill_childs(pids);
childs_running(pids, 0);
check_exit_codes(pids);
break;
}
printf(" To abort waiting enter 'yes' [ -- ]:");
} while (rr != -1);
printf("\n");
}
if (saved_stdin != -1) {
dup2(saved_stdin, fileno(stdin));
dup2(saved_stdout, fileno(stdout));
}
return 0;
}
static int adm_cmd_cmp(const void *a, const void *b)
{
return strcmp((*(struct adm_cmd **)a)->name,
(*(struct adm_cmd **)b)->name);
}
static void print_cmds(int level)
{
const struct adm_cmd **cmds2;
int i, j, half;
cmds2 = alloca(ARRAY_SIZE(cmds) * sizeof(struct adm_cmd));
for (i = 0, j = 0; i < ARRAY_SIZE(cmds); i++) {
if (cmds[i]->show_in_usage != level)
continue;
cmds2[j++] = cmds[i];
}
qsort(cmds2, j, sizeof(struct adm_cmd *), adm_cmd_cmp);
half = (j + 1) / 2;
for (i = 0; i < half; i++) {
if (i + half < j)
printf(" %-35s %-35s\n",
cmds2[i]->name,
cmds2[i + half]->name);
else
printf(" %-35s\n",
cmds2[i]->name);
}
}
static int hidden_cmds(const struct cfg_ctx *ignored __attribute((unused)))
{
printf("\nThese additional commands might be useful for writing\n"
"nifty shell scripts around drbdadm:\n\n");
print_cmds(2);
printf("\nThese commands are used by the kernel part of DRBD to\n"
"invoke user mode helper programs:\n\n");
print_cmds(3);
printf
("\nThese commands ought to be used by experts and developers:\n\n");
print_cmds(4);
printf("\n");
exit(0);
}
static void field_to_option(const struct field_def *field, struct option *option)
{
option->name = field->name;
option->has_arg = field->argument_is_optional ?
optional_argument : required_argument;
option->flag = NULL;
option->val = 257;
}
static void print_option(struct option *opt)
{
if (opt->has_arg == required_argument) {
printf(" --%s=...", opt->name);
if (opt->val > 1 && opt->val < 256)
printf(", -%c ...", opt->val);
printf("\n");
} else if (opt->has_arg == optional_argument) {
printf(" --%s[=...]", opt->name);
if (opt->val > 1 && opt->val < 256)
printf(", -%c...", opt->val);
printf("\n");
} else {
printf(" --%s", opt->name);
if (opt->val > 1 && opt->val < 256)
printf(", -%c", opt->val);
printf("\n");
}
}
void print_usage_and_exit(struct adm_cmd *cmd, const char *addinfo, int status)
{
struct option *opt;
printf("\nUSAGE: %s %s [OPTION...] {all|RESOURCE...}\n\n"
"GENERAL OPTIONS:\n", progname, cmd ? cmd->name : "COMMAND");
for (opt = general_admopt; opt->name; opt++)
print_option(opt);
if (cmd && cmd->drbdsetup_ctx) {
const struct field_def *field;
printf("\nOPTIONS FOR %s:\n", cmd->name);
for (field = cmd->drbdsetup_ctx->fields; field->name; field++) {
struct option opt;
field_to_option(field, &opt);
print_option(&opt);
}
}
if (!cmd) {
printf("\nCOMMANDS:\n");
print_cmds(1);
}
printf("\nVersion: " PACKAGE_VERSION " (api:%d)\n%s\n",
API_VERSION, drbd_buildtag());
if (addinfo)
printf("\n%s\n", addinfo);
exit(status);
}
void verify_ips(struct d_resource *res)
{
if (global_options.disable_ip_verification)
return;
if ((dry_run == 1 && no_tty) || do_verify_ips == 0)
return;
if (res->ignore)
return;
if (res->stacked && !is_drbd_top)
return;
if (!res->me->address.addr)
return;
if (!have_ip(res->me->address.af, res->me->address.addr)) {
ENTRY *e, *ep, *f;
e = calloc(1, sizeof *e);
if (!e) {
err("calloc: %m\n");
exit(E_EXEC_ERROR);
}
m_asprintf(&e->key, "%s:%s", res->me->address.addr, res->me->address.port);
f = tfind(e, &global_btree, btree_key_cmp);
free(e);
if (f)
ep = *(ENTRY **)f;
err("%s: in resource %s, on %s:\n\t""IP %s not found on this host.\n",
f ? (char *)ep->data : res->config_file, res->name,
names_to_str(&res->me->on_hosts), res->me->address.addr);
if (INVALID_IP_IS_INVALID_CONF)
config_valid = 0;
}
}
static char *conf_file[] = {
DRBD_CONFIG_DIR "/drbd-90.conf",
DRBD_CONFIG_DIR "/drbd-84.conf",
DRBD_CONFIG_DIR "/drbd-83.conf",
DRBD_CONFIG_DIR "/drbd-82.conf",
DRBD_CONFIG_DIR "/drbd-08.conf",
DRBD_CONFIG_DIR "/drbd.conf",
0
};
int pushd(const char *path)
{
int cwd_fd = -1;
cwd_fd = open(".", O_RDONLY | O_CLOEXEC);
if (cwd_fd < 0) {
err("open(\".\") failed: %m\n");
exit(E_USAGE);
}
if (path && path[0] && chdir(path)) {
err("chdir(\"%s\") failed: %m\n", path);
exit(E_USAGE);
}
return cwd_fd;
}
void popd(int fd)
{
if (fd >= 0) {
if (fchdir(fd) < 0) {
err("fchdir() failed: %m\n");
exit(E_USAGE);
}
close(fd);
}
}
/*
* returns a pointer to an malloced area that contains
* an absolute, canonical, version of path.
* aborts if any allocation or syscall fails.
* return value should be free()d, once no longer needed.
*/
char *canonify_path(char *path)
{
int cwd_fd = -1;
char *last_slash;
char *tmp;
char *that_wd;
char *abs_path;
if (!path || !path[0]) {
err("cannot canonify an empty path\n");
exit(E_USAGE);
}
tmp = strdupa(path);
last_slash = strrchr(tmp, '/');
/* Maybe this already is in the top level directory. */
if (last_slash == tmp)
return strdup(path);
if (last_slash) {
*last_slash++ = '\0';
cwd_fd = pushd(tmp);
} else {
last_slash = tmp;
}
that_wd = getcwd(NULL, 0);
if (!that_wd) {
err("getcwd() failed: %m\n");
exit(E_USAGE);
}
/* could have been a symlink to / */
if (!strcmp("/", that_wd))
m_asprintf(&abs_path, "/%s", last_slash);
else
m_asprintf(&abs_path, "%s/%s", that_wd, last_slash);
free(that_wd);
popd(cwd_fd);
return abs_path;
}
void assign_command_names_from_argv0(char **argv)
{
struct cmd_helper {
char *name;
char **var;
};
static struct cmd_helper helpers[] = {
{"drbdsetup", &drbdsetup},
{"drbdmeta", &drbdmeta},
{"drbd-proxy-ctl", &drbd_proxy_ctl},
{"drbdadm-83", &drbdadm_83},
{"drbdadm-84", &drbdadm_84},
{NULL, NULL}
};
struct cmd_helper *c;
/* in case drbdadm is called with an absolute or relative pathname
* look for the drbdsetup binary in the same location,
* otherwise, just let execvp sort it out... */
if ((progname = strrchr(argv[0], '/')) == NULL) {
progname = argv[0];
for (c = helpers; c->name; ++c)
*(c->var) = strdup(c->name);
} else {
size_t len_dir, l;
++progname;
len_dir = progname - argv[0];
for (c = helpers; c->name; ++c) {
l = len_dir + strlen(c->name) + 1;
*(c->var) = malloc(l);
if (*(c->var)) {
strncpy(*(c->var), argv[0], len_dir);
strcpy(*(c->var) + len_dir, c->name);
if (access(*(c->var), X_OK))
strcpy(*(c->var), c->name); /* see add_lib_drbd_to_path() */
}
}
/* for pretty printing, truncate to basename */
argv[0] = progname;
}
}
static void recognize_all_drbdsetup_options(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
const struct adm_cmd *cmd = cmds[i];
const struct field_def *field;
if (!cmd->drbdsetup_ctx)
continue;
for (field = cmd->drbdsetup_ctx->fields; field->name; field++) {
struct option opt;
int n;
field_to_option(field, &opt);
for (n = 0; admopt[n].name; n++) {
if (!strcmp(admopt[n].name, field->name)) {
if (admopt[n].val == 257)
assert (admopt[n].has_arg == opt.has_arg);
else {
err("Warning: drbdsetup %s option --%s "
"can only be passed as -W--%s\n",
cmd->name, admopt[n].name, admopt[n].name);
goto skip;
}
}
}
if (admopt == general_admopt) {
admopt = malloc((n + 2) * sizeof(*admopt));
memcpy(admopt, general_admopt, (n + 1) * sizeof(*admopt));
} else
admopt = realloc(admopt, (n + 2) * sizeof(*admopt));
memcpy(&admopt[n+1], &admopt[n], sizeof(*admopt));
admopt[n] = opt;
skip:
/* dummy statement required because of label */ ;
}
}
}
struct adm_cmd *find_cmd(char *cmdname);
int parse_options(int argc, char **argv, struct adm_cmd **cmd, char ***resource_names)
{
const char *optstring = make_optstring(admopt);
struct names backend_options_check;
struct d_name *b_opt;
int longindex, first_arg_index;
STAILQ_INIT(&backend_options_check);
*cmd = NULL;
*resource_names = calloc(argc + 1, sizeof(char *));
opterr = 1;
optind = 0;
while (1) {
int c;
c = getopt_long(argc, argv, optstring, admopt, &longindex);
if (c == -1)
break;
switch (c) {
case 257: /* drbdsetup option */
{
struct option *option = &admopt[longindex];
char *opt;
if (optarg)
opt = ssprintf("--%s=%s", option->name, optarg);
else
opt = ssprintf("--%s", option->name);
insert_tail(&backend_options_check, names_from_str(opt));
}
break;
case 'S':
is_drbd_top = 1;
break;
case 'v':
verbose++;
break;
case 'd':
dry_run = 1;
break;
case 'c':
if (!strcmp(optarg, "-")) {
yyin = stdin;
if (asprintf(&config_file, "STDIN") < 0) {
err("asprintf(config_file): %m\n");
return 20;
}
config_from_stdin = 1;
} else {
yyin = fopen(optarg, "r");
if (!yyin) {
err("Can not open '%s'.\n.", optarg);
exit(E_EXEC_ERROR);
}
if (asprintf(&config_file, "%s", optarg) < 0) {
err("asprintf(config_file): %m\n");
return 20;
}
}
break;
case 't':
config_test = optarg;
break;
case 'E':
/* Remember as absolute name */
was_file_already_seen(optarg);
break;
case 's':
{
char *pathes[2];
pathes[0] = optarg;
pathes[1] = 0;
find_drbdcmd(&drbdsetup, pathes);
}
break;
case 'm':
{
char *pathes[2];
pathes[0] = optarg;
pathes[1] = 0;
find_drbdcmd(&drbdmeta, pathes);
}
break;
case 'p':
{
char *pathes[2];
pathes[0] = optarg;
pathes[1] = 0;
find_drbdcmd(&drbd_proxy_ctl, pathes);
}
break;
case 'n':
{
char *c;
int shell_var_name_ok = 1;
for (c = optarg; *c && shell_var_name_ok; c++) {
switch (*c) {
case 'a'...'z':
case 'A'...'Z':
case '0'...'9':
case '_':
break;
default:
shell_var_name_ok = 0;
}
}
if (shell_var_name_ok)
sh_varname = optarg;
else
err("ignored --sh-varname=%s: "
"contains suspect characters, allowed set is [a-zA-Z0-9_]\n",
optarg);
}
break;
case 'V':
printf("DRBDADM_BUILDTAG=%s\n", shell_escape(drbd_buildtag()));
printf("DRBDADM_API_VERSION=%u\n", API_VERSION);
printf("DRBD_KERNEL_VERSION_CODE=0x%06x\n", version_code_kernel());
printf("DRBD_KERNEL_VERSION=%s\n", escaped_version_code_kernel());
printf("DRBDADM_VERSION_CODE=0x%06x\n", version_code_userland());
printf("DRBDADM_VERSION=%s\n", shell_escape(PACKAGE_VERSION));
exit(0);
break;
case 'P':
connect_to_host = optarg;
break;
case 'W':
insert_tail(&backend_options, names_from_str(optarg));
break;
case 'h':
help = true;
break;
case '?':
goto help;
}
}
first_arg_index = optind;
for (; optind < argc; optind++) {
optarg = argv[optind];
if (*cmd) {
static int last_idx = 0;
ensure_sanity_of_res_name(optarg);
(*resource_names)[last_idx++] = optarg;
}
else if (!strcmp(optarg, "help"))
help = true;
else {
*cmd = find_cmd(optarg);
if (!*cmd) {
/* Passing drbdsetup options like this is discouraged! */
insert_tail(&backend_options, names_from_str(optarg));
}
}
}
if (help)
print_usage_and_exit(*cmd, NULL, 0);
if (*cmd == NULL) {
if (first_arg_index < argc) {
err("%s: Unknown command '%s'\n", progname, argv[first_arg_index]);
return E_USAGE;
}
print_usage_and_exit(*cmd, "No command specified", E_USAGE);
}
/*
* The backend (drbdsetup) options are command specific. Make sure that only
* setup options that this command recognizes are used.
*/
STAILQ_FOREACH(b_opt, &backend_options_check, link) {
const struct field_def *field;
const char *option;
int len;
option = b_opt->name;
for (len = 0; option[len]; len++)
if (option[len] == '=')
break;
field = NULL;
if (option[0] == '-' && option[1] == '-' && (*cmd)->drbdsetup_ctx &&
(*cmd)->drbdsetup_ctx != &wildcard_ctx) {
for (field = (*cmd)->drbdsetup_ctx->fields; field->name; field++) {
if (strlen(field->name) == len - 2 &&
!strncmp(option + 2, field->name, len - 2))
break;
}
if (!field->name)
field = NULL;
}
if (!field && (*cmd)->drbdsetup_ctx != &wildcard_ctx) {
err("%s: unrecognized option '%.*s'\n", progname, len, option);
goto help;
}
}
STAILQ_CONCAT(&backend_options, &backend_options_check);
return 0;
help:
if (*cmd)
err("try '%s help %s'\n", progname, (*cmd)->name);
else
err("try '%s help'\n", progname);
return E_USAGE;
}
struct adm_cmd *find_cmd(char *cmdname)
{
struct adm_cmd *cmd = NULL;
unsigned int i;
if (!strcmp("hidden-commands", cmdname)) {
// before parsing the configuration file...
hidden_cmds(NULL);
exit(0);
}
/* R_PRIMARY / R_SECONDARY is not a state, but a role. Whatever that
* means, actually. But anyways, we decided to start using _role_ as
* the terminus of choice, and deprecate "state". */
substitute_deprecated_cmd(&cmdname, "state", "role");
/* "outdate-peer" got renamed to fence-peer,
* it is not required to actually outdate the peer,
* depending on situation it may be sufficient to power-reset it
* or do some other fencing action, or even call out to "meatware".
* The name of the handler should not imply something that is not done. */
substitute_deprecated_cmd(&cmdname, "outdate-peer", "fence-peer");
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
if (!strcmp(cmds[i]->name, cmdname)) {
cmd = cmds[i];
break;
}
}
return cmd;
}
char *config_file_from_arg(char *arg)
{
char *f;
int minor = minor_by_id(arg);
if (minor >= 0) {
f = lookup_minor(minor);
if (!f) {
err("Don't know which config file belongs to minor %d, trying default ones...\n", minor);
return NULL;
}
} else {
f = lookup_resource(arg);
if (!f) {
err("Don't know which config file belongs to resource %s, trying default ones...\n", arg);
return NULL;
}
}
yyin = fopen(f, "r");
if (yyin == NULL) {
err("Couldn't open file %s for reading, reason: %m\ntrying default config file...\n", config_file);
return NULL;
}
return f;
}
void assign_default_config_file(void)
{
int i;
for (i = 0; conf_file[i]; i++) {
yyin = fopen(conf_file[i], "r");
if (yyin) {
config_file = conf_file[i];
break;
}
}
if (!config_file) {
err("Can not open '%s': %m\n", conf_file[i - 1]);
exit(E_CONFIG_INVALID);
}
}
void count_resources(void)
{
struct d_resource *res;
struct d_volume *vol;
number_of_minors = 0;
for_each_resource(res, &config) {
if (res->ignore) {
nr_resources[IGNORED]++;
/* How can we count ignored volumes?
* Do we want to? */
continue;
} else if (res->stacked)
nr_resources[STACKED]++;
else
nr_resources[NORMAL]++;
for_each_volume(vol, &res->me->volumes) {
number_of_minors++;
if (res->stacked)
nr_volumes[STACKED]++;
/* res->ignored won't come here */
else
nr_volumes[NORMAL]++;
}
}
}
void die_if_no_resources(void)
{
if (!is_drbd_top && nr_resources[IGNORED] > 0 && nr_resources[NORMAL] == 0) {
err("WARN: no normal resources defined for this host (%s)!?\n"
"Misspelled name of the local machine with the 'on' keyword ?\n",
hostname);
exit(E_USAGE);
}
if (!is_drbd_top && nr_resources[NORMAL] == 0) {
err("WARN: no normal resources defined for this host (%s)!?\n", hostname);
exit(E_USAGE);
}
if (is_drbd_top && nr_resources[STACKED] == 0) {
err("WARN: nothing stacked for this host (%s), "
"nothing to do in stacked mode!\n",
hostname);
exit(E_USAGE);
}
}
int main(int argc, char **argv)
{
size_t i;
int rv = 0, r;
struct adm_cmd *cmd = NULL;
char **resource_names = NULL;
struct d_resource *res;
char *env_drbd_nodename = NULL;
int is_dump_xml;
int is_dump;
int is_adjust;
struct cfg_ctx ctx = { };
initialize_err();
initialize_deferred_cmds();
yyin = NULL;
hostname = get_hostname();
no_tty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
env_drbd_nodename = getenv("__DRBD_NODE__");
if (env_drbd_nodename && *env_drbd_nodename) {
hostname = strdup(env_drbd_nodename);
err("\n"
" found __DRBD_NODE__ in environment\n"
" PRETENDING that I am >>%s<<\n\n",
hostname);
}
assign_command_names_from_argv0(argv);
if (drbdsetup == NULL || drbdmeta == NULL || drbd_proxy_ctl == NULL) {
err("could not strdup argv[0].\n");
exit(E_EXEC_ERROR);
}
maybe_exec_legacy_drbdadm(argv);
recognize_all_drbdsetup_options();
rv = parse_options(argc, argv, &cmd, &resource_names);
if (rv)
return rv;
if (config_test && !cmd->test_config) {
err("The --config-to-test (-t) option is only allowed "
"with the dump and sh-nop commands\n");
exit(E_USAGE);
}
do_verify_ips = cmd->verify_ips;
is_dump_xml = (cmd == &dump_xml_cmd);
is_dump = (is_dump_xml || cmd == &dump_cmd);
is_adjust = (cmd == &adjust_cmd || cmd == &adjust_wp_cmd);
if (!resource_names[0]) {
if (!is_dump && cmd->res_name_required)
print_usage_and_exit(cmd, "No resource names specified", E_USAGE);
} else if (resource_names[0]) {
if (cmd->backend_res_name)
/* Okay */ ;
else if (!cmd->res_name_required)
err("This command will ignore resource names!\n");
else if (resource_names[1] && cmd->use_cached_config_file)
err("You should not use this command with multiple resources!\n");
}
if (!config_file && cmd->use_cached_config_file)
config_file = config_file_from_arg(resource_names[0]);
if (!config_file)
/* may exit if no config file can be used! */
assign_default_config_file();
/* for error-reporting reasons config_file may be re-assigned by adm_adjust,
* we need the current value for register_minor, though.
* save that. */
if (config_from_stdin)
config_save = config_file;
else
config_save = canonify_path(config_file);
my_parse();
if (config_test) {
char *saved_config_file = config_file;
char *saved_config_save = config_save;
config_file = config_test;
config_save = canonify_path(config_test);
fclose(yyin);
yyin = fopen(config_test, "r");
if (!yyin) {
err("Can not open '%s'.\n.", config_test);
exit(E_EXEC_ERROR);
}
my_parse();
config_file = saved_config_file;
config_save = saved_config_save;
}
if (!config_valid)
exit(E_CONFIG_INVALID);
post_parse(&config, cmd->is_proxy_cmd ? MATCH_ON_PROXY : 0);
if (!is_dump || dry_run || verbose)
expand_common();
if (dry_run || config_from_stdin)
do_register = 0;
count_resources();
if (cmd->uc_dialog)
uc_node(global_options.usage_count);
ctx.cmd = cmd;
if (cmd->res_name_required || resource_names[0]) {
if (STAILQ_EMPTY(&config) && !is_dump) {
err("no resources defined!\n");
exit(E_USAGE);
}
global_validate_maybe_expand_die_if_invalid(!is_dump,
cmd->is_proxy_cmd ? MATCH_ON_PROXY : 0);
if (!resource_names[0] || !strcmp(resource_names[0], "all")) {
/* either no resource arguments at all,
* but command is dump / dump-xml, so implicit "all",
* or an explicit "all" argument is given */
if (!is_dump)
die_if_no_resources();
/* verify ips first, for all of them */
for_each_resource(res, &config) {
verify_ips(res);
}
if (!config_valid)
exit(E_CONFIG_INVALID);
if (is_dump_xml)
print_dump_xml_header();
else if (is_dump)
print_dump_header();
if (is_adjust)
adjust_more_than_one_resource = 1;
for_each_resource(res, &config) {
if (!is_dump && res->ignore)
continue;
if (!is_dump && is_drbd_top != res->stacked)
continue;
ctx.res = res;
ctx.vol = NULL;
r = call_cmd(cmd, &ctx, EXIT_ON_FAIL); /* does exit for r >= 20! */
/* this super positioning of return values is soo ugly
* anyone any better idea? */
if (r > rv)
rv = r;
}
if (is_dump_xml)
printf("</config>\n");
} else {
/* explicit list of resources to work on */
struct connection *conn;
/* first we execute some sanity checks,
* the checks use ignore_tmp */
for_each_resource(res, &config)
for_each_connection(conn, &res->connections)
conn->ignore_tmp = conn->ignore;
/* check if we would enable a connection that should be ignored */
for (i = 0; resource_names[i]; i++)
if (ctx_by_name(&ctx, resource_names[i], WOULD_ENABLE_DISABLED) > 0) {
err("USAGE_BUG: Tried to enable disabled connections %s\n",
resource_names[i]);
exit(E_USAGE);
}
/* check if we would enable a connection that was already enabled.
* set all connections to ignore and then check if we would enable a
* connection twice */
for_each_resource(res, &config)
for_each_connection(conn, &res->connections)
conn->ignore_tmp = true;
for (i = 0; resource_names[i]; i++)
if (ctx_by_name(&ctx, resource_names[i], WOULD_ENABLE_MULTI_TIMES) > 0) {
err("USAGE_BUG: %s would enable an already enabled connection\n",
resource_names[i]);
exit(E_USAGE);
}
if (is_adjust && resource_names[1])
adjust_more_than_one_resource = 1;
for (i = 0; resource_names[i]; i++) {
ctx.res = NULL;
ctx.vol = NULL;
r = ctx_by_name(&ctx, resource_names[i], SETUP_MULTI);
if (!ctx.res) {
ctx_by_minor(&ctx, resource_names[i]);
r = 0;
}
if (!ctx.res) {
err("'%s' not defined in your config (for this host).\n", resource_names[i]);
exit(E_USAGE);
}
if (r)
exit(E_USAGE);
if (!cmd->vol_id_required && !cmd->iterate_volumes && ctx.vol != NULL && !cmd->vol_id_optional) {
if (ctx.vol->implicit)
ctx.vol = NULL;
else {
err("%s operates on whole resources, but you specified a specific volume!\n",
cmd->name);
exit(E_USAGE);
}
}
if (cmd->vol_id_required && !ctx.vol && STAILQ_FIRST(&ctx.res->me->volumes)->implicit)
ctx.vol = STAILQ_FIRST(&ctx.res->me->volumes);
if (cmd->vol_id_required && !ctx.vol) {
err("%s requires a specific volume id, but none is specified.\n"
"Try '%s minor-<minor_number>' or '%s %s/<vnr>'\n",
cmd->name, cmd->name, cmd->name, resource_names[i]);
exit(E_USAGE);
}
if (ctx.res->ignore && !is_dump) {
err("'%s' ignored, since this host (%s) is not mentioned with an 'on' keyword.\n",
ctx.res->name, hostname);
if (rv < E_USAGE)
rv = E_USAGE;
continue;
}
/* Someone named it explicitly. Don't be annoying.
* Also, don't break handlers called from kernel. */
is_drbd_top = ctx.res->stacked;
verify_ips(ctx.res);
if (!is_dump && !config_valid)
exit(E_CONFIG_INVALID);
r = call_cmd(cmd, &ctx, EXIT_ON_FAIL); /* does exit for r >= 20! */
if (r > rv)
rv = r;
}
}
} else { // Commands which do not need a resource name
/* no call_cmd, as that implies register_minor,
* which does not make sense for resource independent commands.
* It does also not need to iterate over volumes: it does not even know the resource. */
ctx.cmd = cmd;
rv = __call_cmd_fn(&ctx, KEEP_RUNNING);
if (rv >= 10) { /* why do we special case the "generic sh-*" commands? */
err("command %s exited with code %d\n", cmd->name, rv);
exit(rv);
}
}
r = run_deferred_cmds();
if (r > rv)
rv = r;
free_config();
free(resource_names);
if (admopt != general_admopt)
free(admopt);
free_btrees();
return rv;
}
void yyerror(char *text)
{
err("%s:%d: %s\n", config_file, line, text);
exit(E_SYNTAX);
}
|