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
|
/*
* Copyright (c) 2000-2002 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program 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.
*
* This program is distributed in the hope that it would 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 this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <time.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <termios.h>
#include <getopt.h>
#include <stdint.h>
#include <sched.h>
#include <pthread.h>
#include <assert.h>
#include <string.h>
#include <uuid/uuid.h>
#include <locale.h>
#include "config.h"
#include "exit.h"
#include "types.h"
#include "stream.h"
#include "cldmgr.h"
#include "getopt.h"
#include "mlog.h"
#include "qlock.h"
#include "lock.h"
#include "dlog.h"
#include "global.h"
#include "drive.h"
#include "media.h"
#include "content.h"
#include "inventory.h"
#ifdef DUMP
/* main.c - main for dump
*/
#endif /* DUMP */
#ifdef RESTORE
/* main.c - main for restore
*/
#endif /* RESTORE */
/* structure definitions used locally ****************************************/
#ifdef RESTORE
#define VMSZ_PER 4 /* proportion of available vm to use in tree */
#endif /* RESTORE */
#define DLOG_TIMEOUT 60 /* time out operator dialog */
#define STOP_TIMEOUT 600 /* seconds after stop req. before abort */
#define ABORT_TIMEOUT 10 /* seconds after abort req. before abort */
#define MINSTACKSZ 0x02000000
#define MAXSTACKSZ 0x08000000
/* declarations of externally defined global symbols *************************/
extern void rmt_turnonmsgs(int);
/* forward declarations of locally defined global functions ******************/
void usage(void);
bool_t preemptchk(int);
/* forward declarations of locally defined static functions ******************/
static bool_t loadoptfile(int *argcp, char ***argvp);
static char * stripquotes(char *p);
static void shiftleftby1(char *p, char *endp);
static void sighandler(int);
static int childmain(void *);
static bool_t sigint_dialog(void);
static char *sigintstr(void);
#ifdef DUMP
static bool_t set_rlimits(void);
#endif /* DUMP */
#ifdef RESTORE
static bool_t set_rlimits(size64_t *);
#endif /* RESTORE */
static char *sig_numstring(int num);
static char *strpbrkquotes(char *p, const char *sep);
/* definition of locally defined global variables ****************************/
char *progname = 0; /* used in all error output */
char *homedir = 0; /* directory invoked from */
bool_t pipeline = BOOL_FALSE;
bool_t stdoutpiped = BOOL_FALSE;
pthread_t parenttid;
char *sistr;
size_t pgsz;
size_t pgmask;
/* definition of locally defined static variables *****************************/
static rlim64_t minstacksz;
static rlim64_t maxstacksz;
#ifdef RESTORE
static size64_t vmsz;
#endif /* RESTORE */
static time32_t stop_deadline;
static bool_t stop_in_progress;
static bool_t sighup_received;
static bool_t sigterm_received;
static bool_t sigquit_received;
static bool_t sigint_received;
/* REFERENCED */
static int sigstray_received;
static bool_t progrpt_enabledpr;
static time32_t progrpt_interval;
static time32_t progrpt_deadline;
/* definition of locally defined global functions ****************************/
int
main(int argc, char *argv[])
{
int c;
#ifdef DUMP
uid_t euid;
#endif /* DUMP */
ix_t stix; /* stream index */
bool_t infoonly;
#ifdef DUMP
global_hdr_t *gwhdrtemplatep;
#endif /* DUMP */
bool_t init_error;
bool_t coredump_requested = BOOL_FALSE;
int exitcode;
rlim64_t tmpstacksz;
struct sigaction sa;
int prbcld_xc = EXIT_NORMAL;
int xc;
bool_t ok;
/* REFERENCED */
int rval;
int err;
/* sanity checks
*/
assert(sizeof(char_t) == 1);
assert(sizeof(u_char_t) == 1);
assert(sizeof(int32_t) == 4);
assert(sizeof(uint32_t) == 4);
assert(sizeof(size32_t) == 4);
assert(sizeof(int64_t) == 8);
assert(sizeof(uint64_t) == 8);
assert(sizeof(size64_t) == 8);
/* record the command name used to invoke
*/
progname = argv[0];
/* setup I18N support */
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* bootstrap message logging (stage 0)
*/
mlog_init0();
/* Get the parent's pthread id. will be used
* to differentiate parent from children.
*/
parenttid = pthread_self();
rval = atexit(mlog_exit_flush);
assert(rval == 0);
/* pre-scan the command line for the option file option.
* if found, create a new argv.
*/
ok = loadoptfile(&argc, &argv);
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_OPT);
}
/* initialize message logging (stage 1)
*/
ok = mlog_init1(argc, argv);
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* scan the command line for the info, progress
* report options, and stacksz.
*/
minstacksz = MINSTACKSZ;
maxstacksz = MAXSTACKSZ;
infoonly = BOOL_FALSE;
progrpt_enabledpr = BOOL_FALSE;
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, GETOPT_CMDSTRING)) != EOF) {
switch (c) {
case GETOPT_MINSTACKSZ:
if (!optarg || optarg[0] == '-') {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument missing\n"),
c);
usage();
return mlog_exit(EXIT_ERROR, RV_OPT);
}
errno = 0;
tmpstacksz = strtoull(optarg, 0, 0);
if (tmpstacksz == UINT64_MAX
||
errno == ERANGE) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument (%s) invalid\n"),
c,
optarg);
usage();
return mlog_exit(EXIT_ERROR, RV_OPT);
}
minstacksz = tmpstacksz;
break;
case GETOPT_MAXSTACKSZ:
if (!optarg || optarg[0] == '-') {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument missing\n"),
c);
usage();
return mlog_exit(EXIT_ERROR, RV_OPT);
}
errno = 0;
tmpstacksz = strtoull(optarg, 0, 0);
if (tmpstacksz == UINT64_MAX
||
errno == ERANGE) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument (%s) invalid\n"),
c,
optarg);
usage();
return mlog_exit(EXIT_ERROR, RV_OPT);
}
maxstacksz = tmpstacksz;
break;
case GETOPT_HELP:
infoonly = BOOL_TRUE;
mlog_exit_hint(RV_USAGE);
break;
case GETOPT_PROGRESS:
if (!optarg || optarg[0] == '-') {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument missing\n"),
c);
usage();
return mlog_exit(EXIT_ERROR, RV_OPT);
}
progrpt_interval = (time32_t)atoi(optarg);
if (progrpt_interval > 0) {
progrpt_enabledpr = BOOL_TRUE;
} else {
progrpt_enabledpr = BOOL_FALSE;
}
break;
}
}
/* sanity check resultant stack size limits
*/
if (minstacksz > maxstacksz) {
mlog(MLOG_NORMAL
|
MLOG_ERROR
|
MLOG_NOLOCK
|
MLOG_PROC,
_("specified minimum stack size is larger than maximum: "
"min is 0x%llx, max is 0x%llx\n"),
minstacksz,
maxstacksz);
return mlog_exit(EXIT_ERROR, RV_INIT);
}
if (argc == 1) {
infoonly = BOOL_TRUE;
}
/* set a progress report deadline to allow preemptchk() to
* report
*/
if (progrpt_enabledpr) {
progrpt_deadline = time(0) + progrpt_interval;
}
/* intitialize the stream manager
*/
stream_init();
#ifdef DUMP
/* set the memory limits to their appropriate values.
*/
ok = set_rlimits();
#endif /* DUMP */
#ifdef RESTORE
/* set the memory limits to their appropriate values. this is necessary
* to accomodate the tree abstraction and some recursive functions.
* also determines maximum vm, which will be budgeted among the
* various abstractions.
*/
ok = set_rlimits(&vmsz);
#endif /* RESTORE */
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* initialize message logging (stage 2) - allocate the message lock
*/
ok = mlog_init2();
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* initialize the critical region lock
*/
lock_init();
rmt_turnonmsgs(1); /* turn on WARNING msgs for librmt */
mlog(MLOG_NITTY + 1, "INTGENMAX == %ld (0x%lx)\n", INTGENMAX, INTGENMAX);
mlog(MLOG_NITTY + 1, "UINTGENMAX == %lu (0x%lx)\n", UINTGENMAX, UINTGENMAX);
mlog(MLOG_NITTY + 1, "OFF64MAX == %lld (0x%llx)\n", OFF64MAX, OFF64MAX);
mlog(MLOG_NITTY + 1, "OFFMAX == %ld (0x%lx)\n", OFFMAX, OFFMAX);
mlog(MLOG_NITTY + 1, "SIZEMAX == %lu (0x%lx)\n", SIZEMAX, SIZEMAX);
mlog(MLOG_NITTY + 1, "INOMAX == %lu (0x%lx)\n", INOMAX, INOMAX);
mlog(MLOG_NITTY + 1, "TIMEMAX == %ld (0x%lx)\n", TIMEMAX, TIMEMAX);
mlog(MLOG_NITTY + 1, "SIZE64MAX == %llu (0x%llx)\n", SIZE64MAX, SIZE64MAX);
mlog(MLOG_NITTY + 1, "INO64MAX == %llu (0x%llx)\n", INO64MAX, INO64MAX);
mlog(MLOG_NITTY + 1, "UINT64MAX == %llu (0x%llx)\n", UINT64MAX, UINT64MAX);
mlog(MLOG_NITTY + 1, "INT64MAX == %lld (0x%llx)\n", INT64MAX, INT64MAX);
mlog(MLOG_NITTY + 1, "UINT32MAX == %u (0x%x)\n", UINT32MAX, UINT32MAX);
mlog(MLOG_NITTY + 1, "INT32MAX == %d (0x%x)\n", INT32MAX, INT32MAX);
mlog(MLOG_NITTY + 1, "INT16MAX == %d (0x%x)\n", INT16MAX, INT16MAX);
mlog(MLOG_NITTY + 1, "UINT16MAX == %u (0x%x)\n", UINT16MAX, UINT16MAX);
/* ask the system for the true vm page size, which must be used
* in all mmap calls
*/
pgsz = (size_t)getpagesize();
mlog(MLOG_DEBUG | MLOG_PROC,
"getpagesize( ) returns %u\n",
pgsz);
assert((int)pgsz > 0);
pgmask = pgsz - 1;
/* report parent tid
*/
mlog(MLOG_DEBUG | MLOG_PROC,
"parent tid is %lu\n",
parenttid);
/* get the current working directory: this is where we will dump
* core, if necessary. some tmp files may be placed here as well.
*/
homedir = getcwd(0, MAXPATHLEN);
if (!homedir) {
mlog(MLOG_NORMAL | MLOG_ERROR,
_("unable to determine current directory: %s\n"),
strerror(errno));
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* sanity check the inventory database directory, setup global paths
*/
ok = inv_setup_base();
if (!ok) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("both /var/lib/xfsdump and /var/xfsdump exist - fatal\n"));
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* if just looking for info, oblige
*/
if (infoonly) {
mlog(MLOG_NORMAL,
_("version %s (dump format %d.0)\n"),
VERSION, GLOBAL_HDR_VERSION);
usage();
return mlog_exit(EXIT_NORMAL, RV_OK); /* normal termination */
}
/* if an inventory display is requested, do it and exit
*/
if (!inv_DEBUG_print(argc, argv)) {
return mlog_exit(EXIT_NORMAL, RV_OK); /* normal termination */
}
#ifdef DUMP
/* insist that the effective user id is root.
* this must appear after inv_DEBUG_print(),
* so it may be done without root privilege.
*/
euid = geteuid();
mlog(MLOG_DEBUG | MLOG_PROC,
"effective user id is %d\n",
euid);
if (euid != 0) {
mlog(MLOG_NORMAL,
_("effective user ID must be root\n"));
return mlog_exit(EXIT_ERROR, RV_PERM);
}
#endif /* DUMP */
/* initialize operator dialog capability
*/
ok = dlog_init(argc, argv);
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* initialize the child process manager
*/
ok = cldmgr_init();
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* select and instantiate a drive manager for each stream. this
* is the first pass at initialization, so don't do anything
* terribly time-consuming here. A second initialization pass
* will be done shortly.
*/
ok = drive_init1(argc, argv);
if (!ok) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
/* check the drives to see if we're in a pipeline.
* if not, check stdout anyway, in case someone is trying to pipe
* the log messages into more, tee, ...
*/
if (drivepp[0]->d_isunnamedpipepr) {
mlog(MLOG_DEBUG | MLOG_NOTE,
"pipeline detected\n");
pipeline = BOOL_TRUE;
} else {
struct stat64 statbuf;
if (fstat64(1, &statbuf) == 0
&&
(statbuf.st_mode & S_IFMT) == S_IFIFO) {
stdoutpiped = BOOL_TRUE;
}
}
/* announce version and instructions
*/
sistr = sigintstr();
mlog(MLOG_VERBOSE,
_("version %s (dump format %d.0)"),
VERSION, GLOBAL_HDR_VERSION);
if (!pipeline && !stdoutpiped && sistr && dlog_allowed()) {
mlog(MLOG_VERBOSE | MLOG_BARE, _(
" - "
"type %s for status and control\n"),
sistr);
} else {
mlog(MLOG_VERBOSE | MLOG_BARE,
"\n");
}
#ifdef DUMP
/* build a global write header template
*/
gwhdrtemplatep = global_hdr_alloc(argc, argv);
if (!gwhdrtemplatep) {
return mlog_exit(EXIT_ERROR, RV_INIT);
}
#endif /* DUMP */
/* tell mlog how many streams there are. the format of log messages
* depends on whether there are one or many.
*/
mlog_tell_streamcnt(drivecnt);
/* initialize the state of signal processing. if in a pipeline, just
* want to exit when a signal is received. otherwise, hold signals so
* they don't interfere with sys calls; they will be released at
* pre-emption points and upon pausing in the main loop.
*
* note that since we're multi-threaded, handling SIGCHLD causes
* problems with system()'s ability to obtain a child's exit status
* (because the main thread may process SIGCHLD before the thread
* running system() calls waitpid()). likewise explicitly ignoring
* SIGCHLD also prevents system() from getting an exit status.
* therefore we don't do anything with SIGCHLD.
*/
sigfillset(&sa.sa_mask);
sa.sa_flags = 0;
/* always ignore SIGPIPE, instead handle EPIPE as part
* of normal sys call error handling.
*/
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
if (!pipeline) {
sigset_t blocked_set;
stop_in_progress = BOOL_FALSE;
coredump_requested = BOOL_FALSE;
sighup_received = BOOL_FALSE;
sigterm_received = BOOL_FALSE;
sigint_received = BOOL_FALSE;
sigquit_received = BOOL_FALSE;
sigstray_received = BOOL_FALSE;
alarm(0);
sigemptyset(&blocked_set);
sigaddset(&blocked_set, SIGINT);
sigaddset(&blocked_set, SIGHUP);
sigaddset(&blocked_set, SIGTERM);
sigaddset(&blocked_set, SIGQUIT);
sigaddset(&blocked_set, SIGALRM);
sigaddset(&blocked_set, SIGUSR1);
pthread_sigmask(SIG_SETMASK, &blocked_set, NULL);
sa.sa_handler = sighandler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGALRM, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
}
/* do content initialization.
*/
#ifdef DUMP
ok = content_init(argc, argv, gwhdrtemplatep);
#endif /* DUMP */
#ifdef RESTORE
ok = content_init(argc, argv, vmsz / VMSZ_PER);
#endif /* RESTORE */
if (!ok) {
err = mlog_exit(EXIT_ERROR, RV_INIT);
goto err_free;
}
/* if in a pipeline, go single-threaded with just one stream.
*/
if (pipeline) {
int exitcode;
sa.sa_handler = sighandler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
#ifdef DUMP
ok = drive_init2(argc,
argv,
gwhdrtemplatep);
#endif /* DUMP */
#ifdef RESTORE
ok = drive_init2(argc,
argv,
(global_hdr_t *)0);
#endif /* RESTORE */
if (!ok) {
err = mlog_exit(EXIT_ERROR, RV_INIT);
goto err_free;
}
ok = drive_init3();
if (!ok) {
err = mlog_exit(EXIT_ERROR, RV_INIT);
goto err_free;
}
#ifdef DUMP
exitcode = content_stream_dump(0);
#endif /* DUMP */
#ifdef RESTORE
exitcode = content_stream_restore(0);
#endif /* RESTORE */
if (exitcode != EXIT_NORMAL) {
(void)content_complete();
/* for cleanup side-effect */
err = mlog_exit(exitcode, RV_UNKNOWN);
} else if (content_complete()) {
err = mlog_exit(EXIT_NORMAL, RV_OK);
} else {
err = mlog_exit(EXIT_INTERRUPT, RV_UNKNOWN);
}
goto err_free;
}
/* used to skip to end if errors occur during any
* stage of initialization.
*/
init_error = BOOL_FALSE;
/* now do the second and third passes of drive initialization.
* allocate per-stream write and read headers. if a drive
* manager uses a worker process, it should be created now,
* using cldmgr_create(). each drive manager may use the worker to
* asynchronously read the media file header, typically a very
* time-consuming chore. drive_init3 will synchronize with each worker.
*/
if (!init_error) {
#ifdef DUMP
ok = drive_init2(argc,
argv,
gwhdrtemplatep);
#endif /* DUMP */
#ifdef RESTORE
ok = drive_init2(argc,
argv,
(global_hdr_t *)0);
#endif /* RESTORE */
if (!ok) {
init_error = BOOL_TRUE;
}
}
if (!init_error) {
ok = drive_init3();
if (!ok) {
init_error = BOOL_TRUE;
}
}
/* create a child thread for each stream. drivecnt global from
* drive.h, initialized by drive_init[12]
*/
if (!init_error) {
for (stix = 0; stix < drivecnt; stix++) {
ok = cldmgr_create(childmain,
stix,
"child",
(void *)stix);
if (!ok) {
init_error = BOOL_TRUE;
}
}
}
/* loop here, waiting for children to die, processing operator
* signals.
*/
if (progrpt_enabledpr) {
(void)alarm((uint)progrpt_interval);
}
for (;;) {
time32_t now;
bool_t stop_requested = BOOL_FALSE;
int stop_timeout = -1;
sigset_t empty_set;
/* if there was an initialization error,
* immediately stop all children.
*/
if (init_error) {
stop_timeout = STOP_TIMEOUT;
stop_requested = BOOL_TRUE;
}
/* if one or more children died abnormally, request a
* stop. furthermore, note that core should be dumped if
* the child explicitly exited with EXIT_FAULT.
*/
xc = cldmgr_join();
if (xc) {
if (xc == EXIT_FAULT) {
coredump_requested = BOOL_TRUE;
stop_timeout = ABORT_TIMEOUT;
} else {
stop_timeout = STOP_TIMEOUT;
}
prbcld_xc = xc;
stop_requested = BOOL_TRUE;
}
/* all children died normally. break out.
*/
if (cldmgr_remainingcnt() == 0) {
mlog(MLOG_DEBUG,
"all children have exited\n");
break;
}
/* get the current time
*/
now = time(0);
/* check for stop timeout. request a core dump and bail
*/
if (stop_in_progress && now >= stop_deadline) {
mlog(MLOG_NORMAL | MLOG_ERROR,
_("session interrupt timeout\n"));
coredump_requested = BOOL_TRUE;
break;
}
/* operator sent SIGINT. if dialog allowed, enter dialog.
* otherwise treat as a hangup and request a stop.
*/
if (sigint_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGINT received\n");
if (stop_in_progress) {
if (dlog_allowed()) {
(void)sigint_dialog();
}
/*
mlog(MLOG_NORMAL,
_("session interrupt in progress: "
"please wait\n"));
*/
} else {
if (dlog_allowed()) {
stop_requested = sigint_dialog();
} else {
stop_requested = BOOL_TRUE;
}
stop_timeout = STOP_TIMEOUT;
}
/* important that this appear after dialog.
* allows dialog to be terminated with SIGINT,
* without infinite loop.
*/
sigint_received = BOOL_FALSE;
}
/* refresh the current time in case in dialog for a while
*/
now = time(0);
/* request a stop on hangup
*/
if (sighup_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGHUP received\n");
stop_requested = BOOL_TRUE;
stop_timeout = STOP_TIMEOUT;
sighup_received = BOOL_FALSE;
}
/* request a stop on termination request
*/
if (sigterm_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGTERM received\n");
stop_requested = BOOL_TRUE;
stop_timeout = STOP_TIMEOUT;
sigterm_received = BOOL_FALSE;
}
/* operator send SIGQUIT. treat like an interrupt,
* but force a core dump
*/
if (sigquit_received) {
mlog(MLOG_NORMAL | MLOG_PROC,
"SIGQUIT received\n");
if (stop_in_progress) {
mlog(MLOG_NORMAL,
_("session interrupt in progress: "
"please wait\n"));
stop_deadline = now;
} else {
stop_requested = BOOL_TRUE;
stop_timeout = ABORT_TIMEOUT;
sigquit_received = BOOL_FALSE;
coredump_requested = BOOL_TRUE;
}
}
/* see if need to initiate a stop
*/
if (stop_requested && !stop_in_progress) {
mlog(MLOG_NORMAL,
_("initiating session interrupt (timeout in %d sec)\n"),
stop_timeout);
mlog_exit_hint(RV_INTR);
stop_in_progress = BOOL_TRUE;
cldmgr_stop();
assert(stop_timeout >= 0);
stop_deadline = now + (time32_t)stop_timeout;
}
/* set alarm if needed (note time stands still during dialog)
*/
if (stop_in_progress) {
int timeout = (int)(stop_deadline - now);
if (timeout < 0) {
timeout = 0;
}
mlog(MLOG_DEBUG | MLOG_PROC,
"setting alarm for %d second%s\n",
timeout,
timeout == 1 ? "" : "s");
(void)alarm((uint)timeout);
if (timeout == 0) {
continue;
}
}
if (progrpt_enabledpr && !stop_in_progress) {
bool_t need_progrptpr = BOOL_FALSE;
while (now >= progrpt_deadline) {
need_progrptpr = BOOL_TRUE;
progrpt_deadline += progrpt_interval;
}
if (need_progrptpr) {
size_t statlinecnt;
char **statline;
ix_t i;
statlinecnt = content_statline(&statline);
for (i = 0; i < statlinecnt; i++) {
mlog(MLOG_NORMAL,
statline[i]);
}
}
(void)alarm((uint)(progrpt_deadline
-
now));
}
/* sleep until next signal
*/
sigemptyset(&empty_set);
sigsuspend(&empty_set);
(void)alarm(0);
}
/* check if core dump requested
*/
if (coredump_requested) {
mlog(MLOG_DEBUG | MLOG_PROC,
"core dump requested, aborting (pid %d)\n",
getpid());
abort();
}
/* determine if dump or restore was interrupted
* or an initialization error occurred.
*/
if (init_error) {
(void)content_complete();
exitcode = EXIT_ERROR;
} else {
if (content_complete()) {
if (prbcld_xc != EXIT_NORMAL)
exitcode = EXIT_ERROR;
else
exitcode = EXIT_NORMAL;
} else {
exitcode = EXIT_INTERRUPT;
if (mlog_get_hint() == RV_NONE)
mlog_exit_hint(RV_INCOMPLETE);
}
}
err = mlog_exit(exitcode, RV_UNKNOWN);
err_free:
#ifdef DUMP
global_hdr_free(gwhdrtemplatep);
#endif /* DUMP */
return err;
}
#define ULO(f, o) fprintf(stderr, \
"%*s[ -%c %s ]\n", \
ps, \
ns, \
o, \
f), \
ps = pfxsz
#define ULN(f) fprintf(stderr, \
"%*s[ %s ]\n", \
ps, \
ns, \
f), \
ps = pfxsz
void
usage(void)
{
int pfxsz;
int ps = 0;
char *ns = "";
pfxsz = fprintf(stderr, _("%s: usage: %s "),
progname, basename(progname));
#ifdef DUMP
ULO(_("(dump DMF dualstate files as offline)"), GETOPT_DUMPASOFFLINE);
ULO(_("<blocksize>"), GETOPT_BLOCKSIZE);
ULO(_("<media change alert program> "), GETOPT_ALERTPROG);
ULO(_("<dump media file size> "), GETOPT_FILESZ);
ULO(_("(allow files to be excluded)"), GETOPT_EXCLUDEFILES);
ULO(_("<destination> ..."), GETOPT_DUMPDEST);
ULO(_("(help)"), GETOPT_HELP);
ULO(_("<level>"), GETOPT_LEVEL);
ULO(_("(force usage of minimal rmt)"), GETOPT_MINRMT);
ULO(_("(overwrite tape)"), GETOPT_OVERWRITE);
ULO(_("<seconds between progress reports>"), GETOPT_PROGRESS);
ULO(_("<use QIC tape settings>"), GETOPT_QIC);
ULO(_("<subtree> ..."), GETOPT_SUBTREE);
ULO(_("<file> (use file mtime for dump time"), GETOPT_DUMPTIME);
ULO(_("<verbosity {silent, verbose, trace}>"), GETOPT_VERBOSITY);
ULO(_("<maximum file size>"), GETOPT_MAXDUMPFILESIZE);
ULO(_("(don't dump extended file attributes)"), GETOPT_NOEXTATTR);
ULO(_("<base dump session id>"), GETOPT_BASED);
#ifdef REVEAL
ULO(_("(generate tape record checksums)"), GETOPT_RECCHKSUM);
#endif /* REVEAL */
ULO(_("(skip unchanged directories)"), GETOPT_NOUNCHANGEDDIRS);
ULO(_("(pre-erase media)"), GETOPT_ERASE);
ULO(_("(don't prompt)"), GETOPT_FORCE);
#ifdef REVEAL
ULO(_("<minimum thread stack size>"), GETOPT_MINSTACKSZ);
ULO(_("<maximum thread stack size>"), GETOPT_MAXSTACKSZ);
#endif /* REVEAL */
ULO(_("(display dump inventory)"), GETOPT_INVPRINT);
ULO(_("(inhibit inventory update)"), GETOPT_NOINVUPDATE);
ULO(_("(generate format 2 dump)"), GETOPT_FMT2COMPAT);
ULO(_("<session label>"), GETOPT_DUMPLABEL);
ULO(_("<media label> ..."), GETOPT_MEDIALABEL);
#ifdef REVEAL
ULO(_("(timestamp messages)"), GETOPT_TIMESTAMP);
#endif /* REVEAL */
ULO(_("<options file>"), GETOPT_OPTFILE);
#ifdef REVEAL
ULO(_("(pin down I/O buffers)"), GETOPT_RINGPIN);
#endif /* REVEAL */
ULO(_("(resume)"), GETOPT_RESUME);
ULO(_("(don't timeout dialogs)"), GETOPT_NOTIMEOUTS);
#ifdef REVEAL
ULO(_("(unload media when change needed)"), GETOPT_UNLOAD);
ULO(_("(show subsystem in messages)"), GETOPT_SHOWLOGSS);
ULO(_("(show verbosity in messages)"), GETOPT_SHOWLOGLEVEL);
#endif /* REVEAL */
ULO(_("<I/O buffer ring length>"), GETOPT_RINGLEN);
ULN(_("- (stdout)"));
ULN(_("<source (mntpnt|device)>"));
#endif /* DUMP */
#ifdef RESTORE
ULO(_("<alt. workspace dir> ..."), GETOPT_WORKSPACE);
ULO(_("<blocksize>"), GETOPT_BLOCKSIZE);
ULO(_("<media change alert program> "), GETOPT_ALERTPROG);
ULO(_("(don't overwrite existing files)"), GETOPT_EXISTING);
ULO(_("<source> ..."), GETOPT_DUMPDEST);
ULO(_("(help)"), GETOPT_HELP);
ULO(_("(interactive)"), GETOPT_INTERACTIVE);
ULO(_("(force usage of minimal rmt)"), GETOPT_MINRMT);
ULO(_("<file> (restore only if newer than)"), GETOPT_NEWER);
ULO(_("(restore owner/group even if not root)"),GETOPT_OWNER);
ULO(_("<seconds between progress reports>"), GETOPT_PROGRESS);
ULO(_("<use QIC tape settings>"), GETOPT_QIC);
ULO(_("(cumulative restore)"), GETOPT_CUMULATIVE);
ULO(_("<subtree> ..."), GETOPT_SUBTREE);
ULO(_("(contents only)"), GETOPT_TOC);
ULO(_("<verbosity {silent, verbose, trace}>"), GETOPT_VERBOSITY);
ULO(_("(use small tree window)"), GETOPT_SMALLWINDOW);
ULO(_("(try to fix rootdir due to xfsdump issue)"),GETOPT_FIXROOTDIR);
ULO(_("(don't restore extended file attributes)"), GETOPT_NOEXTATTR);
ULO(_("(restore root dir owner/permissions)"), GETOPT_ROOTPERM);
ULO(_("(restore DMAPI event settings)"), GETOPT_SETDM);
#ifdef REVEAL
ULO(_("(check tape record checksums)"), GETOPT_RECCHKSUM);
#endif /* REVEAL */
ULO(_("(don't overwrite if changed)"), GETOPT_CHANGED);
ULO(_("(don't prompt)"), GETOPT_FORCE);
ULO(_("(display dump inventory)"), GETOPT_INVPRINT);
ULO(_("(inhibit inventory update)"), GETOPT_NOINVUPDATE);
ULO(_("(force use of format 2 generation numbers)"), GETOPT_FMT2COMPAT);
ULO(_("<session label>"), GETOPT_DUMPLABEL);
#ifdef REVEAL
ULO(_("(timestamp messages)"), GETOPT_TIMESTAMP);
#endif /* REVEAL */
ULO(_("<options file>"), GETOPT_OPTFILE);
#ifdef REVEAL
ULO(_("(pin down I/O buffers)"), GETOPT_RINGPIN);
#endif /* REVEAL */
ULO(_("(force interrupted session completion)"),GETOPT_SESSCPLT);
ULO(_("(resume)"), GETOPT_RESUME);
ULO(_("<session id>"), GETOPT_SESSIONID);
ULO(_("(don't timeout dialogs)"), GETOPT_NOTIMEOUTS);
#ifdef REVEAL
ULO(_("(unload media when change needed)"), GETOPT_UNLOAD);
ULO(_("(show subsystem in messages)"), GETOPT_SHOWLOGSS);
ULO(_("(show verbosity in messages)"), GETOPT_SHOWLOGLEVEL);
#endif /* REVEAL */
ULO(_("<excluded subtree> ..."), GETOPT_NOSUBTREE);
ULO(_("<I/O buffer ring length>"), GETOPT_RINGLEN);
ULN(_("- (stdin)"));
ULN(_("<destination>"));
#endif /* RESTORE */
/* anywhere usage is called we will exit shortly after...
* catch all of those cases below
*/
(void) mlog_exit(EXIT_ERROR, RV_OPT);
}
/* returns TRUE if preemption
*/
bool_t
preemptchk(int flg)
{
bool_t preempt_requested;
int i;
int sigs[] = { SIGINT, SIGHUP, SIGTERM, SIGQUIT };
int num_sigs = sizeof(sigs) / sizeof(sigs[0]);
sigset_t pending_set, handle_set;
/* see if a progress report needed
*/
if (progrpt_enabledpr) {
time32_t now = time(0);
bool_t need_progrptpr = BOOL_FALSE;
while (now >= progrpt_deadline) {
need_progrptpr = BOOL_TRUE;
progrpt_deadline += progrpt_interval;
}
if (need_progrptpr) {
size_t statlinecnt;
char **statline;
ix_t i;
statlinecnt = content_statline(&statline);
for (i = 0; i < statlinecnt; i++) {
mlog(MLOG_NORMAL,
statline[i]);
}
}
}
/* Progress report only */
if (flg == PREEMPT_PROGRESSONLY) {
return BOOL_FALSE;
}
/* signals not caught if in a pipeline
*/
if (pipeline) {
return BOOL_FALSE;
}
/* release signals momentarily to let any pending ones
* invoke signal handler and set flags
*/
sigpending(&pending_set);
for (i = 0; i < num_sigs; i++) {
if (sigismember(&pending_set, sigs[i]) == 1) {
sigfillset(&handle_set);
sigdelset(&handle_set, sigs[i]);
sigsuspend(&handle_set);
}
}
preempt_requested = BOOL_FALSE;
if (sigint_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGINT received (preempt)\n");
if (dlog_allowed()) {
preempt_requested = sigint_dialog();
} else {
preempt_requested = BOOL_TRUE;
}
/* important that this appear after dialog.
* allows dialog to be terminated with SIGINT,
* without infinite loop.
*/
sigint_received = BOOL_FALSE;
}
if (sighup_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGHUP received (prempt)\n");
preempt_requested = BOOL_TRUE;
sighup_received = BOOL_FALSE;
}
if (sigterm_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGTERM received (prempt)\n");
preempt_requested = BOOL_TRUE;
sigterm_received = BOOL_FALSE;
}
if (sigquit_received) {
mlog(MLOG_DEBUG | MLOG_PROC,
"SIGQUIT received (preempt)\n");
preempt_requested = BOOL_TRUE;
sigquit_received = BOOL_FALSE;
}
return preempt_requested;
}
/* definition of locally defined static functions ****************************/
static bool_t
loadoptfile(int *argcp, char ***argvp)
{
char *optfilename;
ix_t optfileix = 0;
int fd;
size_t sz;
int i;
struct stat64 stat;
char *argbuf;
char *p;
size_t tokencnt;
int nread;
const char *sep = " \t\n\r";
char **newargv;
int c;
int rval;
/* see if option specified
*/
optind = 1;
opterr = 0;
optfilename = 0;
while ((c = getopt(*argcp, *argvp, GETOPT_CMDSTRING)) != EOF) {
switch (c) {
case GETOPT_OPTFILE:
if (!optarg || optarg[0] == '-') {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c argument missing\n"),
c);
usage();
return BOOL_FALSE;
}
if (optfilename) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_NOLOCK,
_("-%c allowed only once\n"),
c);
usage();
return BOOL_FALSE;
}
optfilename = optarg;
assert(optind > 2);
optfileix = (ix_t)optind - 2;
break;
}
}
if (!optfilename) {
return BOOL_TRUE;
}
/* attempt to open the option file
*/
errno = 0;
fd = open(optfilename, O_RDONLY);
if (fd < 0) {
mlog(MLOG_ERROR | MLOG_NOLOCK,
_("cannot open option file %s: %s (%d)\n"),
optfilename,
strerror(errno),
errno);
return BOOL_FALSE;
}
/* get file status
*/
rval = fstat64(fd, &stat);
if (rval) {
mlog(MLOG_ERROR | MLOG_NOLOCK,
_("cannot stat option file %s: %s (%d)\n"),
optfilename,
strerror(errno),
errno);
close(fd);
return BOOL_FALSE;
}
/* ensure the file is ordinary
*/
if ((stat.st_mode & S_IFMT) != S_IFREG) {
mlog(MLOG_ERROR | MLOG_NOLOCK,
_("given option file %s is not ordinary file\n"),
optfilename);
close(fd);
return BOOL_FALSE;
}
/* calculate the space required for the cmd line options.
* skip the GETOPT_OPTFILE option which put us here!
*/
sz = 0;
for (i = 0; i < *argcp; i++) {
if (i == (int)optfileix) {
i++; /* to skip option argument */
continue;
}
sz += strlen((*argvp)[i]) + 1;
}
/* add in the size of the option file (plus one byte in case
* option file ends without newline, and one NULL for safety)
*/
sz += (size_t)stat.st_size + 2;
/* allocate an argument buffer
*/
argbuf = (char *)malloc(sz);
assert(argbuf);
/* copy arg0 (the executable's name) in first
*/
p = argbuf;
i = 0;
sprintf(p, "%s ", ( * argvp)[ i]);
p += strlen((*argvp)[i]) + 1;
i++;
/* copy the options file into the buffer after the given args
*/
nread = read(fd, (void *)p, (size_t)stat.st_size);
if (nread < 0) {
mlog(MLOG_ERROR | MLOG_NOLOCK,
_("read of option file %s failed: %s (%d)\n"),
optfilename,
strerror(errno),
errno);
close(fd);
return BOOL_FALSE;
}
assert((off64_t)nread == stat.st_size);
p += (size_t)stat.st_size;
*p++ = ' ';
/* copy the remaining command line args into the buffer
*/
for (; i < *argcp; i++) {
if (i == (int)optfileix) {
i++; /* to skip option argument */
continue;
}
sprintf(p, "%s ", ( * argvp)[ i]);
p += strlen((*argvp)[i]) + 1;
}
/* null-terminate the entire buffer
*/
*p++ = 0;
assert((size_t)(p - argbuf) <= sz);
/* change newlines and carriage returns into spaces
*/
for (p = argbuf; *p; p++) {
if (strchr("\n\r", ( int)( *p))) {
*p = ' ';
}
}
/* count the tokens in the buffer
*/
tokencnt = 0;
p = argbuf;
for (;;) {
/* start at the first non-separator character
*/
while (*p && strchr(sep, (int)(*p))) {
p++;
}
/* done when NULL encountered
*/
if (!*p) {
break;
}
/* we have a token
*/
tokencnt++;
/* find the end of the first token
*/
p = strpbrkquotes(p, sep);
/* if no more separators, all tokens seen
*/
if (!p) {
break;
}
}
/* if no arguments, can return now
*/
if (!tokencnt) {
close(fd);
return BOOL_TRUE;
}
/* allocate a new argv array to hold the tokens
*/
newargv = (char **)calloc(tokencnt, sizeof(char *));
assert(newargv);
/* null-terminate tokens and place in new argv, after
* extracting quotes and escapes
*/
p = argbuf;
for (i = 0 ; ; i++) {
char *endp = 0;
/* start at the first non-separator character
*/
while (*p && strchr(sep, (int)*p)) {
p++;
}
/* done when NULL encountered
*/
if (!*p) {
break;
}
/* better not disagree with counting scan!
*/
assert(i < (int)tokencnt);
/* find the end of the first token
*/
endp = strpbrkquotes(p, sep);
/* null-terminate if needed
*/
if (endp) {
*endp = 0;
}
/* strip quotes and escapes
*/
p = stripquotes(p);
/* stick result in new argv array
*/
newargv[i] = p;
/* if no more separators, all tokens seen
*/
if (!endp) {
break;
}
p = endp + 1;
}
/* return new argc anr argv
*/
close(fd);
*argcp = (int)tokencnt;
*argvp = newargv;
return BOOL_TRUE;
}
/* parent and children share this handler.
*/
static void
sighandler(int signo)
{
/* dialog gets first crack at the signal
*/
if (dlog_sighandler(signo))
return;
/* if in pipeline, don't do anything risky. just quit.
*/
if (pipeline) {
int rval;
mlog(MLOG_TRACE | MLOG_NOTE | MLOG_NOLOCK | MLOG_PROC,
_("received signal %d (%s): cleanup and exit\n"),
signo,
sig_numstring(signo));
if (content_complete()) {
rval = EXIT_NORMAL;
} else {
rval = EXIT_INTERRUPT;
}
mlog_exit(rval, RV_NONE);
exit(rval);
}
switch (signo) {
case SIGHUP:
/* immediately disable further dialogs
*/
dlog_desist();
sighup_received = BOOL_TRUE;
break;
case SIGTERM:
/* immediately disable further dialogs
*/
dlog_desist();
sigterm_received = BOOL_TRUE;
break;
case SIGINT:
sigint_received = BOOL_TRUE;
break;
case SIGQUIT:
/* immediately disable further dialogs
*/
dlog_desist();
sigquit_received = BOOL_TRUE;
break;
case SIGALRM:
case SIGUSR1:
break;
default:
sigstray_received = signo;
break;
}
}
static int
childmain(void *arg1)
{
ix_t stix;
int exitcode;
drive_t *drivep;
/* Determine which stream I am.
*/
stix = (ix_t)arg1;
/* tell the content manager to begin.
*/
#ifdef DUMP
exitcode = content_stream_dump(stix);
#endif /* DUMP */
#ifdef RESTORE
exitcode = content_stream_restore(stix);
#endif /* RESTORE */
/* let the drive manager shut down its worker thread
*/
drivep = drivepp[stix];
(*drivep->d_opsp->do_quit)(drivep);
return exitcode;
}
/* ARGSUSED */
static void
prompt_prog_cb(void *uctxp, dlog_pcbp_t pcb, void *pctxp)
{
/* query: ask for a dump label
*/
(*pcb)(pctxp,
progrpt_enabledpr
?
_("please enter seconds between progress reports, "
"or 0 to disable")
:
_("please enter seconds between progress reports"));
}
/* SIGINTR dialog
*
* side affect is to change verbosity level.
* return code of BOOL_TRUE indicates a stop was requested.
*/
#define PREAMBLEMAX (7 + 2 * STREAM_SIMMAX)
#define QUERYMAX 3
#define CHOICEMAX 9
#define ACKMAX 7
#define POSTAMBLEMAX 3
static bool_t
sigint_dialog(void)
{
fold_t fold;
char **statline;
ix_t i;
size_t statlinecnt;
char *preamblestr[PREAMBLEMAX];
size_t preamblecnt;
char *querystr[QUERYMAX];
size_t querycnt;
char *choicestr[CHOICEMAX];
size_t choicecnt;
char *ackstr[ACKMAX];
size_t ackcnt;
char *postamblestr[POSTAMBLEMAX];
size_t postamblecnt;
size_t interruptix;
size_t verbosityix;
size_t metricsix;
size_t controlix;
size_t ioix;
size_t mediachangeix;
#ifdef RESTORE
size_t piix;
size_t roix;
#endif /* RESTORE */
size_t progix;
size_t mllevix;
size_t mlssix;
size_t mltsix;
size_t continueix;
size_t allix;
size_t nochangeix;
size_t responseix;
int ssselected = 0;
bool_t stop_requested = BOOL_FALSE;
/* preamble: the content status line, indicate if interrupt happening
*/
fold_init(fold, _("status and control dialog"), '=');
statlinecnt = content_statline(&statline);
preamblecnt = 0;
preamblestr[preamblecnt++ ] = "\n";
preamblestr[preamblecnt++] = fold;
preamblestr[preamblecnt++ ] = "\n";
preamblestr[preamblecnt++ ] = "\n";
for (i = 0; i < statlinecnt; i++) {
preamblestr[preamblecnt++] = statline[i];
}
if (stop_in_progress) {
preamblestr[preamblecnt++] =
_("\nsession interrupt in progress\n");
}
preamblestr[preamblecnt++ ] = "\n";
assert(preamblecnt <= PREAMBLEMAX);
dlog_begin(preamblestr, preamblecnt);
/* top-level query: a function of session interrupt status
*/
querycnt = 0;
querystr[querycnt++ ] = _("please select one of "
"the following operations\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
if (!stop_in_progress) {
interruptix = choicecnt;
choicestr[choicecnt++ ] = _("interrupt this session");
} else {
interruptix = SIZEMAX; /* never happen */
}
verbosityix = choicecnt;
choicestr[choicecnt++ ] = _("change verbosity");
metricsix = choicecnt;
choicestr[choicecnt++ ] = _("display metrics");
if (content_media_change_needed) {
mediachangeix = choicecnt;
choicestr[choicecnt++ ] = _("confirm media change");
} else {
mediachangeix = SIZEMAX; /* never happen */
}
controlix = choicecnt;
choicestr[choicecnt++ ] = _("other controls");
continueix = choicecnt;
choicestr[choicecnt++ ] = _("continue");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
continueix, /* defaultix */
DLOG_TIMEOUT, /* timeout */
continueix, /* timeout ix */
continueix, /* sigint ix */
continueix, /* sighup ix */
continueix); /* sigquit ix */
if (responseix == interruptix) {
ackcnt = 0;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
querycnt = 0;
querystr[querycnt++ ] = _("please confirm\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
interruptix = choicecnt;
choicestr[choicecnt++ ] = _("interrupt this session");
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("continue");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
nochangeix, /* defaultix */
DLOG_TIMEOUT,/* timeout */
nochangeix, /* timeout ix */
nochangeix, /* sigint ix */
nochangeix, /* sighup ix */
nochangeix);/* sigquit ix */
ackcnt = 0;
if (responseix == nochangeix) {
ackstr[ackcnt++ ] = _("continuing\n");
} else {
ackstr[ackcnt++ ] = _("interrupt request accepted\n");
stop_requested = BOOL_TRUE;
}
dlog_multi_ack(ackstr,
ackcnt);
} else if (responseix == verbosityix) {
ackcnt = 0;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
querycnt = 0;
querystr[querycnt++ ] = _("please select one of "
"the following subsystems\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
/* number of lines must match number of subsystems
*/
for (choicecnt = 0; choicecnt < MLOG_SS_CNT; choicecnt++) {
choicestr[choicecnt] = mlog_ss_names[choicecnt];
}
allix = choicecnt;
choicestr[choicecnt++ ] = _("all of the above");
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("no change");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
allix, /* defaultix */
DLOG_TIMEOUT,/* timeout */
nochangeix, /* timeout ix */
nochangeix, /* sigint ix */
nochangeix, /* sighup ix */
nochangeix);/* sigquit ix */
ackcnt = 0;
if (responseix == nochangeix) {
ackstr[ackcnt++ ] = _("no change\n");
} else if (responseix == allix) {
ssselected = -1;
ackstr[ackcnt++ ] = _("all subsystems selected\n\n");
} else {
ssselected = (int)responseix;
ackstr[ackcnt++ ] = "\n";
}
dlog_multi_ack(ackstr,
ackcnt);
if (responseix != nochangeix) {
querycnt = 0;
querystr[querycnt++ ] = ("please select one of the "
"following verbosity levels\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
choicestr[choicecnt++ ] = _("silent");
choicestr[choicecnt++ ] = _("verbose");
choicestr[choicecnt++ ] = _("trace");
choicestr[choicecnt++ ] = _("debug");
choicestr[choicecnt++ ] = _("nitty");
choicestr[choicecnt++ ] = _("nitty + 1");
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("no change");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
ssselected == -1
?
0
:
_(" (current)"),/* hilitestr */
ssselected == -1
?
IXMAX
:
(ix_t)mlog_level_ss[ssselected], /* hiliteix */
0, /* defaultstr */
nochangeix,/* defaultix */
DLOG_TIMEOUT,/* timeout */
nochangeix, /* timeout ix */
nochangeix, /* sigint ix */
nochangeix, /* sighup ix */
nochangeix);/* sigquit ix */
ackcnt = 0;
if (responseix == nochangeix
||
(ssselected >= 0
&&
responseix
==
(ix_t)mlog_level_ss[ssselected])) {
ackstr[ackcnt++ ] = _("no change\n");
} else {
if (ssselected < 0) {
ix_t ssix;
assert(ssselected == -1);
for (ssix = 0
;
ssix < MLOG_SS_CNT
;
ssix++) {
mlog_level_ss[ssix] =
(int)responseix;
}
} else {
mlog_level_ss[ssselected] =
(int)responseix;
}
ackstr[ackcnt++ ] = _("level changed\n");
}
dlog_multi_ack(ackstr,
ackcnt);
}
} else if (responseix == metricsix) {
ackcnt = 0;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
querycnt = 0;
querystr[querycnt++ ] = _("please select one of "
"the following metrics\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
ioix = choicecnt;
choicestr[choicecnt++ ] = _("I/O");
#ifdef RESTORE
piix = choicecnt;
choicestr[choicecnt++ ] = _("media inventory status");
roix = choicecnt;
choicestr[choicecnt++ ] = _("needed media objects");
#endif /* RESTORE */
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("continue");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
nochangeix, /* defaultix */
DLOG_TIMEOUT,
nochangeix, /* timeout ix */
nochangeix, /* sigint ix */
nochangeix, /* sighup ix */
nochangeix);/* sigquit ix */
if (responseix != nochangeix) {
ackcnt = 0;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
}
if (responseix == ioix) {
drive_display_metrics();
#ifdef RESTORE
} else if (responseix == piix) {
content_showinv();
} else if (responseix == roix) {
content_showremainingobjects();
#endif /* RESTORE */
}
if (responseix != nochangeix) {
querycnt = 0;
querystr[querycnt++ ] = "\n";
assert(querycnt <= QUERYMAX);
choicecnt = 0;
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("continue");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
nochangeix,/* defaultix*/
DLOG_TIMEOUT,
nochangeix,/*timeout ix*/
nochangeix,/* sigint ix*/
nochangeix,/* sighup ix*/
nochangeix);/*sigquitix*/
}
ackcnt = 0;
ackstr[ackcnt++ ] = _("continuing\n");
dlog_multi_ack(ackstr,
ackcnt);
} else if (responseix == mediachangeix) {
ackcnt = 0;
dlog_multi_ack(ackstr,
ackcnt);
ackcnt = 0;
ackstr[ackcnt++] = content_mediachange_query();
dlog_multi_ack(ackstr,
ackcnt);
} else if (responseix == controlix) {
ackcnt = 0;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
querycnt = 0;
querystr[querycnt++ ] = _("please select one of "
"the following controls\n");
assert(querycnt <= QUERYMAX);
choicecnt = 0;
progix = choicecnt;
if (progrpt_enabledpr) {
choicestr[choicecnt++ ] = _("change interval of "
"or disable progress reports");
} else {
choicestr[choicecnt++ ] = _("enable progress reports");
}
mllevix = choicecnt;
if (mlog_showlevel) {
choicestr[choicecnt++ ] = _("hide log message levels");
} else {
choicestr[choicecnt++ ] = _("show log message levels");
}
mlssix = choicecnt;
if (mlog_showss) {
choicestr[choicecnt++ ] = _("hide log message subsystems");
} else {
choicestr[choicecnt++ ] = _("show log message subsystems");
}
mltsix = choicecnt;
if (mlog_timestamp) {
choicestr[choicecnt++ ] = _("hide log message timestamps");
} else {
choicestr[choicecnt++ ] = _("show log message timestamps");
}
nochangeix = choicecnt;
choicestr[choicecnt++ ] = _("continue");
assert(choicecnt <= CHOICEMAX);
responseix = dlog_multi_query(querystr,
querycnt,
choicestr,
choicecnt,
0, /* hilitestr */
IXMAX, /* hiliteix */
0, /* defaultstr */
nochangeix, /* defaultix */
DLOG_TIMEOUT,
nochangeix, /* timeout ix */
nochangeix, /* sigint ix */
nochangeix, /* sighup ix */
nochangeix);/* sigquit ix */
ackcnt = 0;
if (responseix == progix) {
char buf[10];
const size_t ncix = 1;
const size_t okix = 2;
ackstr[ackcnt++ ] = "\n";
dlog_multi_ack(ackstr,
ackcnt);
ackcnt = 0;
responseix = dlog_string_query(prompt_prog_cb,
0,
buf,
sizeof(buf),
DLOG_TIMEOUT,
ncix,/* timeout ix */
ncix, /* sigint ix */
ncix, /* sighup ix */
ncix, /* sigquit ix */
okix);
if (responseix == okix) {
int newinterval;
newinterval = atoi(buf);
if (!strlen(buf)) {
ackstr[ackcnt++ ] = _("no change\n");
} else if (newinterval > 0) {
time32_t newdeadline;
char intervalbuf[64];
newdeadline = time(0) + (time32_t)newinterval;
if (progrpt_enabledpr) {
if ((time32_t)newinterval == progrpt_interval) {
ackstr[ackcnt++ ] = _("no change\n");
} else {
ackstr[ackcnt++ ] = _("changing progress report interval to ");
sprintf(intervalbuf,
_("%d seconds\n"),
newinterval);
assert(strlen(intervalbuf)
<
sizeof(intervalbuf));
ackstr[ackcnt++] = intervalbuf;
if (progrpt_deadline > newdeadline) {
progrpt_deadline = newdeadline;
}
}
} else {
ackstr[ackcnt++ ] = _("enabling progress reports at ");
sprintf(intervalbuf,
_("%d second intervals\n"),
newinterval);
assert(strlen(intervalbuf)
<
sizeof(intervalbuf));
ackstr[ackcnt++] = intervalbuf;
progrpt_enabledpr = BOOL_TRUE;
progrpt_deadline = newdeadline;
}
progrpt_interval = (time32_t)newinterval;
} else {
if (progrpt_enabledpr) {
ackstr[ackcnt++ ] = _("disabling progress reports\n");
} else {
ackstr[ackcnt++ ] = _("no change\n");
}
progrpt_enabledpr = BOOL_FALSE;
}
} else {
ackstr[ackcnt++ ] = _("no change\n");
}
} else if (responseix == mllevix) {
mlog_showlevel = !mlog_showlevel;
if (mlog_showlevel) {
ackstr[ackcnt++ ] = _("showing log message levels\n");
} else {
ackstr[ackcnt++ ] = _("hiding log message levels\n");
}
} else if (responseix == mlssix) {
mlog_showss = !mlog_showss;
if (mlog_showss) {
ackstr[ackcnt++ ] = _("showing log message subsystems\n");
} else {
ackstr[ackcnt++ ] = _("hiding log message subsystems\n");
}
} else if (responseix == mltsix) {
mlog_timestamp = !mlog_timestamp;
if (mlog_timestamp) {
ackstr[ackcnt++ ] = _("showing log message timestamps\n");
} else {
ackstr[ackcnt++ ] = _("hiding log message timestamps\n");
}
}
dlog_multi_ack(ackstr,
ackcnt);
} else {
ackcnt = 0;
ackstr[ackcnt++ ] = _("continuing\n");
dlog_multi_ack(ackstr,
ackcnt);
}
fold_init(fold, _("end dialog"), '-');
postamblecnt = 0;
postamblestr[postamblecnt++ ] = "\n";
postamblestr[postamblecnt++] = fold;
postamblestr[postamblecnt++ ] = "\n\n";
assert(postamblecnt <= POSTAMBLEMAX);
dlog_end(postamblestr,
postamblecnt);
return stop_requested;
}
static char *
sigintstr(void)
{
int ttyfd;
static char buf[20];
struct termios termios;
cc_t intchr;
int rval;
ttyfd = dlog_fd();
if (ttyfd == -1) {
return 0;
}
rval = tcgetattr(ttyfd, &termios);
if (rval) {
mlog(MLOG_NITTY | MLOG_PROC,
"could not get controlling terminal information: %s\n",
strerror(errno));
return 0;
}
intchr = termios.c_cc[VINTR];
mlog(MLOG_NITTY | MLOG_PROC,
"tty fd: %d; terminal interrupt character: %c (0%o)\n",
ttyfd,
intchr,
intchr);
if (intchr < ' ') {
sprintf(buf, "^%c", intchr + '@');
} else if (intchr == 0177) {
sprintf(buf, "DEL");
} else {
sprintf(buf, "%c", intchr);
}
assert(strlen(buf) < sizeof(buf));
return buf;
}
#ifdef DUMP
static bool_t
set_rlimits(void)
#endif /* DUMP */
#ifdef RESTORE
static bool_t
set_rlimits(size64_t *vmszp)
#endif /* RESTORE */
{
struct rlimit64 rlimit64;
#ifdef RESTORE
size64_t vmsz;
#endif /* RESTORE */
/* REFERENCED */
int rval;
assert(minstacksz <= maxstacksz);
rval = getrlimit64(RLIMIT_AS, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_AS org cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
#ifdef RESTORE
if (rlimit64.rlim_cur != RLIM64_INFINITY) {
rlimit64.rlim_cur = rlimit64.rlim_max;
(void)setrlimit64(RLIMIT_AS, &rlimit64);
rval = getrlimit64(RLIMIT_AS, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_VMEM now cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
}
vmsz = (size64_t)rlimit64.rlim_cur;
#endif /* RESTORE */
assert(minstacksz <= maxstacksz);
rval = getrlimit64(RLIMIT_STACK, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_STACK org cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
if (rlimit64.rlim_cur < minstacksz) {
if (rlimit64.rlim_max < minstacksz) {
mlog(MLOG_DEBUG
|
MLOG_NOLOCK
|
MLOG_PROC,
"raising stack size hard limit "
"from 0x%llx to 0x%llx\n",
rlimit64.rlim_max,
minstacksz);
rlimit64.rlim_cur = minstacksz;
rlimit64.rlim_max = minstacksz;
(void)setrlimit64(RLIMIT_STACK, &rlimit64);
rval = getrlimit64(RLIMIT_STACK, &rlimit64);
assert(!rval);
if (rlimit64.rlim_cur < minstacksz) {
mlog(MLOG_NORMAL
|
MLOG_WARNING
|
MLOG_NOLOCK
|
MLOG_PROC,
_("unable to raise stack size hard limit "
"from 0x%llx to 0x%llx\n"),
rlimit64.rlim_max,
minstacksz);
}
} else {
mlog(MLOG_DEBUG
|
MLOG_NOLOCK
|
MLOG_PROC,
"raising stack size soft limit "
"from 0x%llx to 0x%llx\n",
rlimit64.rlim_cur,
minstacksz);
rlimit64.rlim_cur = minstacksz;
(void)setrlimit64(RLIMIT_STACK, &rlimit64);
rval = getrlimit64(RLIMIT_STACK, &rlimit64);
assert(!rval);
if (rlimit64.rlim_cur < minstacksz) {
mlog(MLOG_NORMAL
|
MLOG_WARNING
|
MLOG_NOLOCK
|
MLOG_PROC,
_("unable to raise stack size soft limit "
"from 0x%llx to 0x%llx\n"),
rlimit64.rlim_cur,
minstacksz);
}
}
} else if (rlimit64.rlim_cur > maxstacksz) {
mlog(MLOG_DEBUG
|
MLOG_NOLOCK
|
MLOG_PROC,
"lowering stack size soft limit "
"from 0x%llx to 0x%llx\n",
rlimit64.rlim_cur,
maxstacksz);
rlimit64.rlim_cur = maxstacksz;
(void)setrlimit64(RLIMIT_STACK, &rlimit64);
rval = getrlimit64(RLIMIT_STACK, &rlimit64);
assert(!rval);
if (rlimit64.rlim_cur > maxstacksz) {
mlog(MLOG_NORMAL
|
MLOG_WARNING
|
MLOG_NOLOCK
|
MLOG_PROC,
_("unable to lower stack size soft limit "
"from 0x%llx to 0x%llx\n"),
rlimit64.rlim_cur,
maxstacksz);
}
}
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_STACK new cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
rval = getrlimit64(RLIMIT_DATA, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_DATA org cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
rval = getrlimit64(RLIMIT_FSIZE, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_FSIZE org cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
rlimit64.rlim_cur = rlimit64.rlim_max;
(void)setrlimit64(RLIMIT_FSIZE, &rlimit64);
rlimit64.rlim_cur = RLIM64_INFINITY;
(void)setrlimit64(RLIMIT_FSIZE, &rlimit64);
rval = getrlimit64(RLIMIT_FSIZE, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_FSIZE now cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
rval = getrlimit64(RLIMIT_CPU, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_CPU cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
rlimit64.rlim_cur = rlimit64.rlim_max;
(void)setrlimit64(RLIMIT_CPU, &rlimit64);
rval = getrlimit64(RLIMIT_CPU, &rlimit64);
assert(!rval);
mlog(MLOG_NITTY | MLOG_NOLOCK | MLOG_PROC,
"RLIMIT_CPU now cur 0x%llx max 0x%llx\n",
rlimit64.rlim_cur,
rlimit64.rlim_max);
#ifdef RESTORE
*vmszp = vmsz;
#endif /* RESTORE */
return BOOL_TRUE;
}
struct sig_printmap {
int num;
char *string;
};
typedef struct sig_printmap sig_printmap_t;
static sig_printmap_t sig_printmap[] = {
{SIGHUP, "SIGHUP"},
{SIGINT, "SIGINT"},
{SIGQUIT, "SIGQUIT"},
{SIGILL, "SIGILL"},
{SIGABRT, "SIGABRT"},
{SIGFPE, "SIGFPE"},
{SIGBUS, "SIGBUS"},
{SIGSEGV, "SIGSEGV"},
#ifdef SIGSYS
{SIGSYS, "SIGSYS"},
#endif
{SIGPIPE, "SIGPIPE"},
{SIGALRM, "SIGALRM"},
{SIGTERM, "SIGTERM"},
{SIGUSR1, "SIGUSR1"},
{SIGUSR2, "SIGUSR2"},
{SIGCHLD, "SIGCHLD"},
{SIGPWR, "SIGPWR"},
{SIGURG, "SIGURG"},
{SIGPOLL, "SIGPOLL"},
{SIGXCPU, "SIGXCPU"},
{SIGXFSZ, "SIGXFSZ"},
#if HIDDEN
{SIGRTMIN, "SIGRTMIN"},
{SIGRTMAX, "SIGRTMAX"},
#endif
{0, "???"}
};
static char *
sig_numstring(int num)
{
sig_printmap_t *p = sig_printmap;
sig_printmap_t *endp = sig_printmap
+
(sizeof(sig_printmap)
/
sizeof(sig_printmap[0]));
for (; p < endp; p++) {
if (p->num == num) {
return p->string;
}
}
return "???";
}
static char *
strpbrkquotes(char *p, const char *sep)
{
bool_t prevcharwasbackslash = BOOL_FALSE;
bool_t inquotes = BOOL_FALSE;
for (;; p++) {
if (*p == 0) {
return 0;
}
if (*p == '\\') {
if (!prevcharwasbackslash) {
prevcharwasbackslash = BOOL_TRUE;
} else {
prevcharwasbackslash = BOOL_FALSE;
}
continue;
}
if (*p == '"') {
if (prevcharwasbackslash) {
prevcharwasbackslash = BOOL_FALSE;
continue;
}
if (inquotes) {
inquotes = BOOL_FALSE;
} else {
inquotes = BOOL_TRUE;
}
continue;
}
if (!inquotes) {
if (strchr(sep, (int)(*p))) {
return p;
}
}
prevcharwasbackslash = BOOL_FALSE;
}
/* NOTREACHED */
}
static char *
stripquotes(char *p)
{
size_t len = strlen(p);
char *endp;
char *nextp;
bool_t justremovedbackslash;
if (len > 2 && p[0 ] == '"') {
p++;
len--;
if (len && p[len - 1 ] == '"') {
p[len - 1] = 0;
len--;
}
}
endp = p + len;
justremovedbackslash = BOOL_FALSE;
for (nextp = p; nextp < endp;) {
if (*nextp == '\\' && !justremovedbackslash) {
shiftleftby1(nextp, endp);
endp--;
justremovedbackslash = BOOL_TRUE;
} else {
justremovedbackslash = BOOL_FALSE;
nextp++;
}
}
return p;
}
static void
shiftleftby1(char *p, char *endp)
{
for (; p < endp; p++) {
*p = p[1];
}
}
|