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
|
/** Implementation for NSProcessInfo for GNUStep
Copyright (C) 1995-2017 Free Software Foundation, Inc.
Written by: Georg Tuparev <Tuparev@EMBL-Heidelberg.de>
Heidelberg, Germany
Modified by: Richard Frith-Macdonald <rfm@gnu.org>
This file is part of the GNUstep Base Library.
This library 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; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
<title>NSProcessInfo class reference</title>
$Date$ $Revision$
*/
/*************************************************************************
* File Name : NSProcessInfo.m
* Date : 06-aug-1995
*************************************************************************
* Notes :
* 1) The class functionality depends on the following UNIX functions and
* global variables: gethostname(), getpid(), and environ. For all system
* I had the opportunity to test them they are defined and have the same
* behavior. The same is true for the meaning of argv[0] (process name).
* 2) The global variable _gnu_sharedProcessInfoObject should NEVER be
* deallocate during the process runtime. Therefore I implemented a
* concrete NSProcessInfo subclass (_NSConcreteProcessInfo) with the only
* purpose to override the autorelease, retain, and release methods.
* To Do :
* 1) To test the class on more platforms;
* Bugs : Not known
* Last update: 07-aug-2002
* History : 06-aug-1995 - Birth and the first beta version (v. 0.5);
* 08-aug-1995 - V. 0.6 (tested on NS, SunOS, Solaris, OSF/1
* The use of the environ global var was changed to more
* conventional env[] (main function) so now the class could be
* used on SunOS and Solaris. [GT]
*************************************************************************
* Acknowledgments:
* - Adam Fedor, Andrew McCallum, and Paul Kunz for their help;
* - To the NEXTSTEP/GNUStep community
*************************************************************************/
#import "common.h"
#include <stdio.h>
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#endif
#if defined(HAVE_SYS_SIGNAL_H)
# include <sys/signal.h>
#elif defined(HAVE_SIGNAL_H)
# include <signal.h>
#endif
#if defined(HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#if defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#elif defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#ifdef HAVE_KVM_ENV
#include <kvm.h>
#if defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#elif defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#include <sys/param.h>
#endif /* HAVE_KVM_ENV */
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#if HAVE_PROCFS_H
#define id _procfs_avoid_id_collision
#include <procfs.h>
#undef id
#endif
#if defined(__APPLE__) && !GS_FAKE_MAIN
#include <crt_externs.h>
#endif
#import "Foundation/NSArray.h"
#import "Foundation/NSSet.h"
#import "Foundation/NSCharacterSet.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSException.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSHost.h"
#import "Foundation/NSLock.h"
#import "GNUstepBase/NSProcessInfo+GNUstepBase.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GSPrivate.h"
/* This error message should be called only if the private main function
* was not executed successfully. This may happen ONLY if another library
* or kit defines its own main function (as gnustep-base does).
*/
#if GS_FAKE_MAIN
#define _GNU_MISSING_MAIN_FUNCTION_CALL "\nGNUSTEP Internal Error:\n\
The private GNUstep function to establish the argv and environment\n\
variables was not called.\n\
Perhaps your program failed to #include <Foundation/NSObject.h> or\n\
<Foundation/Foundation.h>?\n\
If that is not the problem, Please report the error to bug-gnustep@gnu.org.\n\n"
#else
#ifdef GS_PASS_ARGUMENTS
#define _GNU_MISSING_MAIN_FUNCTION_CALL "\nGNUSTEP Error:\n\
A call to NSProcessInfo +initializeWithArguments:... must be made\n\
as the first ObjC statment in main. This function is used to \n\
establish the argv and environment variables.\n"
#else
#define _GNU_MISSING_MAIN_FUNCTION_CALL "\nGNUSTEP Internal Error:\n\
The private GNUstep function to establish the argv and environment\n\
variables was not called.\n\
\n\
Mismatched library versions between GNUstep Foundation (base) and AppKit\n\
(gui) is most often the cause of this message. Please be sure you\n\
are using known compatible versions and not a mismatched set. Generally,\n\
we recommend you use versions of base and gui which were released together.\n\
\n\
For more detailed assistance, please report the error to bug-gnustep@gnu.org.\n\n"
#endif
#endif
@interface NSHost (NSProcessInfo)
+ (NSString*) _myHostName;
@end
/*************************************************************************
*** _NSConcreteProcessInfo
*************************************************************************/
@interface _NSConcreteProcessInfo: NSProcessInfo
- (id) autorelease;
- (oneway void) release;
- (id) retain;
@end
@implementation _NSConcreteProcessInfo
- (id) autorelease
{
return self;
}
- (oneway void) release
{
return;
}
- (id) retain
{
return self;
}
@end
/*************************************************************************
*** NSProcessInfo implementation
*************************************************************************/
/**
* Instances of this class encapsulate information on the current process.
* For example, you can get the arguments, environment variables, host name,
* or process name. There is only one instance per process, for obvious
* reasons, and it may be obtained through the +processInfo method.
*/
@implementation NSProcessInfo
/*************************************************************************
*** Static global vars
*************************************************************************/
// The lock to protect shared process resources.
static NSRecursiveLock *procLock = nil;
// The shared NSProcessInfo instance
static NSProcessInfo *_gnu_sharedProcessInfoObject = nil;
// Host name of the CPU executing the process
static NSString *_gnu_hostName = nil;
static char *_gnu_arg_zero = 0;
// Current process name
static NSString *_gnu_processName = nil;
// Array of NSStrings (argv[1] .. argv[argc-1])
static NSArray *_gnu_arguments = nil;
// Dictionary of environment vars and their values
static NSDictionary *_gnu_environment = nil;
// The operating system we are using.
static unsigned int _operatingSystem = 0;
static NSString *_operatingSystemName = nil;
static NSString *_operatingSystemVersion = nil;
// Flag to indicate that fallbackInitialisation was executed.
static BOOL fallbackInitialisation = NO;
static NSMutableSet *mySet = nil;
#ifdef __ANDROID__
static jobject _androidContext = NULL;
static NSString *_androidFilesDir = nil;
static NSString *_androidCacheDir = nil;
/* The following macro assumes that the function's return type is bool.
*/
#define GS_JNI_CHECK(env, obj) \
if (unlikely(JNI_TRUE == (*env)->ExceptionCheck(env))) { \
fprintf(stderr, "File: %s, Line: %d jenv: %p obj: %p Pending exception in JNI environment.\n", __FILE__, __LINE__, env, obj); \
(*env)->ExceptionDescribe(env); \
(*env)->ExceptionClear(env); \
abort(); \
} \
if (unlikely(obj == NULL)) { \
fprintf(stderr, "File: %s, Line: %d jenv: %p JNI returned NULL instead of a valid JNI object.\n", __FILE__, __LINE__, env); \
abort(); \
}
#define GS_JNI_CLS_CHECK(env, cls, name) \
if (unlikely(cls == NULL)) { \
fprintf(stderr, "File: %s, Line: %d jenv: %p JNI returned NULL instead of a valid JNI class object for '%s'.\n", __FILE__, __LINE__, env, name); \
abort(); \
}
#define GS_JNI_METH_CHECK(env, meth) \
if (unlikely(meth == NULL)) { \
fprintf(stderr, "File: %s, Line: %d jenv: %p JNI returned NULL instead of a valid JNI method object.\n", __FILE__, __LINE__, env); \
abort(); \
}
#endif
/*************************************************************************
*** Implementing the gnustep_base_user_main function
*************************************************************************/
static void
_gnu_process_args(int argc, char *argv[], char *env[])
{
ENTER_POOL
NSString *arg0 = nil;
int i;
if (_gnu_arg_zero != 0)
{
free(_gnu_arg_zero);
}
if (argv != 0 && argv[0] != 0)
{
int len;
len = strlen(argv[0]) + 1;
_gnu_arg_zero = (char*)malloc(len);
memcpy(_gnu_arg_zero, argv[0], len);
arg0 = [[NSString alloc] initWithCString: _gnu_arg_zero];
}
else
{
#if defined(_WIN32)
unichar *buffer;
int buffer_size = 0;
int needed_size = 0;
int len;
const char *tmp;
while (needed_size == buffer_size)
{
buffer_size = buffer_size + 256;
buffer = (unichar*)malloc(buffer_size * sizeof(unichar));
needed_size = GetModuleFileNameW(NULL, buffer, buffer_size);
if (needed_size < buffer_size)
{
unsigned i;
for (i = 0; i < needed_size; i++)
{
if (buffer[i] == 0)
{
break;
}
}
arg0 = [[NSString alloc] initWithCharacters: buffer length: i];
}
else
{
free(buffer);
}
}
tmp = [arg0 cStringUsingEncoding: [NSString defaultCStringEncoding]];
len = strlen(tmp) + 1;
_gnu_arg_zero = (char*)malloc(len);
memcpy(_gnu_arg_zero, tmp, len);
#else
fprintf(stderr, "Error: for some reason, argv not properly set up "
"during GNUstep base initialization\n");
abort();
#endif
}
/* Getting the process name */
IF_NO_ARC([_gnu_processName release];)
_gnu_processName = [arg0 lastPathComponent];
#if defined(_WIN32)
/* On windows we remove any .exe extension for consistency with app names
* under unix
*/
{
NSString *e = [_gnu_processName pathExtension];
if (e != nil && [e caseInsensitiveCompare: @"EXE"] == NSOrderedSame)
{
_gnu_processName = [_gnu_processName stringByDeletingPathExtension];
}
}
#endif
IF_NO_ARC(RETAIN(_gnu_processName);)
/* Copy the argument list */
#if defined(_WIN32)
{
unichar **argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
NSString *str;
id obj_argv[argc];
int added = 1;
/* Copy the zero'th argument to the argument list */
obj_argv[0] = arg0;
if (mySet == nil) mySet = [NSMutableSet new];
for (i = 1; i < argc; i++)
{
str = [NSString stringWithCharacters: argvw[i] length: wcslen(argvw[i])];
if ([str hasPrefix: @"--GNU-Debug="])
{
[mySet addObject: [str substringFromIndex: 12]];
}
else
{
obj_argv[added++] = str;
}
}
IF_NO_ARC([_gnu_arguments release];)
_gnu_arguments = [[NSArray alloc] initWithObjects: obj_argv count: added];
}
#else
if (argv)
{
NSString *str;
id obj_argv[argc];
int added = 1;
NSStringEncoding enc = GSPrivateDefaultCStringEncoding();
/* Copy the zero'th argument to the argument list */
obj_argv[0] = arg0;
if (mySet == nil) mySet = [NSMutableSet new];
for (i = 1; i < argc; i++)
{
str = [NSString stringWithCString: argv[i] encoding: enc];
if ([str hasPrefix: @"--GNU-Debug="])
[mySet addObject: [str substringFromIndex: 12]];
else
obj_argv[added++] = str;
}
IF_NO_ARC([_gnu_arguments release];)
_gnu_arguments = [[NSArray alloc] initWithObjects: obj_argv count: added];
}
else
{
IF_NO_ARC([_gnu_arguments release];)
_gnu_arguments = [[NSArray alloc] init];
}
#endif
IF_NO_ARC([arg0 release];)
/* Copy the evironment list */
{
NSMutableArray *keys = [NSMutableArray new];
NSMutableArray *values = [NSMutableArray new];
NSStringEncoding enc = GSPrivateDefaultCStringEncoding();
#if defined(_WIN32)
if (fallbackInitialisation == NO)
{
unichar *base;
base = GetEnvironmentStringsW();
if (base != 0)
{
const unichar *wenvp = base;
while (*wenvp != 0)
{
const unichar *start = wenvp;
NSString *key;
NSString *val;
start = wenvp;
while (*wenvp != '=' && *wenvp != 0)
{
wenvp++;
}
if (*wenvp == '=')
{
key = [NSString stringWithCharacters: start
length: wenvp - start];
wenvp++;
start = wenvp;
}
else
{
break; // Bad format ... expected '='
}
while (*wenvp != 0)
{
wenvp++;
}
val = [NSString stringWithCharacters: start
length: wenvp - start];
wenvp++; // Skip past variable terminator
[keys addObject: key];
[values addObject: val];
}
FreeEnvironmentStringsW(base);
env = 0; // Suppress standard code.
}
}
#endif
if (env != 0)
{
i = 0;
while (env[i])
{
int len = strlen(env[i]);
char *cp = strchr(env[i], '=');
if (len && cp)
{
char buf[len+2];
memcpy(buf, env[i], len + 1);
cp = &buf[cp - env[i]];
*cp++ = '\0';
[keys addObject:
[NSString stringWithCString: buf encoding: enc]];
[values addObject:
[NSString stringWithCString: cp encoding: enc]];
}
i++;
}
}
IF_NO_ARC([_gnu_environment release];)
_gnu_environment = [[NSDictionary alloc] initWithObjects: values
forKeys: keys];
IF_NO_ARC([keys release];)
IF_NO_ARC([values release];)
}
LEAVE_POOL
}
#if !GS_FAKE_MAIN && ((defined(HAVE_PROCFS) || defined(HAVE_KVM_ENV) || defined(HAVE_PROCFS_PSINFO) || defined(__APPLE__)) && (defined(HAVE_LOAD_METHOD)))
/*
* We have to save program arguments and environment before main () is
* executed, because main () could modify their values before we get a
* chance to read them
*/
static int _gnu_noobjc_argc = 0;
static char **_gnu_noobjc_argv = NULL;
static char **_gnu_noobjc_env = NULL;
/*
* The +load method (an extension of the GNU compiler) is invoked
* before main and +initialize (for this class) is executed. This is
* guaranteed if +load contains only pure C code, as we have here. The
* code in here either uses libkvm if available, or else procfs.
*/
+ (void) load
{
#ifdef HAVE_KVM_ENV
/*
* Use the kvm library to open the kernel and read the environment and
* arguments. As we are not running as root we cannot open the memory
* device and thus we fake it using /dev/null. This is allowed under
* FreeBSD, but may fail on other operating systems which check the
* file type. The kvm calls used are those which are supposedly backward
* compatible with Solaris rather than being FreeBSD specific
*/
kvm_t *kptr = NULL;
struct kinfo_proc *proc_ptr = NULL;
int nprocs, i, count;
char **vectors;
/* open the kernel */
kptr = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "NSProcessInfo");
if (!kptr)
{
fprintf(stderr, "Error: Your system appears to provide libkvm, but the kernel open fails\n");
fprintf(stderr, "Try to reconfigure gnustep-base with --enable-fake-main. to work\n");
fprintf(stderr, "around this problem.");
abort();
}
/* find the process */
proc_ptr = kvm_getprocs(kptr, KERN_PROC_PID, getpid(), &nprocs);
if (!proc_ptr || (nprocs != 1))
{
fprintf(stderr, "Error: libkvm cannot find the current process\n");
abort();
}
/* get the environment vectors the normal way, since this always works.
On FreeBSD, the only other way is via /proc, and in later versions
/proc is not mounted. */
{
extern char **environ;
vectors = environ;
if (!vectors)
{
fprintf(stderr, "Error: for some reason, environ == NULL "
"during GNUstep base initialization\n"
"Please check the linking process\n");
abort();
}
}
/* copy the environment strings */
for (count = 0; vectors[count]; count++)
;
_gnu_noobjc_env = (char**)malloc(sizeof(char*) * (count + 1));
if (!_gnu_noobjc_env)
goto malloc_error;
for (i = 0; i < count; i++)
{
_gnu_noobjc_env[i] = (char *)strdup(vectors[i]);
if (!_gnu_noobjc_env[i])
goto malloc_error;
}
_gnu_noobjc_env[i] = NULL;
/* get the argument vectors */
vectors = kvm_getargv(kptr, proc_ptr, 0);
if (!vectors)
{
fprintf(stderr, "Error: libkvm does not return arguments for the current process\n");
fprintf(stderr, "this may be due to a bug (undocumented feature) in libkvm\n");
fprintf(stderr, "which fails to get arguments unless /proc is mounted.\n");
fprintf(stderr, "If so, you can mount the /proc filesystem or reconfigure/build\n");
fprintf(stderr, "gnustep-base with --enable-fake-main as a workaround, and\n");
fprintf(stderr, "should report the bug to the maintainer of libkvm on your operating system.\n");
abort();
}
/* copy the argument strings */
for (_gnu_noobjc_argc = 0; vectors[_gnu_noobjc_argc]; _gnu_noobjc_argc++)
;
_gnu_noobjc_argv
= (char**)malloc(sizeof(char*) * (_gnu_noobjc_argc + 1));
if (!_gnu_noobjc_argv)
goto malloc_error;
for (i = 0; i < _gnu_noobjc_argc; i++)
{
_gnu_noobjc_argv[i] = (char *)strdup(vectors[i]);
if (!_gnu_noobjc_argv[i])
goto malloc_error;
}
_gnu_noobjc_argv[i] = NULL;
return;
#elif defined(HAVE_PROCFS_PSINFO)
char *proc_file_name = NULL;
FILE *ifp;
psinfo_t pinfo;
char **vectors;
int i, count;
// Read commandline
proc_file_name = (char*)malloc(2048);
snprintf(proc_file_name, 2048, "/proc/%d/psinfo", (int)getpid());
ifp = fopen(proc_file_name, "r");
if (ifp == NULL)
{
fprintf(stderr, "Error: Failed to open the process info file:%s\n",
proc_file_name);
abort();
}
fread(&pinfo, sizeof(pinfo), 1, ifp);
fclose(ifp);
vectors = (char **)pinfo.pr_envp;
if (!vectors)
{
fprintf(stderr, "Error: for some reason, environ == NULL "
"during GNUstep base initialization\n"
"Please check the linking process\n");
abort();
}
/* copy the environment strings */
for (count = 0; vectors[count]; count++)
;
_gnu_noobjc_env = (char**)malloc(sizeof(char*) * (count + 1));
if (!_gnu_noobjc_env)
goto malloc_error;
for (i = 0; i < count; i++)
{
_gnu_noobjc_env[i] = (char *)strdup(vectors[i]);
if (!_gnu_noobjc_env[i])
goto malloc_error;
}
_gnu_noobjc_env[i] = NULL;
/* get the argument vectors */
vectors = (char **)pinfo.pr_argv;
if (!vectors)
{
fprintf(stderr, "Error: psinfo does not return arguments for the current process\n");
abort();
}
/* copy the argument strings */
for (_gnu_noobjc_argc = 0; vectors[_gnu_noobjc_argc]; _gnu_noobjc_argc++)
;
_gnu_noobjc_argv
= (char**)malloc(sizeof(char*) * (_gnu_noobjc_argc + 1));
if (!_gnu_noobjc_argv)
goto malloc_error;
for (i = 0; i < _gnu_noobjc_argc; i++)
{
_gnu_noobjc_argv[i] = (char *)strdup(vectors[i]);
if (!_gnu_noobjc_argv[i])
goto malloc_error;
}
_gnu_noobjc_argv[i] = NULL;
return;
#elif defined(__APPLE__)
/*
* Darwin/Mac OS X provides indirect access to command line arguments and
* the environment with functions defined in the C runtime system.
*/
int i, n;
int argc = *_NSGetArgc();
char **argv = *_NSGetArgv();
char **environ = *_NSGetEnviron();
/* copy environment */
n = 0;
while (environ[n] != NULL)
n++;
_gnu_noobjc_env = (char **)malloc(sizeof(char *) * (n + 1));
if (_gnu_noobjc_env == NULL)
goto malloc_error;
for (i = 0; i < n; i++)
{
_gnu_noobjc_env[i] = (char *)strdup(environ[i]);
if (_gnu_noobjc_env[i] == NULL)
goto malloc_error;
}
_gnu_noobjc_env[i] = NULL;
/* copy arguments */
_gnu_noobjc_argc = argc;
_gnu_noobjc_argv = (char **)malloc(sizeof(char *) * (argc + 1));
if (_gnu_noobjc_argv == NULL)
goto malloc_error;
for (i = 0; i < argc; i++)
{
_gnu_noobjc_argv[i] = (char *)strdup(argv[i]);
if (_gnu_noobjc_argv[i] == NULL)
goto malloc_error;
}
_gnu_noobjc_argv[i] = NULL;
return;
#else /* !HAVE_KVM_ENV (i.e. HAVE_PROCFS). */
/*
* Now we have the problem of reading program arguments and
* environment. We take the environment from extern char **environ, and
* the program arguments from the /proc filesystem.
*/
extern char **environ;
char *proc_file_name = NULL;
FILE *ifp;
int c;
int argument;
int length;
int position;
int env_terms;
BOOL stripTrailingNewline = NO;
#ifdef HAVE_PROGRAM_INVOCATION_NAME
extern char *program_invocation_name;
#endif /* HAVE_PROGRAM_INVOCATION_NAME */
// Read environment
/* NB: This should *never* happen if your compiler tools are
sane. But, if you are playing with them, you could break
them to the point you get here. :-) */
if (environ == NULL)
{
/* TODO: Try reading environment from /proc before aborting. */
fprintf(stderr, "Error: for some reason, environ == NULL "
"during GNUstep base initialization\n"
"Please check the linking process\n");
abort();
}
c = 0;
while (environ[c] != NULL)
c++;
env_terms = c;
_gnu_noobjc_env = (char**)malloc(sizeof(char*) * (env_terms + 1));
if (_gnu_noobjc_env == NULL)
goto malloc_error;
for (c = 0; c < env_terms; c++)
{
_gnu_noobjc_env[c] = (char *)strdup(environ[c]);
if (_gnu_noobjc_env[c] == NULL)
goto malloc_error;
}
_gnu_noobjc_env[c] = NULL;
// Read commandline
proc_file_name = (char *)malloc(2048);
snprintf(proc_file_name, 2048, "/proc/%d/cmdline", (int)getpid());
/*
* We read the /proc file thrice.
* First, to know how many arguments there are and allocate memory for them.
* Second, to know how long each argument is, and allocate memory accordingly.
* Third, to actually copy the arguments into memory.
*/
_gnu_noobjc_argc = 0;
#ifdef HAVE_STRERROR
errno = 0;
#endif /* HAVE_STRERROR */
ifp = fopen(proc_file_name, "r");
if (ifp == NULL)
goto proc_fs_error;
while (1)
{
c = getc(ifp);
if (c == 0)
_gnu_noobjc_argc++;
else if (c == EOF)
break;
}
#if (CMDLINE_TERMINATED == 0)
_gnu_noobjc_argc++;
#endif
fclose(ifp);
/*
* Now _gnu_noobcj_argc is the number of arguments;
* allocate memory accordingly.
*/
_gnu_noobjc_argv = (char **)malloc((sizeof(char *)) * (_gnu_noobjc_argc + 1));
if (_gnu_noobjc_argv == NULL)
goto malloc_error;
ifp = fopen(proc_file_name,"r");
//freopen(proc_file_name, "r", ifp);
if (ifp == NULL)
{
free(_gnu_noobjc_argv);
goto proc_fs_error;
}
argument = 0;
length = 0;
while (argument < _gnu_noobjc_argc)
{
c = getc(ifp);
length++;
if ((c == EOF) || (c == 0)) // End of a parameter
{
_gnu_noobjc_argv[argument]
= (char*)malloc((sizeof(char))*length);
if (_gnu_noobjc_argv[argument] == NULL)
goto malloc_error;
argument++;
length = 0;
if (c == EOF) // End of command line
{
_gnu_noobjc_argc = argument;
break;
}
}
}
fclose(ifp);
ifp = fopen(proc_file_name,"r");
//freopen(proc_file_name, "r", ifp);
if (ifp == NULL)
{
if (0 != _gnu_noobjc_argv)
{
for (c = 0; c < _gnu_noobjc_argc; c++)
{
free(_gnu_noobjc_argv[c]);
}
free(_gnu_noobjc_argv);
}
goto proc_fs_error;
}
argument = 0;
position = 0;
while (argument < _gnu_noobjc_argc)
{
c = getc(ifp);
if ((c == EOF) || (c == 0)) // End of a parameter
{
if (argument == 0 && position > 0
&& _gnu_noobjc_argv[argument][position-1] == '\n')
{
stripTrailingNewline = YES;
}
if (stripTrailingNewline == YES && position > 0
&& _gnu_noobjc_argv[argument][position-1] == '\n')
{
position--;
}
_gnu_noobjc_argv[argument][position] = '\0';
argument++;
if (c == EOF) // End of command line
break;
position = 0;
continue;
}
_gnu_noobjc_argv[argument][position] = c;
position++;
}
_gnu_noobjc_argv[argument] = NULL;
fclose(ifp);
free(proc_file_name);
return;
proc_fs_error:
#ifdef HAVE_STRERROR
/* Don't care about thread safety of strerror() here as this is only
* called in the initial thread and there shouldn't be any other
* threads at this point.
*/
fprintf(stderr, "Couldn't open file %s when starting gnustep-base; %s\n",
proc_file_name, strerror(errno));
#else /* !HAVE_FUNCTION_STRERROR */
fprintf(stderr, "Couldn't open file %s when starting gnustep-base.\n",
proc_file_name);
#endif /* HAVE_FUNCTION_STRERROR */
fprintf(stderr, "Your gnustep-base library is compiled for a kernel supporting the /proc filesystem, but it can't access it.\n");
fprintf(stderr, "You should recompile or change your kernel.\n");
free(proc_file_name);
#ifdef HAVE_PROGRAM_INVOCATION_NAME
fprintf(stderr, "We try to go on anyway; but the program will ignore any argument which were passed to it.\n");
_gnu_noobjc_argc = 1;
_gnu_noobjc_argv = malloc(sizeof(char *) * 2);
if (_gnu_noobjc_argv == NULL)
goto malloc_error;
_gnu_noobjc_argv[0] = strdup(program_invocation_name);
if (_gnu_noobjc_argv[0] == NULL)
goto malloc_error;
_gnu_noobjc_argv[1] = NULL;
return;
#else /* !HAVE_PROGRAM_INVOCATION_NAME */
/*
* There is really little sense in going on here, because NSBundle
* will anyway crash later if we just put something like "_Unknown_"
* as the program name.
*/
abort();
#endif /* HAVE_PROGRAM_INVOCATION_NAME */
#endif /* !HAVE_KVM_ENV (e.g. HAVE_PROCFS) */
malloc_error:
fprintf(stderr, "malloc() error when starting gnustep-base.\n");
fprintf(stderr, "Free some memory and then re-run the program.\n");
abort();
}
static void
_gnu_noobjc_free_vars(void)
{
char **p;
p = _gnu_noobjc_argv;
while (*p)
{
free(*p);
p++;
}
free(_gnu_noobjc_argv);
_gnu_noobjc_argv = 0;
p = _gnu_noobjc_env;
while (*p)
{
free(*p);
p++;
}
free(_gnu_noobjc_env);
_gnu_noobjc_env = 0;
}
+ (void) initialize
{
if (nil == procLock) procLock = [NSRecursiveLock new];
if (self == [NSProcessInfo class]
&& !_gnu_processName && !_gnu_arguments && !_gnu_environment)
{
if (_gnu_noobjc_argv == 0 || _gnu_noobjc_env == 0)
{
fprintf(stderr, _GNU_MISSING_MAIN_FUNCTION_CALL);
exit(1);
}
_gnu_process_args(_gnu_noobjc_argc, _gnu_noobjc_argv, _gnu_noobjc_env);
_gnu_noobjc_free_vars();
}
}
#else /*! HAVE_PROCFS !HAVE_LOAD_METHOD !HAVE_KVM_ENV */
#ifdef _WIN32
/* For WindowsAPI Library, we know the global variables (argc, etc) */
+ (void) initialize
{
if (nil == procLock) procLock = [NSRecursiveLock new];
if (self == [NSProcessInfo class]
&& !_gnu_processName && !_gnu_arguments && !_gnu_environment)
{
_gnu_process_args(__argc, __argv, _environ);
}
}
#elif defined(__BEOS__)
extern int __libc_argc;
extern char **__libc_argv;
+ (void) initialize
{
if (nil == procLock) procLock = [NSRecursiveLock new];
if (self == [NSProcessInfo class]
&& !_gnu_processName && !_gnu_arguments && !_gnu_environment)
{
_gnu_process_args(__libc_argc, __libc_argv, environ);
}
}
#else
+ (void) initialize
{
if (nil == procLock) procLock = [NSRecursiveLock new];
}
#ifndef GS_PASS_ARGUMENTS
#undef main
/* The gnustep_base_user_main function is declared 'weak' so that the linker
* should actually use the one compiled as the program's 'main' function.
* The internal version gets called only if the program does not implement
* the function (ie the prgram was compiled with the wrong version of
* GSConfig.h included/imported). The other possible reason for the internal
* function to be called would be a compiler/linker issue (eg 'weak' not
* supported).
*/
int gnustep_base_user_main () __attribute__((weak));
int gnustep_base_user_main (int argc, char *argv[], char *env[])
{
fprintf(stderr, "\nGNUSTEP Internal Error:\n"
"The GNUstep function to establish the argv and environment variables could\n"
"not find the main function of your program.\n"
"Perhaps your program failed to #include <Foundation/NSObject.h> or\n"
"<Foundation/Foundation.h> (or included/imported a different version of the\n"
"header from the one supplied with this copy of the gnustep-base library)?\n"
"If that is not the case, Please report the error to bug-gnustep@gnu.org.\n");
exit(1);
}
int main(int argc, char *argv[], char *env[])
{
#ifdef NeXT_RUNTIME
/* This memcpy has to be done before the first message is sent to any
constant string object. See Apple Radar 2870817 */
memcpy(&_NSConstantStringClassReference,
objc_getClass(STRINGIFY(NXConstantString)),
sizeof(_NSConstantStringClassReference));
#endif
_gnu_process_args(argc, argv, env);
/* Call the user defined main function */
return gnustep_base_user_main(argc, argv, env);
}
#endif /* !GS_PASS_ARGUMENTS */
#endif /* _WIN32 */
#endif /* HAS_LOAD_METHOD && HAS_PROCFS */
+ (NSProcessInfo *) processInfo
{
// Check if the main() function was successfully called
// We can't use NSAssert, which calls NSLog, which calls NSProcessInfo...
if (!(_gnu_processName && _gnu_arguments && _gnu_environment))
{
fprintf(stderr, _GNU_MISSING_MAIN_FUNCTION_CALL);
exit(1);
}
if (!_gnu_sharedProcessInfoObject)
{
_gnu_sharedProcessInfoObject = [[_NSConcreteProcessInfo alloc] init];
[procLock lock];
if (mySet != nil)
{
NSEnumerator *e = [mySet objectEnumerator];
NSMutableSet *s = [_gnu_sharedProcessInfoObject debugSet];
id o;
while ((o = [e nextObject]) != nil)
{
[s addObject: o];
}
[mySet release];
mySet = nil;
}
[procLock unlock];
}
return _gnu_sharedProcessInfoObject;
}
+ (BOOL) _exists: (int)pid
{
if (pid > 0)
{
#if defined(_WIN32)
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION,0,pid);
if (h == NULL && GetLastError() != ERROR_ACCESS_DENIED)
{
return NO;
}
CloseHandle(h);
#else
if (kill(pid, 0) < 0 && errno == ESRCH)
{
return NO;
}
#endif
return YES;
}
return NO;
}
- (NSArray *) arguments
{
return _gnu_arguments;
}
- (NSDictionary *) environment
{
return _gnu_environment;
}
- (NSString *) globallyUniqueString
{
static unsigned long counter = 0;
unsigned long count;
static NSString *host = nil;
NSString *thost = nil;
static int pid = 0;
int tpid = 0;
static unsigned long start;
/* We obtain the host name and pid outside the locked region in case
* the lookup is slow or indirectly calls this method fromm another
* thread (as unlikely as that is ... some subclass/category could
* do it).
*/
if (nil == host)
{
thost = [[self hostName] stringByReplacingString: @"." withString: @"_"];
tpid = [self processIdentifier];
}
[procLock lock];
if (nil == host)
{
start = (unsigned long)GSPrivateTimeNow();
ASSIGN(host, thost);
pid = tpid;
}
count = counter++;
[procLock unlock];
// $$$ The format of the string is not specified by the OpenStep
// specification.
return [NSString stringWithFormat: @"%@_%x_%lx_%lx",
host, pid, start, count];
}
- (NSString *) hostName
{
if (!_gnu_hostName)
{
_gnu_hostName = [[NSHost _myHostName] copy];
}
return _gnu_hostName;
}
static void determineOperatingSystem()
{
if (_operatingSystem == 0)
{
NSString *os = nil;
BOOL parseOS = YES;
#if defined(_WIN32)
OSVERSIONINFOW osver;
osver.dwOSVersionInfoSize = sizeof(osver);
GetVersionExW (&osver);
/* Hmm, we could use this to determine operating system version, but
* that would not distinguish between mingw and cygwin, so we just
* use the information from NSBundle and only get the version info
* here.
*/
_operatingSystemVersion = [[NSString alloc] initWithFormat: @"%lu.%lu",
osver.dwMajorVersion, osver.dwMinorVersion];
#else
#if defined(HAVE_SYS_UTSNAME_H)
struct utsname uts;
/* The system supports uname, so we can use it rather than the
* value determined at configure/compile time.
* That's good if the binary is running on a system other than
* the one it was built for (rare, but can happen).
*/
if (!(uname(&uts) < 0))
{
os = [NSString stringWithCString: uts.sysname encoding: [NSString defaultCStringEncoding]];
os = [os lowercaseString];
/* Get the operating system version ... usually the version string
* is pretty horrible, and the kernel release string actually
* makes more sense.
*/
_operatingSystemVersion = [[NSString alloc]
initWithCString: uts.release
encoding: [NSString defaultCStringEncoding]];
/* Hack for sunos/solaris ... sunos version 5 is solaris
*/
if ([os isEqualToString: @"sunos"] == YES
&& [_operatingSystemVersion intValue] > 4)
{
os = @"solaris";
}
}
#endif /* HAVE_SYS_UTSNAME_H */
#endif /* _WIN32 */
if (_operatingSystemVersion == nil)
{
NSWarnFLog(@"Unable to determine system version, using 0.0");
_operatingSystemVersion = @"0.0";
}
while (parseOS == YES)
{
NSString *fallback = [NSBundle _gnustep_target_os];
if (os == nil)
{
os = fallback;
}
parseOS = NO;
if ([os hasPrefix: @"linux"] == YES)
{
_operatingSystemName = @"GSGNULinuxOperatingSystem";
_operatingSystem = GSGNULinuxOperatingSystem;
}
else if ([os hasPrefix: @"mingw"] == YES
|| [os isEqualToString: @"windows"] == YES)
{
_operatingSystemName = @"NSWindowsNTOperatingSystem";
_operatingSystem = NSWindowsNTOperatingSystem;
}
else if ([os isEqualToString: @"cygwin"] == YES)
{
_operatingSystemName = @"GSCygwinOperatingSystem";
_operatingSystem = GSCygwinOperatingSystem;
}
else if ([os hasPrefix: @"bsd"] == YES
|| [os hasPrefix: @"freebsd"] == YES
|| [os hasPrefix: @"netbsd"] == YES
|| [os hasPrefix: @"openbsd"] == YES)
{
_operatingSystemName = @"GSBSDOperatingSystem";
_operatingSystem = GSBSDOperatingSystem;
}
else if ([os hasPrefix: @"beos"] == YES)
{
_operatingSystemName = @"GSBeOperatingSystem";
_operatingSystem = GSBeOperatingSystem;
}
else if ([os hasPrefix: @"darwin"] == YES)
{
_operatingSystemName = @"NSMACHOperatingSystem";
_operatingSystem = NSMACHOperatingSystem;
}
else if ([os hasPrefix: @"solaris"] == YES)
{
_operatingSystemName = @"NSSolarisOperatingSystem";
_operatingSystem = NSSolarisOperatingSystem;
}
else if ([os hasPrefix: @"hpux"] == YES)
{
_operatingSystemName = @"NSHPUXOperatingSystem";
_operatingSystem = NSHPUXOperatingSystem;
}
else if ([os hasPrefix: @"sunos"] == YES)
{
_operatingSystemName = @"NSSunOSOperatingSystem";
_operatingSystem = NSSunOSOperatingSystem;
}
else if ([os hasPrefix: @"osf"] == YES)
{
_operatingSystemName = @"NSOSF1OperatingSystem";
_operatingSystem = NSOSF1OperatingSystem;
}
if (_operatingSystem == 0 && [os isEqual: fallback] == NO)
{
os = fallback;
parseOS = YES; // Try again with fallback
}
}
if (_operatingSystem == 0)
{
NSWarnFLog(@"Unable to determine O/S ... assuming GNU/Linux");
_operatingSystemName = @"GSGNULinuxOperatingSystem";
_operatingSystem = GSGNULinuxOperatingSystem;
}
}
}
- (NSUInteger) operatingSystem
{
if (_operatingSystem == 0)
{
determineOperatingSystem();
}
return _operatingSystem;
}
- (NSString*) operatingSystemName
{
if (_operatingSystemName == 0)
{
determineOperatingSystem();
}
return _operatingSystemName;
}
- (NSString *) operatingSystemVersionString
{
if (_operatingSystemVersion == nil)
{
determineOperatingSystem();
}
return _operatingSystemVersion;
}
- (int) processIdentifier
{
int pid;
#if defined(_WIN32)
pid = (int)GetCurrentProcessId();
#else
pid = (int)getpid();
#endif
return pid;
}
- (NSString *) processName
{
return _gnu_processName;
}
- (void) setProcessName: (NSString *)newName
{
if (newName && [newName length])
{
[_gnu_processName autorelease];
_gnu_processName = [newName copyWithZone: [self zone]];
}
return;
}
- (NSUInteger) processorCount
{
static NSUInteger procCount = 0;
static BOOL beenHere = NO;
if (beenHere == NO)
{
#if defined(_WIN32)
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
#elif defined(_SC_NPROCESSORS_CONF)
procCount = sysconf(_SC_NPROCESSORS_CONF);
#elif defined(HAVE_SYSCTLBYNAME)
int val;
size_t len = sizeof(val);
if (sysctlbyname("hw.ncpu", &val, &len, 0, 0) == 0)
{
procCount = val;
}
#elif defined(HAVE_PROCFS)
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: @"/proc/cpuinfo"])
{
NSString *cpuInfo;
NSArray *a;
unsigned i;
cpuInfo = [NSString stringWithContentsOfFile: @"/proc/cpuinfo"];
a = [cpuInfo componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// syntax is processor : #
// count up each one
for (i = 0; i < [a count]; ++i)
{
if ([[a objectAtIndex: i] isEqualToString: @"processor"])
{
if (((i+1) < [a count])
&& [[a objectAtIndex: i+1] isEqualToString: @":"])
{
procCount++;
}
}
}
}
#else
#warning "no known way to determine number of processors on this system"
#endif
beenHere = YES;
if (procCount == 0)
{
NSLog(@"Cannot determine processor count.");
}
}
return procCount;
}
- (NSUInteger) activeProcessorCount
{
#if defined(_WIN32)
SYSTEM_INFO info;
int index;
int count = 0;
GetSystemInfo(&info);
for (index = 0; index < 32; index++)
{
if (info.dwActiveProcessorMask & (1<<index))
{
count++;
}
}
return count;
#elif defined(_SC_NPROCESSORS_ONLN)
return sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(HAVE_SYSCTLBYNAME)
int val;
size_t len = sizeof(val);
if (sysctlbyname("kern.smp.cpus", &val, &len, 0, 0) == 0)
{
return val;
}
else if (sysctlbyname("hw.activecpu", &val, &len, 0, 0) == 0)
{
return val;
}
return [self processorCount];
#else
return [self processorCount];
#endif
}
- (unsigned long long) physicalMemory
{
static NSUInteger availMem = 0;
static BOOL beenHere = NO;
if (beenHere == NO)
{
#if defined(_WIN32)
MEMORYSTATUSEX memory;
memory.dwLength = sizeof(memory);
GlobalMemoryStatusEx(&memory);
return memory.ullTotalPhys;
#elif defined(_SC_PHYS_PAGES)
availMem = sysconf(_SC_PHYS_PAGES) * NSPageSize();
#elif defined(HAVE_SYSCTLBYNAME)
long val;
size_t len = val;
if (sysctlbyname("hw.physmem", &val, &len, 0, 0) == 0)
{
availMem = val;
}
#elif defined(HAVE_PROCFS)
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: @"/proc/meminfo"])
{
NSString *memInfo;
NSString *s;
NSArray *a;
NSRange r;
memInfo = [NSString stringWithContentsOfFile: @"/proc/meminfo"];
r = [memInfo rangeOfString: @"MemTotal:"];
if (r.location == NSNotFound)
{
NSLog(@"Cannot determine amount of physical memory.");
return 0;
}
s = [[memInfo substringFromIndex: (r.location + r.length)]
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
a = [s componentsSeparatedByString: @" "];
s = [a objectAtIndex: 0];
availMem = (NSUInteger)[s longLongValue];
availMem *= NSPageSize();
}
#else
#warning "no known way to determine amount of memory on this system"
#endif
beenHere = YES;
if (availMem == 0)
{
NSLog(@"Cannot determine amount of physical memory.");
}
}
return availMem;
}
- (NSUInteger) systemUptime
{
NSUInteger uptime = 0;
#if defined(_WIN32)
#if _WIN32_WINNT < 0x0600 /* less than Vista */
uptime = GetTickCount() / 1000;
#else
uptime = GetTickCount64() / 1000;
#endif
#elif defined(HAVE_SYSCTLBYNAME)
struct timeval tval;
size_t len = sizeof(tval);
if (sysctlbyname("kern.boottime", &tval, &len, 0, 0) == 0)
{
uptime = tval.tv_sec;
}
#elif defined(HAVE_PROCFS)
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: @"/proc/uptime"])
{
NSString *uptimeContent = [NSString
stringWithContentsOfFile: @"/proc/uptime"];
NSString *uptimeString = [[uptimeContent
componentsSeparatedByString:@" "] objectAtIndex:0];
uptime = [uptimeString intValue];
}
#else
#warning "no known way to determine uptime on this system"
#endif
if (uptime == 0)
{
NSLog(@"Cannot determine uptime.");
}
return uptime;
}
- (void) enableSuddenTermination
{
// FIXME: unimplemented
return;
}
- (void) disableSuddenTermination
{
// FIXME: unimplemented
return;
}
- (id) beginActivityWithOptions: (NSActivityOptions)options
reason: (NSString *)reason
{
// FIXME: unimplemented
return nil;
}
- (void) endActivity:(id<NSObject>)activity
{
// FIXME: unimplemented
return;
}
- (void) performActivityWithOptions:(NSActivityOptions)options
reason: (NSString *)reason
usingBlock: (GSPerformActivityBlock)block
{
// FIXME: unimplemented
return;
}
- (void) performExpiringActivityWithReason: (NSString *)reason
usingBlock: (GSPerformExpiringActivityBlock)block
{
// FIXME: unimplemented
return;
}
@end
void
GSInitializeProcess(int argc, char **argv, char **envp)
{
[NSProcessInfo class];
[procLock lock];
fallbackInitialisation = YES;
_gnu_process_args(argc, argv, envp);
[procLock unlock];
}
#ifdef __ANDROID__
static NSString *
_NSStringFromJString(JNIEnv *env, jstring jstr)
{
const jchar *unichars = (*env)->GetStringChars(env, jstr, NULL);
jsize len = (*env)->GetStringLength(env, jstr);
NSString *result = [NSString stringWithCharacters:unichars length:len];
(*env)->ReleaseStringChars(env, jstr, unichars);
return result;
}
void
GSInitializeProcessAndroid(JNIEnv *env, jobject context)
{
jclass contextCls = (*env)->GetObjectClass(env, context);
GS_JNI_CLS_CHECK(env, contextCls, "L/android/content/Context;");
// get package code path (path to APK)
jmethodID packageCodePathMethod = (*env)->GetMethodID(env, contextCls, "getPackageCodePath", "()Ljava/lang/String;");
GS_JNI_METH_CHECK(env, packageCodePathMethod);
jstring packageCodePathJava = (*env)->CallObjectMethod(env, context, packageCodePathMethod);
GS_JNI_CHECK(env, packageCodePathJava);
const char *packageCodePath = (*env)->GetStringUTFChars(env, packageCodePathJava, NULL);
// get package name
jmethodID packageNameMethod = (*env)->GetMethodID(env, contextCls, "getPackageName", "()Ljava/lang/String;");
GS_JNI_CHECK(env, packageNameMethod);
jstring packageNameJava = (*env)->CallObjectMethod(env, context, packageNameMethod);
GS_JNI_CHECK(env, packageNameJava);
const char *packageName = (*env)->GetStringUTFChars(env, packageNameJava, NULL);
// create fake executable path consisting of package code path (without .apk)
// and package name as executable
char *lastSlash = strrchr(packageCodePath, '/');
if (lastSlash == NULL)
{
lastSlash = (char *)packageCodePath + strlen(packageCodePath);
}
char *arg0;
asprintf(&arg0, "%.*s/%s", (int)(lastSlash - packageCodePath), packageCodePath, packageName);
// get current locale
jclass localeCls = (*env)->FindClass(env, "java/util/Locale");
GS_JNI_CLS_CHECK(env, localeCls, "Ljava/util/Locale;");
jmethodID localeDefaultMethod = (*env)->GetStaticMethodID(env, localeCls, "getDefault", "()Ljava/util/Locale;");
GS_JNI_METH_CHECK(env, localeDefaultMethod);
jmethodID localeIdMethod = (*env)->GetMethodID(env, localeCls, "toLanguageTag", "()Ljava/lang/String;");
GS_JNI_METH_CHECK(env, localeIdMethod);
jobject localeObj = (*env)->CallStaticObjectMethod(env, localeCls, localeDefaultMethod);
GS_JNI_CHECK(env, localeObj);
jstring localeIdJava = (*env)->CallObjectMethod(env, localeObj, localeIdMethod);
GS_JNI_CHECK(env, localeIdJava);
const char *localeIdOrig = (*env)->GetStringUTFChars(env, localeIdJava, NULL);
// Android uses dashes as delimiters (e.g "en-US"), but we expect underscores
char *localeId = strdup(localeIdOrig);
for (int i = 0; localeId[i]; i++) {
if (localeId[i] == '-') {
localeId[i] = '_';
}
}
char *localeList = NULL;
#if __ANDROID_API__ >= 24
// get locales ordered by user preference
jclass localeListCls = (*env)->FindClass(env, "android/os/LocaleList");
GS_JNI_CLS_CHECK(env, localeListCls, "L/android/os/LocaleList;");
jmethodID localeListGetDefaultMethod = (*env)->GetStaticMethodID(env, localeListCls, "getDefault", "()Landroid/os/LocaleList;");
GS_JNI_METH_CHECK(env, localeListGetDefaultMethod);
jobject localeListObj = (*env)->CallStaticObjectMethod(env, localeListCls, localeListGetDefaultMethod);
GS_JNI_CHECK(env, localeListObj);
// Retrieve string representation of the locale list
jmethodID localeListToLanguageTagsMethod = (*env)->GetMethodID(env, localeListCls, "toLanguageTags", "()Ljava/lang/String;");
GS_JNI_METH_CHECK(env, localeListToLanguageTagsMethod);
jstring localeListJava = (*env)->CallObjectMethod(env, localeListObj, localeListToLanguageTagsMethod);
GS_JNI_CHECK(env, localeListJava);
const char *localeListOrig = (*env)->GetStringUTFChars(env, localeIdJava, NULL);
// Some devices return with it enclosed in []'s so check if both exists before
// removing to ensure it is formatted correctly
if (localeListOrig[0] == '[' && localeListOrig[strlen(localeListOrig) - 1] == ']') {
localeList = strdup(localeListOrig + 1);
localeList[strlen(localeList) - 1] = '\0';
} else {
localeList = strdup(localeListOrig);
}
// NOTE: This is an IETF BCP 47 language tag and may not correspond exactly tocorrespond ll-CC format
// e.g. gsw-u-sd-chzh is a valid BCP 47 language tag, but uses an ISO 639-3 subtag to classify the language.
// There is no easy fix to this, as we use ISO 639-2 subtags internally.
for (int i = 0; localeList[i]; i++) {
if (localeList[i] == '-') {
localeList[i] = '_';
}
}
(*env)->ReleaseStringUTFChars(env, localeListJava, localeListOrig);
#endif
jclass timezoneCls = (*env)->FindClass(env, "java/util/TimeZone");
GS_JNI_CLS_CHECK(env, timezoneCls, "Ljava/util/TimeZone;");
jmethodID timezoneDefaultMethod = (*env)->GetStaticMethodID(env, timezoneCls, "getDefault", "()Ljava/util/TimeZone;");
GS_JNI_METH_CHECK(env, timezoneDefaultMethod);
jmethodID timezoneIdMethod = (*env)->GetMethodID(env, timezoneCls, "getID", "()Ljava/lang/String;");
GS_JNI_METH_CHECK(env, timezoneIdMethod);
jobject timezoneObj = (*env)->CallStaticObjectMethod(env, timezoneCls, timezoneDefaultMethod);
GS_JNI_CHECK(env, timezoneObj);
jstring timezoneIdJava = (*env)->CallObjectMethod(env, timezoneObj, timezoneIdMethod);
GS_JNI_CHECK(env, timezoneIdJava);
const char *timezoneId = (*env)->GetStringUTFChars(env, timezoneIdJava, NULL);
char *localeListValue = "";
if (localeList) {
localeListValue = localeList;
}
// initialize process with these options
char *argv[] = {
arg0,
"-Locale", localeId,
"-Local Time Zone", (char *)timezoneId,
"-GSAndroidLocaleList", localeListValue,
"-GSLogSyslog", "YES" // use syslog (available via logcat) instead of stdout/stderr (not available on Android)
};
GSInitializeProcessAndroidWithArgs(env, context, sizeof(argv)/sizeof(char *), argv, NULL);
free(arg0);
free(localeId);
free(localeList);
(*env)->ReleaseStringUTFChars(env, packageCodePathJava, packageCodePath);
(*env)->ReleaseStringUTFChars(env, packageNameJava, packageName);
(*env)->ReleaseStringUTFChars(env, localeIdJava, localeIdOrig);
(*env)->ReleaseStringUTFChars(env, timezoneIdJava, timezoneId);
}
void
GSInitializeProcessAndroidWithArgs(JNIEnv *env, jobject context, int argc, char **argv, char **envp)
{
[NSProcessInfo class];
// create global reference to to prevent garbage collection
_androidContext = (*env)->NewGlobalRef(env, context);
// initialize process
[procLock lock];
fallbackInitialisation = YES;
_gnu_process_args(argc, argv, NULL);
[procLock unlock];
jclass contextCls = (*env)->GetObjectClass(env, context);
GS_JNI_CLS_CHECK(env, contextCls, "L/android/content/Context;");
// get File class and path method
jclass fileCls = (*env)->FindClass(env, "java/io/File");
GS_JNI_CLS_CHECK(env, fileCls, "Ljava/io/File;");
jmethodID getAbsolutePathMethod = (*env)->GetMethodID(env, fileCls, "getAbsolutePath", "()Ljava/lang/String;");
GS_JNI_METH_CHECK(env, getAbsolutePathMethod);
// get Android files dir
jmethodID filesDirMethod = (*env)->GetMethodID(env, contextCls, "getFilesDir", "()Ljava/io/File;");
GS_JNI_METH_CHECK(env, filesDirMethod);
jobject filesDirObj = (*env)->CallObjectMethod(env, context, filesDirMethod);
GS_JNI_CHECK(env, filesDirObj);
jstring filesDirJava = (*env)->CallObjectMethod(env, filesDirObj, getAbsolutePathMethod);
GS_JNI_CHECK(env, filesDirJava);
_androidFilesDir = _NSStringFromJString(env, filesDirJava);
// get Android cache dir
jmethodID cacheDirMethod = (*env)->GetMethodID(env, contextCls, "getCacheDir", "()Ljava/io/File;");
GS_JNI_METH_CHECK(env, cacheDirMethod);
jobject cacheDirObj = (*env)->CallObjectMethod(env, context, cacheDirMethod);
GS_JNI_CHECK(env, cacheDirObj);
jstring cacheDirJava = (*env)->CallObjectMethod(env, cacheDirObj, getAbsolutePathMethod);
GS_JNI_CHECK(env, cacheDirJava);
_androidCacheDir = _NSStringFromJString(env, cacheDirJava);
// get asset manager and initialize NSBundle
jmethodID assetManagerMethod = (*env)->GetMethodID(env, contextCls, "getAssets", "()Landroid/content/res/AssetManager;");
GS_JNI_METH_CHECK(env, assetManagerMethod);
jstring assetManagerJava = (*env)->CallObjectMethod(env, context, assetManagerMethod);
GS_JNI_CHECK(env, assetManagerJava);
[NSBundle setJavaAssetManager:assetManagerJava withJNIEnv:env];
// clean up our NSTemporaryDirectory() if it exists
NSString *tempDirName = [_androidCacheDir stringByAppendingPathComponent: @"tmp"];
[[NSFileManager defaultManager] removeItemAtPath:tempDirName error:NULL];
}
#endif
@implementation NSProcessInfo (GNUstep)
+ (void) initializeWithArguments: (char**)argv
count: (int)argc
environment: (char**)env
{
GSInitializeProcess(argc, argv, env);
}
- (BOOL) setLogFile: (NSString*)path
{
extern int _NSLogDescriptor;
int desc;
#if defined(_WIN32)
desc = _wopen((wchar_t*)[path fileSystemRepresentation],
O_RDWR|O_CREAT|O_APPEND, 0644);
#else
desc = open([path fileSystemRepresentation], O_RDWR|O_CREAT|O_APPEND, 0644);
#endif
if (desc >= 0)
{
if (_NSLogDescriptor >= 0 && _NSLogDescriptor != 2)
{
close(_NSLogDescriptor);
}
_NSLogDescriptor = desc;
return YES;
}
return NO;
}
#ifdef __ANDROID__
- (jobject) androidContext
{
return _androidContext;
}
- (NSString *) androidFilesDir
{
return _androidFilesDir;
}
- (NSString *) androidCacheDir
{
return _androidCacheDir;
}
#endif
@end
BOOL
GSPrivateEnvironmentFlag(const char *name, BOOL def)
{
const char *c = getenv(name);
BOOL a = def;
if (c != 0)
{
a = NO;
if ((c[0] == 'y' || c[0] == 'Y') && (c[1] == 'e' || c[1] == 'E')
&& (c[2] == 's' || c[2] == 'S') && c[3] == 0)
{
a = YES;
}
else if ((c[0] == 't' || c[0] == 'T') && (c[1] == 'r' || c[1] == 'R')
&& (c[2] == 'u' || c[2] == 'U') && (c[3] == 'e' || c[3] == 'E')
&& c[4] == 0)
{
a = YES;
}
else if (isdigit(c[0]) && c[0] != '0')
{
a = YES;
}
}
return a;
}
const char*
GSPrivateArgZero()
{
if (_gnu_arg_zero == 0)
return "";
else
return _gnu_arg_zero;
}
|