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
|
/*********************************************************
* Copyright (C) 1998 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program 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 Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/utsname.h>
#include <netdb.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <pwd.h>
#include <sys/resource.h>
#if defined(sun)
#include <sys/systeminfo.h>
#endif
#include <sys/socket.h>
#if defined(__FreeBSD__) || defined(__APPLE__)
# include <sys/sysctl.h>
#endif
#if defined(__APPLE__)
#define SYS_NMLN _SYS_NAMELEN
#include <assert.h>
#include <CoreServices/CoreServices.h>
#include <mach-o/dyld.h>
#include <mach/host_info.h>
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#elif defined(__FreeBSD__)
#if !defined(RLIMIT_AS)
# if defined(RLIMIT_VMEM)
# define RLIMIT_AS RLIMIT_VMEM
# else
# define RLIMIT_AS RLIMIT_RSS
# endif
#endif
#else
#if !defined(USING_AUTOCONF) || defined(HAVE_SYS_VFS_H)
#include <sys/vfs.h>
#endif
#if !defined(sun) && (!defined(USING_AUTOCONF) || (defined(HAVE_SYS_IO_H) && defined(HAVE_SYS_SYSINFO_H)))
#include <sys/io.h>
#include <sys/sysinfo.h>
#ifndef HAVE_SYSINFO
#define HAVE_SYSINFO 1
#endif
#endif
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <paths.h>
#endif
#if !defined(_PATH_DEVNULL)
#define _PATH_DEVNULL "/dev/null"
#endif
#include "vmware.h"
#include "hostType.h"
#include "hostinfo.h"
#include "safetime.h"
#include "vm_version.h"
#include "str.h"
#include "msg.h"
#include "log.h"
#include "posix.h"
#include "file.h"
#include "backdoor_def.h"
#include "util.h"
#include "vmstdio.h"
#include "su.h"
#include "vm_atomic.h"
#if defined(__i386__) || defined(__x86_64__)
#include "x86cpuid.h"
#endif
#include "syncMutex.h"
#include "unicode.h"
#include "guest_os.h"
#include "dynbuf.h"
#include "strutil.h"
#if defined(VMX86_SERVER)
#include "uwvmkAPI.h"
#include "uwvmk.h"
#include "vmkSyscall.h"
#endif
#define LGPFX "HOSTINFO:"
#define MAX_LINE_LEN 128
#define SYSTEM_BITNESS_32 "i386"
#define SYSTEM_BITNESS_64_SUN "amd64"
#define SYSTEM_BITNESS_64_LINUX "x86_64"
#define SYSTEM_BITNESS_MAXLEN \
MAX(sizeof SYSTEM_BITNESS_32, \
MAX(sizeof SYSTEM_BITNESS_64_SUN, \
sizeof SYSTEM_BITNESS_64_LINUX))
static Bool hostinfoOSVersionInitialized;
#if defined(__APPLE__)
#define SYS_NMLN _SYS_NAMELEN
#endif
static int hostinfoOSVersion[4];
static char hostinfoOSVersionString[SYS_NMLN];
#define DISTRO_BUF_SIZE 255
typedef struct lsb_distro_info {
char *name;
char *scanstring;
} LSBDistroInfo;
static LSBDistroInfo lsbFields[] = {
{"DISTRIB_ID=", "DISTRIB_ID=%s"},
{"DISTRIB_RELEASE=", "DISTRIB_RELEASE=%s"},
{"DISTRIB_CODENAME=", "DISTRIB_CODENAME=%s"},
{"DISTRIB_DESCRIPTION=", "DISTRIB_DESCRIPTION=%s"},
{NULL, NULL},
};
typedef struct distro_info {
char *name;
char *filename;
} DistroInfo;
static DistroInfo distroArray[] = {
{"RedHat", "/etc/redhat-release"},
{"RedHat", "/etc/redhat_version"},
{"Sun", "/etc/sun-release"},
{"SuSE", "/etc/SuSE-release"},
{"SuSE", "/etc/novell-release"},
{"SuSE", "/etc/sles-release"},
{"Debian", "/etc/debian_version"},
{"Debian", "/etc/debian_release"},
{"Mandrake", "/etc/mandrake-release"},
{"Mandriva", "/etc/mandriva-release"},
{"Mandrake", "/etc/mandrakelinux-release"},
{"TurboLinux", "/etc/turbolinux-release"},
{"Fedora Core", "/etc/fedora-release"},
{"Gentoo", "/etc/gentoo-release"},
{"Novell", "/etc/nld-release"},
{"Ubuntu", "/etc/lsb-release"},
{"Annvix", "/etc/annvix-release"},
{"Arch", "/etc/arch-release"},
{"Arklinux", "/etc/arklinux-release"},
{"Aurox", "/etc/aurox-release"},
{"BlackCat", "/etc/blackcat-release"},
{"Cobalt", "/etc/cobalt-release"},
{"Conectiva", "/etc/conectiva-release"},
{"Immunix", "/etc/immunix-release"},
{"Knoppix", "/etc/knoppix_version"},
{"Linux-From-Scratch", "/etc/lfs-release"},
{"Linux-PPC", "/etc/linuxppc-release"},
{"MkLinux", "/etc/mklinux-release"},
{"PLD", "/etc/pld-release"},
{"Slackware", "/etc/slackware-version"},
{"Slackware", "/etc/slackware-release"},
{"SMEServer", "/etc/e-smith-release"},
{"Solaris", "/etc/release"},
{"Tiny Sofa", "/etc/tinysofa-release"},
{"UltraPenguin", "/etc/ultrapenguin-release"},
{"UnitedLinux", "/etc/UnitedLinux-release"},
{"VALinux", "/etc/va-release"},
{"Yellow Dog", "/etc/yellowdog-release"},
{NULL, NULL},
};
/*
*----------------------------------------------------------------------
*
* HostinfoOSVersionInit --
*
* Compute the OS version information
*
* Results:
* None.
*
* Side effects:
* hostinfoOS* variables are filled in.
*
*----------------------------------------------------------------------
*/
static void
HostinfoOSVersionInit(void)
{
struct utsname u;
char extra[SYS_NMLN] = "";
if (hostinfoOSVersionInitialized) {
return;
}
if (uname(&u) < 0) {
Warning("%s unable to get host OS version (uname): %s\n",
__FUNCTION__, strerror(errno));
NOT_IMPLEMENTED();
}
Str_Strcpy(hostinfoOSVersionString, u.release, SYS_NMLN);
ASSERT(ARRAYSIZE(hostinfoOSVersion) >= 4);
if (sscanf(u.release, "%d.%d.%d%s",
&hostinfoOSVersion[0], &hostinfoOSVersion[1],
&hostinfoOSVersion[2], extra) < 1) {
Warning("%s unable to parse host OS version string: %s\n",
__FUNCTION__, u.release);
NOT_IMPLEMENTED();
}
/* If there is a 4th number, use it, otherwise use 0. */
if (sscanf(extra, ".%d%*s", &hostinfoOSVersion[3]) < 1) {
hostinfoOSVersion[3] = 0;
}
hostinfoOSVersionInitialized = TRUE;
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_OSVersionString --
*
* Returns the host version information as returned in the
* release field of uname(2)
*
* Results:
* const char * - pointer to static buffer containing the release
*
* Side effects:
* None
*
*----------------------------------------------------------------------
*/
const char *
Hostinfo_OSVersionString(void)
{
HostinfoOSVersionInit();
return hostinfoOSVersionString;
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_OSVersion --
*
* Host OS release info.
*
* Results:
* The i-th component of a dotted release string.
* 0 if i is greater than the number of components we support.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Hostinfo_OSVersion(int i)
{
HostinfoOSVersionInit();
return i < ARRAYSIZE(hostinfoOSVersion) ? hostinfoOSVersion[i] : 0;
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_GetTimeOfDay --
*
* Return the current time of day according to the host. We want
* UTC time (seconds since Jan 1, 1970).
*
* Results:
* Time of day in microseconds.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Hostinfo_GetTimeOfDay(VmTimeType *time)
{
struct timeval tv;
gettimeofday(&tv, NULL);
*time = ((int64)tv.tv_sec * 1000000) + tv.tv_usec;
}
/*
*----------------------------------------------------------------------------
*
* Hostinfo_GetSystemBitness --
*
* Determines the operating system's bitness.
*
* Return value:
* 32 or 64 on success.
* -1 on failure. Check errno for more details of error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
int
Hostinfo_GetSystemBitness(void)
{
#if defined __linux__
struct utsname u;
if (uname(&u) < 0) {
return -1;
}
if (strstr(u.machine, SYSTEM_BITNESS_64_LINUX)) {
return 64;
} else {
return 32;
}
#elif defined N_PLAT_NLM
return 32;
#else
char buf[SYSTEM_BITNESS_MAXLEN] = { '\0', };
# if defined __FreeBSD__ || defined __APPLE__
static int mib[2] = { CTL_HW, HW_MACHINE, };
size_t len = sizeof buf;
if (sysctl(mib, ARRAYSIZE(mib), buf, &len, NULL, 0) == -1) {
return -1;
}
# elif defined sun
# if !defined SOL10
/*
* XXX: This is bad. We define SI_ARCHITECTURE_K to what it is on Solaris
* 10 so that we can use a single guestd build for Solaris 9 and 10. In the
* future we should have the Solaris 9 build just return 32 -- since it did
* not support 64-bit x86 -- and let the Solaris 10 headers define
* SI_ARCHITECTURE_K, then have the installer symlink to the correct binary.
* For now, though, we'll share a single build for both versions.
*/
# define SI_ARCHITECTURE_K 518
# endif
if (sysinfo(SI_ARCHITECTURE_K, buf, sizeof buf) < 0) {
return -1;
}
# endif
if (strcmp(buf, SYSTEM_BITNESS_32) == 0) {
return 32;
} else if (strcmp(buf, SYSTEM_BITNESS_64_SUN) == 0 ||
strcmp(buf, SYSTEM_BITNESS_64_LINUX) == 0) {
return 64;
}
return -1;
#endif
}
/*
*-----------------------------------------------------------------------------
*
* HostinfoGetOSShortName --
*
* Returns distro information based on .vmx format (distroShort).
*
* Return value:
* Overwrited the short name if we recognise the OS.
* Otherwise leave the short name as it is.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static void
HostinfoGetOSShortName(char *distro, // IN: full distro name
char *distroShort, // OUT: short distro name
int distroShortSize) // IN: size of short distro name
{
char *distroLower = NULL; /* Lower case distro name */
distroLower = calloc(strlen(distro) + 1, sizeof *distroLower);
if (distroLower == NULL) {
Warning("%s: could not allocate memory\n", __FUNCTION__);
return;
}
Str_Strcpy(distroLower, distro, distroShortSize);
distroLower = Str_ToLower(distroLower);
if (strstr(distroLower, "red hat")) {
if (strstr(distroLower, "enterprise")) {
/*
* Looking for "release x" here instead of "x" as there could be
* build version which can be misleading. For example Red Hat
* Enterprise Linux ES release 4 (Nahant Update 3)
*/
int release = 0;
char *releaseStart = strstr(distroLower, "release");
if (releaseStart) {
sscanf(releaseStart, "release %d", &release);
if (release > 0) {
snprintf(distroShort, distroShortSize, STR_OS_RED_HAT_EN"%d",
release);
}
}
if (release <= 0) {
Warning("%s: could not read Red Hat Enterprise release version\n",
__FUNCTION__);
Str_Strcpy(distroShort, STR_OS_RED_HAT_EN, distroShortSize);
}
} else {
Str_Strcpy(distroShort, STR_OS_RED_HAT, distroShortSize);
}
} else if (strstr(distroLower, "suse")) {
if (strstr(distroLower, "enterprise")) {
if (strstr(distroLower, "server 11")) {
Str_Strcpy(distroShort, STR_OS_SLES_11, distroShortSize);
} else if (strstr(distroLower, "server 10")) {
Str_Strcpy(distroShort, STR_OS_SLES_10, distroShortSize);
} else {
Str_Strcpy(distroShort, STR_OS_SUSE_EN, distroShortSize);
}
} else if (strstr(distroLower, "sun")) {
Str_Strcpy(distroShort, STR_OS_SUN_DESK, distroShortSize);
} else if (strstr(distroLower, "novell")) {
Str_Strcpy(distroShort, STR_OS_NOVELL, distroShortSize);
} else {
Str_Strcpy(distroShort, STR_OS_SUSE, distroShortSize);
}
} else if (strstr(distroLower, "mandrake")) {
Str_Strcpy(distroShort, STR_OS_MANDRAKE, distroShortSize);
} else if (strstr(distroLower, "turbolinux")) {
Str_Strcpy(distroShort, STR_OS_TURBO, distroShortSize);
} else if (strstr(distroLower, "sun")) {
Str_Strcpy(distroShort, STR_OS_SUN_DESK, distroShortSize);
} else if (strstr(distroLower, "annvix")) {
Str_Strcpy(distroShort, STR_OS_ANNVIX, distroShortSize);
} else if (strstr(distroLower, "arch")) {
Str_Strcpy(distroShort, STR_OS_ARCH, distroShortSize);
} else if (strstr(distroLower, "arklinux")) {
Str_Strcpy(distroShort, STR_OS_ARKLINUX, distroShortSize);
} else if (strstr(distroLower, "aurox")) {
Str_Strcpy(distroShort, STR_OS_AUROX, distroShortSize);
} else if (strstr(distroLower, "black cat")) {
Str_Strcpy(distroShort, STR_OS_BLACKCAT, distroShortSize);
} else if (strstr(distroLower, "cobalt")) {
Str_Strcpy(distroShort, STR_OS_COBALT, distroShortSize);
} else if (strstr(distroLower, "conectiva")) {
Str_Strcpy(distroShort, STR_OS_CONECTIVA, distroShortSize);
} else if (strstr(distroLower, "debian")) {
Str_Strcpy(distroShort, STR_OS_DEBIAN, distroShortSize);
} else if (strstr(distroLower, "fedora")) {
Str_Strcpy(distroShort, STR_OS_FEDORA, distroShortSize);
} else if (strstr(distroLower, "gentoo")) {
Str_Strcpy(distroShort, STR_OS_GENTOO, distroShortSize);
} else if (strstr(distroLower, "immunix")) {
Str_Strcpy(distroShort, STR_OS_IMMUNIX, distroShortSize);
} else if (strstr(distroLower, "linux-from-scratch")) {
Str_Strcpy(distroShort, STR_OS_LINUX_FROM_SCRATCH, distroShortSize);
} else if (strstr(distroLower, "linux-ppc")) {
Str_Strcpy(distroShort, STR_OS_LINUX_PPC, distroShortSize);
} else if (strstr(distroLower, "mandriva")) {
Str_Strcpy(distroShort, STR_OS_MANDRIVA, distroShortSize);
} else if (strstr(distroLower, "mklinux")) {
Str_Strcpy(distroShort, STR_OS_MKLINUX, distroShortSize);
} else if (strstr(distroLower, "pld")) {
Str_Strcpy(distroShort, STR_OS_PLD, distroShortSize);
} else if (strstr(distroLower, "slackware")) {
Str_Strcpy(distroShort, STR_OS_SLACKWARE, distroShortSize);
} else if (strstr(distroLower, "sme server")) {
Str_Strcpy(distroShort, STR_OS_SMESERVER, distroShortSize);
} else if (strstr(distroLower, "tiny sofa")) {
Str_Strcpy(distroShort, STR_OS_TINYSOFA, distroShortSize);
} else if (strstr(distroLower, "ubuntu")) {
Str_Strcpy(distroShort, STR_OS_UBUNTU, distroShortSize);
} else if (strstr(distroLower, "ultra penguin")) {
Str_Strcpy(distroShort, STR_OS_ULTRAPENGUIN, distroShortSize);
} else if (strstr(distroLower, "united linux")) {
Str_Strcpy(distroShort, STR_OS_UNITEDLINUX, distroShortSize);
} else if (strstr(distroLower, "va linux")) {
Str_Strcpy(distroShort, STR_OS_VALINUX, distroShortSize);
} else if (strstr(distroLower, "yellow dog")) {
Str_Strcpy(distroShort, STR_OS_YELLOW_DOG, distroShortSize);
}
free(distroLower);
}
/*
*-----------------------------------------------------------------------------
*
* HostinfoReadDistroFile --
*
* Look for a distro version file /etc/xxx-release.
* Once found, read the file in and figure out which distribution.
*
* Return value:
* Returns TRUE on success and FALSE on failure.
* Returns distro information verbatium from /etc/xxx-release (distro).
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static Bool
HostinfoReadDistroFile(char *filename, // IN: distro version file name
int distroSize, // IN: size of OS distro name buffer
char *distro) // OUT: full distro name
{
int fd;
int buf_sz;
struct stat st;
Bool ret = FALSE;
char *distroOrig = NULL;
char distroPart[DISTRO_BUF_SIZE];
char *tmpDistroPos = NULL;
int i = 0;
if ((fd = open(filename, O_RDONLY)) < 0) {
Warning("%s: could not open file%s: %d\n", __FUNCTION__, filename, errno);
return FALSE;
}
if (fstat(fd, &st)) {
Warning("%s: could not stat the file %s: %d\n", __FUNCTION__, filename,
errno);
goto out;
}
if (st.st_size == 0) {
Warning("%s: Cannot work with empty file.\n", __FUNCTION__);
goto out;
}
buf_sz = st.st_size;
if (buf_sz >= distroSize) {
Warning("%s: input buffer too small\n", __FUNCTION__);
goto out;
}
distroOrig = calloc(distroSize, sizeof *distroOrig);
if (distroOrig == NULL) {
Warning("%s: could not allocate memory\n", __FUNCTION__);
close(fd);
return FALSE;
}
if (read(fd, distroOrig, buf_sz) != buf_sz) {
Warning("%s: could not read file %s: %d\n", __FUNCTION__, filename,
errno);
goto out;
}
distroOrig[buf_sz - 1] = '\0';
/*
* For the case where we do have a release file in the LSB format,
* but there is no LSB module, let's parse the LSB file for possible fields.
*/
distro[0] = '\0';
for (i = 0; lsbFields[i].name != NULL; i++) {
tmpDistroPos = strstr(distroOrig, lsbFields[i].name);
if (tmpDistroPos) {
sscanf(tmpDistroPos, lsbFields[i].scanstring, distroPart);
if (distroPart[0] == '"') {
char *tmpMakeNull = NULL;
tmpDistroPos += strlen(lsbFields[i].name) + 1;
tmpMakeNull = strchr(tmpDistroPos + 1 , '"');
if (tmpMakeNull) {
*tmpMakeNull = '\0';
Str_Strcat(distro, tmpDistroPos, distroSize);
*tmpMakeNull = '"' ;
}
} else {
Str_Strcat(distro, distroPart, distroSize);
}
Str_Strcat(distro, " ", distroSize);
}
}
if (distro[0] == '\0') {
/* Copy original string. What we got wasn't LSB compliant. */
Str_Strcpy(distro, distroOrig, distroSize);
}
ret = TRUE;
out:
close(fd);
free(distroOrig);
return ret;
}
/*
*----------------------------------------------------------------------
*
* HostinfoGetCmdOutput --
*
* Run a cmd & get its cmd line output
*
* Results:
* An allocated string or NULL if an error occurred.
*
* Side effects:
* The cmd is run.
*
*----------------------------------------------------------------------
*/
static char *
HostinfoGetCmdOutput(const char *cmd) // IN:
{
#if defined(N_PLAT_NLM)
Warning("Trying to execute command \"%s\" and catch its output... No way on NetWare...\n", cmd);
return NULL;
#else
DynBuf db;
FILE *stream;
char *out = NULL;
DynBuf_Init(&db);
stream = Posix_Popen(cmd, "r");
if (stream == NULL) {
Warning("Unable to get output of command \"%s\"\n", cmd);
return NULL;
}
for (;;) {
char *line = NULL;
size_t size;
switch (StdIO_ReadNextLine(stream, &line, 0, &size)) {
case StdIO_Error:
goto closeIt;
break;
case StdIO_EOF:
break;
case StdIO_Success:
break;
default:
ASSERT_NOT_IMPLEMENTED(FALSE);
}
if (line == NULL) {
break;
}
/* size does -not- include the NUL terminator. */
DynBuf_Append(&db, line, size + 1);
free(line);
}
if (DynBuf_Get(&db)) {
out = (char *) DynBuf_AllocGet(&db);
}
closeIt:
DynBuf_Destroy(&db);
pclose(stream);
return out;
#endif
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_GetOSName --
*
* Return OS version information. First retrieve OS information using
* uname, then look in /etc/xxx-release file to get the distro info.
* osFullName will be:
* <OS NAME> <OS RELEASE> <SPECIFIC_DISTRO_INFO>
* An example of such string would be:
* Linux 2.4.18-3 Red Hat Linux release 7.3 (Valhalla)
*
* osName contains an os name in the same format that is used
* in .vmx file.
*
* Return value:
* Returns TRUE on success and FALSE on failure.
* Returns the guest's full OS name (osNameFull)
* Returns the guest's OS name in the same format as .vmx file (osName)
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
Bool
Hostinfo_GetOSName(uint32 outBufFullLen, // IN: length of osNameFull buffer
uint32 outBufLen, // IN: length of osName buffer
char *osNameFull, // OUT: Full OS name
char *osName) // OUT: OS name (.vmx file format)
{
struct utsname buf;
unsigned int lastCharPos;
const char *lsbCmd = "lsb_release -sd 2>/dev/null";
char *lsbOutput = NULL;
/*
* Use uname to get complete OS information.
*/
if (uname(&buf) < 0) {
Warning("%s: uname failed %d\n", __FUNCTION__, errno);
return FALSE;
}
if (strlen(buf.sysname) + strlen(buf.release) + 3 > outBufFullLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Strcpy(osName, STR_OS_EMPTY, outBufLen);
Str_Sprintf(osNameFull, outBufFullLen, "%s %s", buf.sysname, buf.release);
/*
* Check to see if this is Linux
* If yes, determine the distro by looking for /etc/xxx file.
*/
if (strstr(osNameFull, "Linux")) {
char distro[DISTRO_BUF_SIZE];
char distroShort[DISTRO_BUF_SIZE];
int i = 0;
int distroSize = sizeof distro;
/*
* Write default distro string depending on the kernel version. If
* later we find more detailed information this will get overwritten.
*/
if (StrUtil_StartsWith(buf.release, "2.4.")) {
Str_Strcpy(distro, STR_OS_OTHER_24_FULL, distroSize);
Str_Strcpy(distroShort, STR_OS_OTHER_24, distroSize);
} else if (StrUtil_StartsWith(buf.release, "2.6.")) {
Str_Strcpy(distro, STR_OS_OTHER_26_FULL, distroSize);
Str_Strcpy(distroShort, STR_OS_OTHER_26, distroSize);
} else {
Str_Strcpy(distro, STR_OS_OTHER_FULL, distroSize);
Str_Strcpy(distroShort, STR_OS_OTHER, distroSize);
}
/*
* Try to get OS detailed information from the lsb_release command.
*/
lsbOutput = HostinfoGetCmdOutput(lsbCmd);
if (!lsbOutput) {
/*
* Try to get more detailed information from the version file.
*/
for (i = 0; distroArray[i].filename != NULL; i++) {
if (HostinfoReadDistroFile(distroArray[i].filename, distroSize,
distro)) {
break;
}
}
/*
* If we failed to read every distro file, exit now, before calling
* strlen on the distro buffer (which wasn't set).
*/
if (distroArray[i].filename == NULL) {
Warning("%s: Error: no distro file found\n", __FUNCTION__);
return FALSE;
}
} else {
char *lsbStart = lsbOutput;
char *quoteEnd = NULL;
if (lsbStart[0] == '"') {
lsbStart++;
quoteEnd = strchr(lsbStart, '"');
if (quoteEnd) {
*quoteEnd = '\0';
}
}
Str_Strcpy(distro, lsbStart, distroSize);
free(lsbOutput);
}
HostinfoGetOSShortName(distro, distroShort, distroSize);
if (strlen(distro) + strlen(osNameFull) + 2 > outBufFullLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Strcat(osNameFull, " ", outBufFullLen);
Str_Strcat(osNameFull, distro, outBufFullLen);
if (strlen(distroShort) + 1 > outBufLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Strcpy(osName, distroShort, outBufLen);
} else if (strstr(osNameFull, "FreeBSD")) {
size_t nameLen = sizeof STR_OS_FREEBSD - 1;
size_t releaseLen = 0;
char *dashPtr;
/*
* FreeBSD releases report their version as "x.y-RELEASE". We'll be naive
* look for the first dash, and use everything before it as the version
* number.
*/
dashPtr = Str_Strchr(buf.release, '-');
if (dashPtr != NULL) {
releaseLen = dashPtr - buf.release;
}
if (nameLen + releaseLen + 1 > outBufLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Strcpy(osName, STR_OS_FREEBSD, outBufLen);
} else if (strstr(osNameFull, "SunOS")) {
size_t nameLen = sizeof STR_OS_SOLARIS - 1;
size_t releaseLen = 0;
char solarisRelease[3] = "";
/*
* Solaris releases report their version as "x.y". For our supported
* releases it seems that x is always "5", and is ignored in favor of
* y for the version number.
*/
if (sscanf(buf.release, "5.%2[0-9]", solarisRelease) == 1) {
releaseLen = strlen(solarisRelease);
}
if (nameLen + releaseLen + 1 > outBufLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Snprintf(osName, outBufLen, "%s%s", STR_OS_SOLARIS, solarisRelease);
}
if (Hostinfo_GetSystemBitness() == 64) {
if (strlen(osName) + sizeof STR_OS_64BIT_SUFFIX > outBufLen) {
Warning("%s: Error: buffer too small\n", __FUNCTION__);
return FALSE;
}
Str_Strcat(osName, STR_OS_64BIT_SUFFIX, outBufLen);
}
/*
* Before returning, truncate the \n character at the end of the full name.
*/
lastCharPos = strlen(osNameFull) - 1;
if (osNameFull[lastCharPos] == '\n') {
osNameFull[lastCharPos] = '\0';
}
return TRUE;
}
#if defined(VMX86_SERVER)
/*
*----------------------------------------------------------------------
*
* HostinfoReadProc --
*
* Depending on what string is passed to it, this function parses the
* /proc/vmware/sched/ncpus node and returns the requested value.
*
* Results:
* A postive value on success, -1 (0xFFFFFFFF) on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static uint32
HostinfoReadProc(const char *str) // IN:
{
/* XXX this should use sysinfo!! (bug 59849)
*/
FILE *f;
char *line;
uint32 count;
ASSERT(!strcmp("logical", str) || !strcmp("cores", str) ||
!strcmp("packages", str));
ASSERT(!HostType_OSIsVMK()); // Don't use /proc/vmware
f = Posix_Fopen("/proc/vmware/sched/ncpus", "r");
if (f != NULL) {
while (StdIO_ReadNextLine(f, &line, 0, NULL) == StdIO_Success) {
if (strstr(line, str)) {
if (sscanf(line, "%d ", &count) == 1) {
free(line);
break;
}
}
free(line);
}
fclose(f);
if (count > 0) {
return count;
}
}
return -1;
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_HTDisabled --
*
* Figure out if hyperthreading is enabled
*
* Results:
* TRUE if hyperthreading is disabled, FALSE otherwise
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Bool
Hostinfo_HTDisabled(void)
{
static uint32 logical = 0, cores = 0;
if (HostType_OSIsVMK()) {
VMK_ReturnStatus status = VMKernel_HTEnabledCPU();
if (status != VMK_OK) {
return TRUE;
} else {
return FALSE;
}
}
if (logical == 0 && cores == 0) {
logical = HostinfoReadProc("logical");
cores = HostinfoReadProc("cores");
if (logical <= 0 || cores <= 0) {
logical = cores = 0;
}
}
return logical == cores;
}
#endif /*ifdef VMX86_SERVER*/
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_NumCPUs --
*
* Get the number of logical CPUs on the host. If the CPUs are
* hyperthread-capable, this number may be larger than the number of
* physical CPUs. For example, if the host has four hyperthreaded
* physical CPUs with 2 logical CPUs apiece, this function returns 8.
*
* This function returns the number of CPUs that the host presents to
* applications, which is what we want in the vast majority of cases. We
* would only ever care about the number of physical CPUs for licensing
* purposes.
*
* Results:
* On success, the number of CPUs (> 0) the host tells us we have.
* On failure, 0xFFFFFFFF (-1).
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
uint32
Hostinfo_NumCPUs(void)
{
#if defined(__APPLE__)
uint32 out;
size_t outSize = sizeof out;
/*
* Quoting sys/sysctl.h:
* "
* These are the support HW selectors for sysctlbyname. Parameters that
* are byte counts or frequencies are 64 bit numbers. All other parameters
* are 32 bit numbers.
* ...
* hw.activecpu - The number of processors currently available for executing
* threads. Use this number to determine the number threads
* to create in SMP aware applications. This number can
* change when power management modes are changed.
* "
*
* Apparently the only way to retrieve this info is by name, and I have
* verified the info changes when you dynamically switch a CPU
* offline/online. --hpreg
*/
if (sysctlbyname("hw.activecpu", &out, &outSize, NULL, 0) == -1) {
return -1;
}
return out;
#elif defined(__FreeBSD__)
uint32 out;
size_t outSize = sizeof out;
#if __FreeBSD__version >= 500019
if (sysctlbyname("kern.smp.cpus", &out, &outSize, NULL, 0) == -1) {
return -1;
}
#else
if (sysctlbyname("machdep.smp_cpus", &out, &outSize, NULL, 0) == -1) {
if (errno == ENOENT) {
out = 1;
} else {
return -1;
}
}
#endif
return out;
#else
static int count = 0;
if (count <= 0) {
#if defined(VMX86_SERVER)
if (HostType_OSIsVMK()) {
VMK_ReturnStatus status = VMKernel_GetNumCPUsUsed(&count);
if (status != VMK_OK) {
count = 0;
return -1;
}
} else {
count = HostinfoReadProc("logical");
if (count <= 0) {
count = 0;
return -1;
}
}
#else /* ifdef VMX86_SERVER */
FILE *f;
char *line;
f = Posix_Fopen("/proc/cpuinfo", "r");
if (f == NULL) {
return -1;
}
while (StdIO_ReadNextLine(f, &line, 0, NULL) == StdIO_Success) {
if (strncmp(line, "processor", strlen("processor")) == 0) {
count++;
}
free(line);
}
fclose(f);
if (count == 0) {
return -1;
}
#endif /* ifdef VMX86_SERVER */
}
return count;
#endif
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_OSIsSMP --
*
* Host OS SMP capability.
*
* Results:
* TRUE is host OS is SMP capable.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Bool
Hostinfo_OSIsSMP(void)
{
uint32 ncpu;
#if defined(__APPLE__)
size_t ncpuSize = sizeof ncpu;
if (sysctlbyname("hw.ncpu", &ncpu, &ncpuSize, NULL, 0) == -1) {
return FALSE;
}
#else
ncpu = Hostinfo_NumCPUs();
if (ncpu == 0xFFFFFFFF) {
return FALSE;
}
#endif
return ncpu > 1 ? TRUE : FALSE;
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_NameGet --
*
* Return the fully qualified host name of the host.
* Thread-safe. --hpreg
*
* Results:
* The (memorized) name on success
* NULL on failure
*
* Side effects:
* A host name resolution can occur.
*
*-----------------------------------------------------------------------------
*/
Unicode
Hostinfo_NameGet(void)
{
Unicode result;
static Atomic_Ptr state; /* Implicitly initialized to NULL. --hpreg */
result = Atomic_ReadPtr(&state);
if (UNLIKELY(result == NULL)) {
Unicode before;
result = Hostinfo_HostName();
before = Atomic_ReadIfEqualWritePtr(&state, NULL, result);
if (before) {
Unicode_Free(result);
result = before;
}
}
return result;
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_GetUser --
*
* Return current user name, or NULL if can't tell.
* XXX Not thread-safe (somebody could do a setenv()). --hpreg
*
* Results:
* User name. Must be free()d by caller.
*
* Side effects:
* No.
*
*-----------------------------------------------------------------------------
*/
Unicode
Hostinfo_GetUser(void)
{
char buffer[BUFSIZ];
struct passwd pw;
struct passwd *ppw = &pw;
Unicode env = NULL;
Unicode name = NULL;
if ((Posix_Getpwuid_r(getuid(), &pw, buffer, sizeof buffer, &ppw) == 0) &&
(ppw != NULL)) {
if (ppw->pw_name) {
name = Unicode_Duplicate(ppw->pw_name);
}
}
if (!name) {
env = Posix_Getenv("USER");
if (env) {
name = Unicode_Duplicate(env);
}
}
return name;
}
/*
*-----------------------------------------------------------------------------
*
* HostinfoGetLoadAverage --
*
* Returns system average load.
*
* Results:
* TRUE on success, FALSE otherwise.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static Bool
HostinfoGetLoadAverage(float *avg0, // IN/OUT:
float *avg1, // IN/OUT:
float *avg2) // IN/OUT:
{
/* getloadavg(3) was introduced with glibc 2.2 */
#if defined(GLIBC_VERSION_22) || defined(__APPLE__)
double avg[3];
int res;
res = getloadavg(avg, 3);
if (res < 3) {
NOT_TESTED_ONCE();
return FALSE;
}
if (avg0) {
*avg0 = (float) avg[0];
}
if (avg1) {
*avg1 = (float) avg[1];
}
if (avg2) {
*avg2 = (float) avg[2];
}
return TRUE;
#else
/*
* Not implemented. This function is currently only used in the vmx, so
* getloadavg is always available to us. If the linux tools ever need this,
* we can go back to having a look at the output of /proc/loadavg, but
* let's not do that now as long as it's not necessary.
*/
NOT_IMPLEMENTED();
return FALSE;
#endif
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_GetLoadAverage --
*
* Returns system average load * 100.
*
* Results:
* TRUE/FALSE
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
Bool
Hostinfo_GetLoadAverage(uint32 *avg) // IN/OUT:
{
float avg0 = 0;
if (!HostinfoGetLoadAverage(&avg0, NULL, NULL)) {
return FALSE;
}
*avg = (uint32) 100 * avg0;
return TRUE;
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_LogLoadAverage --
*
* Logs system average load.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
void
Hostinfo_LogLoadAverage(void)
{
float avg0 = 0, avg1 = 0, avg2 = 0;
if (HostinfoGetLoadAverage(&avg0, &avg1, &avg2)) {
Log("LOADAVG: %.2f %.2f %.2f\n", avg0, avg1, avg2);
}
}
#if defined(__APPLE__)
/*
*-----------------------------------------------------------------------------
*
* HostinfoMacAbsTimeNS --
*
* Return the Mac OS absolute time.
*
* Results:
* The absolute time in nanoseconds is returned. This time is documented
* to NEVER go backwards.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static inline VmTimeType
HostinfoMacAbsTimeNS(void)
{
VmTimeType raw;
mach_timebase_info_data_t *ptr;
static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
/* Insure that the time base values are correct. */
ptr = (mach_timebase_info_data_t *) Atomic_ReadPtr(&atomic);
if (ptr == NULL) {
char *p;
p = Util_SafeMalloc(sizeof(mach_timebase_info_data_t));
mach_timebase_info((mach_timebase_info_data_t *) p);
if (Atomic_ReadIfEqualWritePtr(&atomic, NULL, p)) {
free(p);
}
ptr = (mach_timebase_info_data_t *) Atomic_ReadPtr(&atomic);
}
raw = mach_absolute_time();
if ((ptr->numer == 1) && (ptr->denom == 1)) {
/* The scaling values are unity, save some time/arithmetic */
return raw;
} else {
/* The scaling values are not unity. Prevent overflow when scaling */
return ((double) raw) * (((double) ptr->numer) / ((double) ptr->denom));
}
}
#endif
/*
*-----------------------------------------------------------------------------
*
* HostinfoRawSystemTimerUS --
*
* Obtain the raw system timer value.
*
* Results:
* Relative time in microseconds or zero if a failure.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
VmTimeType
Hostinfo_RawSystemTimerUS(void)
{
#if defined(__APPLE__)
return HostinfoMacAbsTimeNS() / 1000ULL;
#else
#if defined(VMX86_SERVER)
if (HostType_OSIsPureVMK()) {
uint64 uptime;
VMK_ReturnStatus status;
status = VMKernel_GetUptimeUS(&uptime);
if (status != VMK_OK) {
Log("%s: failure!\n", __FUNCTION__);
return 0; // A timer read failure - this is really bad!
}
return uptime;
} else {
#endif /* ifdef VMX86_SERVER */
struct timeval tval;
/* Read the time from the operating system */
if (gettimeofday(&tval, NULL) != 0) {
Log("%s: failure!\n", __FUNCTION__);
return 0; // A timer read failure - this is really bad!
}
/* Convert into microseconds */
return (((VmTimeType)tval.tv_sec) * 1000000 + tval.tv_usec);
#if defined(VMX86_SERVER)
}
#endif /* ifdef VMX86_SERVER */
#endif /* ifdef __APPLE__ */
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_LogMemUsage --
* Log system memory usage.
*
* Results:
* System memory usage is logged.
*
* Side effects:
* No.
*
*-----------------------------------------------------------------------------
*/
void
Hostinfo_LogMemUsage(void)
{
int fd = Posix_Open("/proc/self/statm", O_RDONLY);
if (fd != -1) {
size_t len;
char buf[64];
len = read(fd, buf, sizeof buf);
close(fd);
if (len != -1) {
int a[7] = { 0 };
buf[len < sizeof buf ? len : sizeof buf - 1] = '\0';
sscanf(buf, "%d %d %d %d %d %d %d",
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]);
Log("RUSAGE size=%d resident=%d share=%d trs=%d lrs=%d drs=%d dt=%d\n",
a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
}
}
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_TouchBackDoor --
*
* Access the backdoor. This is used to determine if we are
* running in a VM or on a physical host. On a physical host
* this should generate a GP which we catch and thereby determine
* that we are not in a VM. However some OSes do not handle the
* GP correctly and the process continues running returning garbage.
* In this case we check the EBX register which should be
* BDOOR_MAGIC if the IN was handled in a VM. Based on this we
* return either TRUE or FALSE.
*
* Results:
* TRUE if we succesfully accessed the backdoor, FALSE or segfault
* if not.
*
* Side effects:
* Exception if not in a VM.
*
*----------------------------------------------------------------------
*/
Bool
Hostinfo_TouchBackDoor(void)
{
/*
* XXX: This can cause Apple's Crash Reporter to erroneously display
* a crash, even though the process has caught the SIGILL and handled
* it.
*
* It's also annoying in gdb, so we'll turn it off in devel builds.
*/
#if !defined(__APPLE__) && !defined(VMX86_DEVEL) && (defined(__i386__) || defined(__x86_64__))
uint32 eax;
uint32 ebx;
uint32 ecx;
__asm__ __volatile__(
# if defined __PIC__ && !vm_x86_64 // %ebx is reserved by the compiler.
"xchgl %%ebx, %1" "\n\t"
"inl %%dx, %%eax" "\n\t"
"xchgl %%ebx, %1"
: "=a" (eax),
"=&rm" (ebx),
# else
"inl %%dx, %%eax"
: "=a" (eax),
"=b" (ebx),
# endif
"=c" (ecx)
: "0" (BDOOR_MAGIC),
"1" (~BDOOR_MAGIC),
"2" (BDOOR_CMD_GETVERSION),
"d" (BDOOR_PORT)
);
if (ebx == BDOOR_MAGIC) {
return TRUE;
}
#endif
return FALSE;
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_ResetProcessState --
*
* Clean up signal handlers and file descriptors before an exec().
* Fds which need to be kept open can be passed as an array.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Hostinfo_ResetProcessState(const int *keepFds, // IN:
size_t numKeepFds) // IN:
{
int s, fd;
struct sigaction sa;
struct rlimit rlim;
#ifdef __linux__
int err;
uid_t euid;
#endif
/*
* Disable itimers before resetting the signal handlers.
* Otherwise, the process may still receive timer signals:
* SIGALRM, SIGVTARLM, or SIGPROF.
*/
struct itimerval it;
it.it_value.tv_sec = it.it_value.tv_usec = 0;
it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &it, NULL);
setitimer(ITIMER_VIRTUAL, &it, NULL);
setitimer(ITIMER_PROF, &it, NULL);
for (s = 1; s <= NSIG; s++) {
sa.sa_handler = SIG_DFL;
sigfillset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(s, &sa, NULL);
}
for (fd = (int) sysconf(_SC_OPEN_MAX) - 1; fd > STDERR_FILENO; fd--) {
size_t i;
for (i = 0; i < numKeepFds; i++) {
if (fd == keepFds[i]) {
break;
}
}
if (i == numKeepFds) {
(void) close(fd);
}
}
if (getrlimit(RLIMIT_AS, &rlim) == 0) {
rlim.rlim_cur = rlim.rlim_max;
setrlimit(RLIMIT_AS, &rlim);
}
#ifdef __linux__
/*
* Drop iopl to its default value.
*/
euid = Id_GetEUid();
/* At this point, _unless we are running as root_, we shouldn't have root
privileges --hpreg */
ASSERT(euid != 0 || getuid() == 0);
Id_SetEUid(0);
err = iopl(0);
Id_SetEUid(euid);
ASSERT_NOT_IMPLEMENTED(err == 0);
#endif
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_Daemonize --
*
* Cross-platform daemon(3)-like wrapper.
*
* Restarts the current process as a daemon, given the path to the
* process (usually from Hostinfo_GetModulePath). This means:
*
* * You're detached from your parent. (Your parent doesn't
* need to wait for you to exit.)
* * Your process no longer has a controlling terminal or
* process group.
* * Your stdin/stdout/stderr fds are redirected to /dev/null. All
* other descriptors, except for the ones that are passed in the
* parameter keepFds, are closed.
* * Your signal handlers are reset to SIG_DFL in the daemonized
* process, and all the signals are unblocked.
* * Your main() function is called with the specified NULL-terminated
* argument list.
*
* (Don't forget that the first string in args is argv[0] -- the
* name of the process).
*
* Unless 'flags' contains HOSTINFO_DAEMONIZE_NOCHDIR, then the
* current directory of the daemon process is set to "/".
*
* Unless 'flags' contains HOSTINFO_DAEMONIZE_NOCLOSE, then all stdio
* file descriptors of the daemon process are redirected to /dev/null.
* This is true even if the stdio descriptors are included in keepFds,
* i.e. the list of fds to be kept open.
*
* If 'flags' contains HOSTINFO_DAEMONIZE_EXIT, then upon successful
* launch of the daemon, the original process will exit.
*
* If pidPath is non-NULL, then upon success, writes the PID
* (as a US-ASCII string followed by a newline) of the daemon
* process to that path.
*
* Results:
* FALSE if the process could not be daemonized. Err_Errno() contains
* the error on failure.
* TRUE if 'flags' does not contain HOSTINFO_DAEMONIZE_EXIT and
* the process was daemonized.
* Otherwise, if the process was daemonized, this function does
* not return, and flow continues from your own main() function.
*
* Side effects:
* The current process is restarted with the given arguments.
* The process state is reset (see Hostinfo_ResetProcessState).
* A new session is created (so the process has no controlling terminal).
*
*-----------------------------------------------------------------------------
*/
Bool
Hostinfo_Daemonize(const char *path, // IN: NUL-terminated UTF-8
// path to exec
char * const *args, // IN: NULL-terminated UTF-8
// argv list
HostinfoDaemonizeFlags flags, // IN: flags
const char *pidPath, // IN/OPT: NUL-terminated
// UTF-8 path to write PID
const int *keepFds, // IN/OPT: array of fds to be
// kept open
size_t numKeepFds) // IN: number of fds in
// keepFds
{
/*
* We use the double-fork method to make a background process whose
* parent is init instead of the original process.
*
* We do this instead of calling daemon(), because daemon() is
* deprecated on Mac OS 10.5 hosts, and calling it causes a compiler
* warning.
*
* We must exec() after forking, because Mac OS library frameworks
* depend on internal Mach ports, which are not correctly propagated
* across fork calls. exec'ing reinitializes the frameworks, which
* causes them to reopen their Mach ports.
*/
int childPid;
int pipeFds[2] = { -1, -1 };
uint32 err = EINVAL;
char *pathLocalEncoding = NULL;
char *pidPathLocalEncoding = NULL;
char **argsLocalEncoding = NULL;
int *tempFds = NULL;
sigset_t sig;
ASSERT_ON_COMPILE(sizeof (errno) <= sizeof err);
ASSERT(args);
ASSERT(path);
ASSERT(numKeepFds == 0 || keepFds);
if (pipe(pipeFds) == -1) {
err = Err_Errno();
Warning("%s: Couldn't create pipe, error %u.\n", __FUNCTION__, err);
pipeFds[0] = pipeFds[1] = -1;
goto cleanup;
}
numKeepFds++;
tempFds = malloc(sizeof tempFds[0] * numKeepFds);
if (!tempFds) {
err = Err_Errno();
Warning("%s: Couldn't allocate memory, error %u.\n", __FUNCTION__, err);
goto cleanup;
}
tempFds[0] = pipeFds[1];
if (keepFds) {
memcpy(tempFds + 1, keepFds, sizeof tempFds[0] * (numKeepFds - 1));
}
if (fcntl(pipeFds[1], F_SETFD, 1) == -1) {
err = Err_Errno();
Warning("%s: Couldn't set close-on-exec for fd %d, error %u.\n",
__FUNCTION__, pipeFds[1], err);
goto cleanup;
}
/* Convert the strings from UTF-8 before we fork. */
pathLocalEncoding = Unicode_GetAllocBytes(path, STRING_ENCODING_DEFAULT);
if (!pathLocalEncoding) {
Warning("%s: Couldn't convert path [%s] to default encoding.\n",
__FUNCTION__, path);
goto cleanup;
}
if (pidPath) {
pidPathLocalEncoding = Unicode_GetAllocBytes(pidPath,
STRING_ENCODING_DEFAULT);
if (!pidPathLocalEncoding) {
Warning("%s: Couldn't convert path [%s] to default encoding.\n",
__FUNCTION__, pidPath);
goto cleanup;
}
}
argsLocalEncoding = Unicode_GetAllocList(args, STRING_ENCODING_DEFAULT, -1);
if (!argsLocalEncoding) {
Warning("%s: Couldn't convert arguments to default encoding.\n",
__FUNCTION__);
goto cleanup;
}
childPid = fork();
switch (childPid) {
case -1:
err = Err_Errno();
Warning("%s: Couldn't fork first child, error %u.\n", __FUNCTION__, err);
goto cleanup;
case 0:
/* We're the first child. Continue on. */
break;
default:
{
/* We're the original process. Check if the first child exited. */
int status;
close(pipeFds[1]);
waitpid(childPid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) {
Warning("%s: Child %d exited with error %d.\n",
__FUNCTION__, childPid, WEXITSTATUS(status));
goto cleanup;
} else if (WIFSIGNALED(status)) {
Warning("%s: Child %d exited with signal %d.\n",
__FUNCTION__, childPid, WTERMSIG(status));
goto cleanup;
}
/*
* Check if the second child exec'ed successfully. If it had
* an error, it will write a uint32 errno to this pipe before
* exiting. Otherwise, its end of the pipe will be closed on
* exec and this call will fail as expected.
* The assumption is that we don't get a partial read. In case,
* it did happen, we can detect it by the number of bytes read.
*/
while (TRUE) {
int res = read(pipeFds[0], &err, sizeof err);
if (res > 0) {
Warning("%s: Child could not exec %s, read %d, error %u.\n",
__FUNCTION__, path, res, err);
goto cleanup;
} else if (res == -1 && errno == EINTR) {
continue;
}
break;
}
err = 0;
goto cleanup;
}
}
/*
* Close all fds except for the write end of the error pipe (which
* we've already set to close on successful exec), and the ones requested by
* the caller. Also reset the signal mask to unblock all signals. fork()
* clears pending signals.
*/
Hostinfo_ResetProcessState(tempFds, numKeepFds);
free(tempFds);
tempFds = NULL;
sigfillset(&sig);
sigprocmask(SIG_UNBLOCK, &sig, NULL);
if (!(flags & HOSTINFO_DAEMONIZE_NOCLOSE) && setsid() == -1) {
Warning("%s: Couldn't create new session, error %d.\n",
__FUNCTION__, Err_Errno());
_exit(EXIT_FAILURE);
}
switch (fork()) {
case -1:
{
Warning("%s: Couldn't fork second child, error %d.\n",
__FUNCTION__, Err_Errno());
_exit(EXIT_FAILURE);
}
case 0:
// We're the second child. Continue on.
break;
default:
/*
* We're the first child. We don't need to exist any more.
*
* Exiting here causes the second child to be reparented to the
* init process, so the original process doesn't need to wait
* for the child we forked off.
*/
_exit(EXIT_SUCCESS);
}
/*
* We can't use our i18n wrappers for file manipulation at this
* point, since we've forked; internal library mutexes might be
* invalid.
*/
if (!(flags & HOSTINFO_DAEMONIZE_NOCHDIR) && chdir("/") == -1) {
uint32 err = Err_Errno();
Warning("%s: Couldn't chdir to /, error %u.\n", __FUNCTION__, err);
/* Let the original process know we failed to chdir. */
if (write(pipeFds[1], &err, sizeof err) == -1) {
Warning("%s: Couldn't write to parent pipe: %u, original error: %u.\n",
__FUNCTION__, Err_Errno(), err);
}
_exit(EXIT_FAILURE);
}
if (!(flags & HOSTINFO_DAEMONIZE_NOCLOSE)) {
int fd;
fd = open(_PATH_DEVNULL, O_RDONLY);
if (fd != -1) {
dup2(fd, STDIN_FILENO);
close(fd);
}
fd = open(_PATH_DEVNULL, O_WRONLY);
if (fd != -1) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
}
if (pidPath) {
int64 pid;
char pidString[32];
int pidStringLen;
int pidPathFd;
ASSERT_ON_COMPILE(sizeof (pid_t) <= sizeof pid);
ASSERT(pidPathLocalEncoding);
/* See above comment about how we can't use our i18n wrappers here. */
pidPathFd = open(pidPathLocalEncoding, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (pidPathFd == -1) {
err = Err_Errno();
Warning("%s: Couldn't open PID path [%s], error %d.\n",
__FUNCTION__, pidPath, err);
if (write(pipeFds[1], &err, sizeof err) == -1) {
Warning("%s: Couldn't write to parent pipe: %u, original "
"error: %u.\n", __FUNCTION__, Err_Errno(), err);
}
_exit(EXIT_FAILURE);
}
pid = getpid();
pidStringLen = Str_Sprintf(pidString, sizeof pidString,
"%"FMT64"d\n", pid);
if (pidStringLen <= 0) {
err = EINVAL;
if (write(pipeFds[1], &err, sizeof err) == -1) {
Warning("%s: Couldn't write to parent pipe: %u, original "
"error: %u.\n", __FUNCTION__, Err_Errno(), err);
}
_exit(EXIT_FAILURE);
}
if (write(pidPathFd, pidString, pidStringLen) != pidStringLen) {
err = Err_Errno();
Warning("%s: Couldn't write PID to path [%s], error %d.\n",
__FUNCTION__, pidPath, err);
if (write(pipeFds[1], &err, sizeof err) == -1) {
Warning("%s: Couldn't write to parent pipe: %u, original "
"error: %u.\n", __FUNCTION__, Err_Errno(), err);
}
_exit(EXIT_FAILURE);
}
close(pidPathFd);
}
if (execv(pathLocalEncoding, argsLocalEncoding) == -1) {
err = Err_Errno();
Warning("%s: Couldn't exec %s, error %d.\n", __FUNCTION__, path, err);
/* Let the original process know we failed to exec. */
if (write(pipeFds[1], &err, sizeof err) == -1) {
Warning("Couldn't write to parent pipe: %u, original error: %u.\n",
Err_Errno(), err);
}
_exit(EXIT_FAILURE);
}
NOT_REACHED();
cleanup:
free(tempFds);
if (pipeFds[0] != -1) {
close(pipeFds[0]);
}
if (pipeFds[1] != -1) {
close(pipeFds[1]);
}
Util_FreeStringList(argsLocalEncoding, -1);
free(pidPathLocalEncoding);
free(pathLocalEncoding);
if (err == 0) {
if (flags & HOSTINFO_DAEMONIZE_EXIT) {
_exit(EXIT_SUCCESS);
}
} else {
Err_SetErrno(err);
if (pidPath) {
Posix_Unlink(pidPath);
}
}
return (err == 0);
}
#if !defined(__APPLE__) && !defined(__FreeBSD__)
/*
*----------------------------------------------------------------------
*
* HostinfoGetCpuInfo --
*
* Get some attribute from /proc/cpuinfo for a given CPU
*
* Results:
* On success: Allocated, NUL-terminated attribute string.
* On failure: NULL.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static char *
HostinfoGetCpuInfo(int nCpu, // IN:
char *name) // IN:
{
FILE *f;
char *line;
int cpu = 0;
char *value = NULL;
f = Posix_Fopen("/proc/cpuinfo", "r");
if (f == NULL) {
Warning(LGPFX" %s: Unable to open /proc/cpuinfo\n", __FUNCTION__);
return NULL;
}
while (cpu <= nCpu &&
StdIO_ReadNextLine(f, &line, 0, NULL) == StdIO_Success) {
char *s;
char *e;
if ((s = strstr(line, name)) &&
(s = strchr(s, ':'))) {
s++;
e = s + strlen(s);
/* Skip leading and trailing while spaces */
for (; s < e && isspace(*s); s++);
for (; s < e && isspace(e[-1]); e--);
*e = 0;
/* Free previous value */
free(value);
value = strdup(s);
ASSERT_MEM_ALLOC(value);
cpu++;
}
free(line);
}
fclose(f);
return value;
}
#endif
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_GetRatedCpuMhz --
*
* Get the rated CPU speed of a given processor.
* Return value is in MHz.
*
* Results:
* TRUE on success, FALSE on failure
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
Bool
Hostinfo_GetRatedCpuMhz(int32 cpuNumber, // IN:
uint32 *mHz) // OUT:
{
#if defined(__APPLE__) || defined(__FreeBSD__)
# if defined(__APPLE__)
# define CPUMHZ_SYSCTL_NAME "hw.cpufrequency_max"
# elif __FreeBSD__version >= 50011
# define CPUMHZ_SYSCTL_NAME "hw.clockrate"
# endif
# if defined(CPUMHZ_SYSCTL_NAME)
uint32 hz;
size_t hzSize = sizeof hz;
/* 'cpuNumber' is ignored: Intel Macs are always perfectly symetric. */
if (sysctlbyname(CPUMHZ_SYSCTL_NAME, &hz, &hzSize, NULL, 0) == -1) {
return FALSE;
}
*mHz = hz / 1000000;
return TRUE;
# else
return FALSE;
# endif
#else
float fMhz = 0;
char *readVal = HostinfoGetCpuInfo(cpuNumber, "cpu MHz");
if (readVal == NULL) {
return FALSE;
}
if (sscanf(readVal, "%f", &fMhz) == 1) {
*mHz = (unsigned int)(fMhz + 0.5);
}
free(readVal);
return TRUE;
#endif
}
/*
*-----------------------------------------------------------------------------
*
* Hostinfo_GetCpuDescription --
*
* Get the descriptive name associated with a given CPU.
*
* Results:
* On success: Allocated, NUL-terminated string.
* On failure: NULL.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
char *
Hostinfo_GetCpuDescription(uint32 cpuNumber) // IN:
{
#if defined(__APPLE__) || defined(__FreeBSD__)
# if defined(__APPLE__)
# define CPUDESC_SYSCTL_NAME "machdep.cpu.brand_string"
# else
# define CPUDESC_SYSCTL_NAME "hw.model"
# endif
char *desc;
size_t descSize;
/* 'cpuNumber' is ignored: Intel Macs are always perfectly symetric. */
if (sysctlbyname(CPUDESC_SYSCTL_NAME, NULL, &descSize, NULL, 0) == -1) {
return NULL;
}
desc = malloc(descSize);
if (!desc) {
return NULL;
}
if (sysctlbyname(CPUDESC_SYSCTL_NAME, desc, &descSize, NULL, 0) == -1) {
free(desc);
return NULL;
}
return desc;
#else
#ifdef VMX86_SERVER
if (HostType_OSIsVMK()) {
char mName[48];
/* VMKernel treats mName as an in/out parameter so terminate it. */
mName[0] = '\0';
if (VMKernel_GetCPUModelName(mName, cpuNumber,
sizeof(mName)) == VMK_OK) {
mName[sizeof(mName) - 1] = '\0';
return strdup(mName);
}
return NULL;
}
#endif
return HostinfoGetCpuInfo(cpuNumber, "model name");
#endif
}
/*
*----------------------------------------------------------------------
*
* Hostinfo_Execute --
*
* Start program COMMAND. If WAIT is TRUE, wait for program
* to complete and return exit status.
*
* Results:
* Exit status of COMMAND.
*
* Side effects:
* Run a separate program.
*
*----------------------------------------------------------------------
*/
int
Hostinfo_Execute(const char *command, // IN:
char * const *args, // IN:
Bool wait) // IN:
{
int pid;
int status;
if (command == NULL) {
return 1;
}
pid = fork();
if (pid == -1) {
return -1;
}
if (pid == 0) {
Hostinfo_ResetProcessState(NULL, 0);
Posix_Execvp(command, args);
exit(127);
}
if (wait) {
for (;;) {
if (waitpid(pid, &status, 0) == -1) {
if (errno == ECHILD) {
return 0; // This sucks. We really don't know.
}
if (errno != EINTR) {
return -1;
}
} else {
return status;
}
}
} else {
return 0;
}
}
|