-
Notifications
You must be signed in to change notification settings - Fork 392
/
si.cpp
3199 lines (2811 loc) · 114 KB
/
si.cpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/*
* $RCSfile: si.c,v $
* $Revision: 1.64 $
* $Date: 2012-12-05 05:27:54 $
*/
#if defined(J9ZOS390)
#define _UNIX03_SOURCE
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if defined(LINUX)
#include <linux/magic.h>
#include <sys/vfs.h>
#include <sched.h>
#include <fstream>
#include <regex>
#endif /* defined(LINUX) */
#if defined(OMR_OS_WINDOWS)
#include <direct.h>
#endif /* defined(OMR_OS_WINDOWS) */
#if !defined(OMR_OS_WINDOWS)
#include <grp.h>
#include <errno.h>
#if defined(J9ZOS390)
#include <limits.h>
#else
#define __STDC_LIMIT_MACROS
#include <stdint.h> /* For INT64_MAX. */
#endif /* defined(J9ZOS390) */
#include <sys/resource.h> /* For RLIM_INFINITY */
#endif /* !defined(OMR_OS_WINDOWS) */
#if defined(OMR_OS_WINDOWS)
#include <windows.h>
#else /* defined(OMR_OS_WINDOWS) */
#include <sys/wait.h>
#include <unistd.h>
#endif /* defined(OMR_OS_WINDOWS) */
#if defined(J9ZOS390) && !defined(OMR_EBCDIC)
#include "atoe.h"
#endif
#include "testHelpers.hpp"
#if defined(OMR_OS_WINDOWS)
#define J9DIRECTORY_SEPARATOR_CHARACTER '\\'
#define J9FILE_EXTENSION ".exe"
#define J9FILE_EXTENSION_LENGTH (sizeof(J9FILE_EXTENSION) - 1)
#else
#define J9DIRECTORY_SEPARATOR_CHARACTER '/'
#endif /* defined(OMR_OS_WINDOWS) */
/* Under the standard compiler configuration, INT64_MAX is
* not available; instead, LONGLONG_MAX is defined by xLC.
*/
#if defined(J9ZOS390) && !defined(INT64_MAX)
#define INT64_MAX LONGLONG_MAX
#endif /* defined(J9ZOS390) && !defined(INT64_MAX) */
/**
* @brief Function takes an expected name for an executable and also the name that was actually
* found using the Port API. It then tries to match these, in a OS specific manner. For example,
* on Unices (and z/OS), executables names could be ./exec or full-path to the executable; on
* windows we do not require ./ as long as the executable is in the current directory, and so on.
*
* @param expected The expected executable name (typically, argv[0]).
* @param found The executable name as found by the Port API.
*
* @return Boolean true, if the names match; false, otherwise.
*/
BOOLEAN
validate_executable_name(const char *expected, const char *found)
{
const char *expected_base_path = NULL;
uintptr_t expected_length = 0;
const char *found_base_path = NULL;
uintptr_t found_length = 0;
uintptr_t length = 0;
if ((NULL == expected) || (NULL == found)) {
return FALSE;
}
/* Extract the executable name from the expected path and path found. Compare these. */
expected_base_path = strrchr(expected, J9DIRECTORY_SEPARATOR_CHARACTER);
/* Move past the directory separator, if found. */
expected_base_path = (NULL != expected_base_path) ? (expected_base_path + 1) : expected;
expected_length = strlen(expected_base_path);
found_base_path = strrchr(found, J9DIRECTORY_SEPARATOR_CHARACTER);
/* Move past the directory separator, if found. */
found_base_path = (NULL != found_base_path) ? (found_base_path + 1) : found;
found_length = strlen(found_base_path);
length = found_length;
/* On Windows, disregard comparing the extension, should this be dropped on the command
* line (API always returns executable name including the extension (.exe).
*/
#if defined(OMR_OS_WINDOWS)
/* Check whether argv0 ends with the extension ".exe". If not, we need to reduce
* the number of characters to compare (against the executable name found by API).
*/
if (strncmp(expected_base_path + (expected_length - J9FILE_EXTENSION_LENGTH),
J9FILE_EXTENSION,
J9FILE_EXTENSION_LENGTH) != 0) {
length -= J9FILE_EXTENSION_LENGTH;
}
#endif /* defined(OMR_OS_WINDOWS) */
if (length == expected_length) {
if (strncmp(found_base_path, expected_base_path, length) == 0) {
return TRUE;
}
}
return FALSE;
}
#define CPU_BURNER_BUFF_SIZE 10000
static uintptr_t
cpuBurner(OMRPortLibrary *portLibrary, const char *myText)
{
/* burn up CPU */
uintptr_t counter = 0;
char buffer[CPU_BURNER_BUFF_SIZE];
char *result = NULL;
for (counter = 0; counter < CPU_BURNER_BUFF_SIZE; ++counter) {
buffer[counter] = 0;
}
for (counter = 0; (strlen(buffer) + strlen(myText) + 1) < CPU_BURNER_BUFF_SIZE; ++counter) {
result = strcat(buffer, myText);
if (NULL != strstr(result, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")) {
return 0;
}
}
return 1;
}
TEST(PortSysinfoTest, sysinfo_test0)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
intptr_t rc = 0;
char *executable_name = NULL;
const char *testName = "omrsysinfo_test0";
char *argv0 = portTestEnv->_argv[0];
reportTestEntry(OMRPORTLIB, testName);
#if defined(OMR_OS_WINDOWS)
/* Remove extra "./" from the front of executable name. */
if (0 == strncmp(argv0, "./", 2)) {
argv0 = &argv0[2];
}
/* When this test is invoked from a Windows machine, the input in `argv` may have forward or back slashes. To avoid false
* negatives we normalize the path names to use forward slashes always.
*/
for (char* cursor = argv0; *cursor != '\0'; ++cursor) {
if (*cursor == '/') {
*cursor = '\\';
}
}
#endif /* defined(OMR_OS_WINDOWS) */
rc = omrsysinfo_get_executable_name(NULL, &executable_name);
if (-1 == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, " Executable name test failed: (rc = %d).\n", rc);
goto done;
}
if (validate_executable_name(argv0, /* expected */
executable_name /* found through API */
)
) {
portTestEnv->log("Executable name test passed.\n"
"Expected (argv0=%s).\n Found=%s.\n",
argv0,
executable_name);
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Executable name test failed.\n"
" Expected (argv0=%s).\n Found=%s.\n",
argv0,
executable_name);
goto done;
}
#if 0
/* TODO: Old tests that has yet to convert to the new format.
* Convert these tests to newer form, on an "as need" basis.
*/
uint32_t pid;
int32_t bufSize = 575; /*should sizeof(infoStirng), */
U_16 classpathSeparator;
uintptr_t number_CPUs;
char *result;
const char *osName, *osType, *osVersion,
char infoString[1000], envVar[100];
HEADING(OMRPORTLIB, "SysInfo (si) test");
pid = omrsysinfo_get_pid();
portTestEnv->log("PLT pid=%d\n", pid);
osVersion = omrsysinfo_get_OS_version();
portTestEnv->log("OSversion=%s\n" osVersion ? osVersion : "NULL");
strcpy(envVar, "PATH"); /*case matters?*/
/*check rc before use: -1=BOGUS 0=good else nowrite, rc=varLength*/
rc = omrsysinfo_get_env(envVar, infoString, bufSize);
portTestEnv->log("%s=%s\nbufSize=%d rc=%d\n\n", envVar, infoString, bufSize, rc);
/*
* note: tty_printf has a implementation-dependent limit on out chars
* printf("\n\n%s=%s bufSize=%d rc=%d\n\n",envVar,infoString,bufSize,rc);
*/
osName = omrsysinfo_get_CPU_architecture();
portTestEnv->log("CPU architecture=%s\n", osName ? osName : "NULL");
osType = omrsysinfo_get_OS_type();
portTestEnv->log("OS type=%s\n", osType ? osType : "NULL");
/*SHOULD omrmem_free_memory() result, eh!!!!!! argv[0] not really used*/
/*check rc before use: 0=good -1=error ???*/
number_CPUs = omrsysinfo_get_number_CPUs_by_type(OMRPORT_CPU_ONLINE);
portTestEnv->log("number of CPU(s)=%d\n", number_CPUs);
portTestEnv->log("SI test done!\n\n");
#endif
done:
reportTestExit(OMRPORTLIB, testName);
}
/*
* Test various aspect of omrsysinfo_get_username
*/
TEST(PortSysinfoTest, sysinfo_test1)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define J9SYSINFO_TEST1_USERNAME_LENGTH 1024
const char *testName = "omrsysinfo_test1";
char username[J9SYSINFO_TEST1_USERNAME_LENGTH];
intptr_t length, rc;
reportTestEntry(OMRPORTLIB, testName);
rc = omrsysinfo_get_username(username, J9SYSINFO_TEST1_USERNAME_LENGTH);
if (rc == -1) {
portTestEnv->log(LEVEL_ERROR, "omrsysinfo_get_username returns -1.\n");
portTestEnv->log(LEVEL_ERROR, "If this is a supported platform, consider this as a failure\n");
reportTestExit(OMRPORTLIB, testName);
return;
} else {
char msg[256] = "";
omrstr_printf(msg, sizeof(msg), "User name returned = \"%s\"\n", username);
portTestEnv->log(msg);
}
length = strlen(username);
rc = omrsysinfo_get_username(username, length - 1);
if (length > rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Error if username buffer is too short: rc= %d, Expecting %d\n", rc, 1);
}
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortSysinfoTest, hostname_test)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define J9SYSINFO_HOSTNAME_LENGTH 1024
const char *testName = "omrsysinfo_hostname_test";
char hostname[J9SYSINFO_HOSTNAME_LENGTH];
intptr_t rc = 0;
reportTestEntry(OMRPORTLIB, testName);
rc = omrsysinfo_get_hostname(hostname, J9SYSINFO_HOSTNAME_LENGTH);
if (rc == -1) {
portTestEnv->log(LEVEL_ERROR, "omrsysinfo_get_hostname returned -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
} else {
char msg[256] = "";
omrstr_printf(msg, sizeof(msg), "Host name returned = \"%s\"\n", hostname);
portTestEnv->log(msg);
}
/* Don't check for buffers that are too small, since the call to gethostname() will
* silently truncate the name and return 0 on some platforms, e.g. MacOS.
*/
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortSysinfoTest, sysinfo_test2)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define J9SYSINFO_TEST2_GROUPNAME_LENGTH 1024
const char *testName = "omrsysinfo_test2";
char groupname[J9SYSINFO_TEST2_GROUPNAME_LENGTH];
intptr_t length, rc;
reportTestEntry(OMRPORTLIB, testName);
rc = omrsysinfo_get_groupname(groupname, J9SYSINFO_TEST2_GROUPNAME_LENGTH);
if (rc == -1) {
portTestEnv->log(LEVEL_ERROR, "omrsysinfo_get_groupname returns -1.\n");
portTestEnv->log(LEVEL_ERROR, "If this is a supported platform, consider this as a failure\n");
reportTestExit(OMRPORTLIB, testName);
return;
} else {
char msg[256] = "";
omrstr_printf(msg, sizeof(msg), "Group name returned = \"%s\"\n", groupname);
portTestEnv->log(msg);
}
length = strlen(groupname);
rc = omrsysinfo_get_groupname(groupname, length - 1);
if (length > rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Error if groupname buffer is too short: rc= %d, Expecting %d\n", rc, 1);
}
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortSysinfoTest, sysinfo_get_OS_type_test)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define J9SYSINFO_TEST3_OSNAME_LENGTH 1024
const char *testName = "omrsysinfo_get_OS_type_test";
const char *osName = NULL;
const char *osVersion = NULL;
reportTestEntry(OMRPORTLIB, testName);
osName = omrsysinfo_get_OS_type();
if (osName == NULL) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_OS_type returned NULL - expected OS name.\n", 0, 1);
reportTestExit(OMRPORTLIB, testName);
return;
} else {
char msg[256];
omrstr_printf(msg, sizeof(msg), "omrsysinfo_get_OS_type returned : \"%s\"\n", osName);
portTestEnv->log(msg);
}
osVersion = omrsysinfo_get_OS_version();
if (osVersion == NULL) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_OS_version returned NULL - expected OS name.\n", 0, 1);
reportTestExit(OMRPORTLIB, testName);
return;
} else {
char msg[256];
omrstr_printf(msg, sizeof(msg), "omrsysinfo_get_OS_version returned : \"%s\"\n", osVersion);
portTestEnv->log(msg);
}
#if defined(OMR_OS_WINDOWS)
if (NULL == strstr(osName, "Windows")) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_OS_version does not contain \"Windows\".\n", 0, 1);
reportTestExit(OMRPORTLIB, testName);
return;
} else if (0 == strcmp(osName, "Windows")) {
/*
* This means we are running a new, unrecognized version of Windows. We need to update omrsysinfo_get_OS_type
* to recognize the specific version.
*/
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_OS_version returned the default Windows version.\n", 0, 1);
reportTestExit(OMRPORTLIB, testName);
return;
}
#endif /* defined(OMR_OS_WINDOWS) */
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortSysinfoTest, sysinfo_test3)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test3";
J9PortSysInfoLoadData loadData;
intptr_t rc;
reportTestEntry(OMRPORTLIB, testName);
rc = omrsysinfo_get_load_average(&loadData);
if (rc == -1) {
portTestEnv->log(LEVEL_ERROR, "omrsysinfo_get_load_average returns -1.\n");
portTestEnv->log(LEVEL_ERROR, "If this is a supported platform, consider this as a failure\n");
reportTestExit(OMRPORTLIB, testName);
return;
} else {
portTestEnv->log("Returned data\n");
portTestEnv->log("One Minute Average: %lf\n", loadData.oneMinuteAverage);
portTestEnv->log("Five Minute Average: %lf\n", loadData.fiveMinuteAverage);
portTestEnv->log("Fifteen Minute Average: %lf\n", loadData.fifteenMinuteAverage);
}
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortSysinfoTest, sysinfo_test_sysinfo_ulimit_iterator)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test_sysinfo_ulimit_iterator";
int32_t rc = 0;
J9SysinfoLimitIteratorState state;
J9SysinfoUserLimitElement element;
reportTestEntry(OMRPORTLIB, testName);
portTestEnv->changeIndent(1);
rc = omrsysinfo_limit_iterator_init(&state);
if (0 != rc) {
if (OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM == rc) {
portTestEnv->log(LEVEL_ERROR, "This platform does not support the limit iterator\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_limit_iterator_init returned: %i\n", rc);
}
goto done;
}
while (omrsysinfo_limit_iterator_hasNext(&state)) {
rc = omrsysinfo_limit_iterator_next(&state, &element);
if (0 == rc) {
portTestEnv->log("%s:\n", element.name);
if (OMRPORT_LIMIT_UNLIMITED == element.softValue) {
portTestEnv->log(" soft: unlimited\n");
} else {
portTestEnv->log(" soft: 0x%zX\n", element.softValue);
}
if (OMRPORT_LIMIT_UNLIMITED == element.hardValue) {
portTestEnv->log(" hard: unlimited\n");
} else {
portTestEnv->log(" hard: 0x%zX\n", element.hardValue);
}
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_limit_iterator_next returned: %i when 0 was expected\n", rc);
}
}
done:
portTestEnv->changeIndent(-1);
reportTestExit(OMRPORTLIB, testName);
}
/**
*
* Test the iterator by supplying it with a:
*
* 1. NULL buffer
* 2. Buffer that is big enough for the entire environment
* 3. Buffer that is big enough to contain an env var, but not big enough for the entire environment
* 4. non-null buffer that is not big enough to contain an env var.
*
* In cases but the first (null buffer) attempt to iterate.
*/
TEST(PortSysinfoTest, sysinfo_test_sysinfo_env_iterator)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test_sysinfo_env_iterator";
int32_t rc = 0;
intptr_t envSize = 0;
J9SysinfoEnvIteratorState state;
J9SysinfoEnvElement element;
void *buffer = NULL;
uint32_t bufferSizeBytes = 0;
int l = 0;
#undef SI_DEBUG
reportTestEntry(OMRPORTLIB, testName);
portTestEnv->changeIndent(1);
/* Test 1: NULL buffer - Pass in NULL for buffer, make sure we get back a positive integer describing the size, or a crash */
buffer = NULL;
rc = omrsysinfo_env_iterator_init(&state, buffer, bufferSizeBytes);
if (rc < 0) {
if (OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM == rc) {
portTestEnv->log(LEVEL_ERROR, "This platform does not support the env iterator\n");
} else if (OMRPORT_ERROR_SYSINFO_ENV_INIT_CRASHED_COPYING_BUFFER == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tCrash passing in NULL buffer while running single-threaded. This is a failure because no-one else should have been able to modify it\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_init returned: %i\n", rc);
}
goto done;
} else {
envSize = rc;
#if defined(SI_DEBUG)
portTestEnv->log("Need a buffer of size %x bytes\n", envSize);
#endif
}
/* Test 2: Buffer that is big enough for the entire environment */
buffer = omrmem_allocate_memory(envSize, OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == buffer) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "OutOfMemory allocating buffer for test - where's all the memory?");
goto done;
}
bufferSizeBytes = (uint32_t)envSize;
rc = omrsysinfo_env_iterator_init(&state, buffer, bufferSizeBytes);
if (rc != 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_init returned: %i\n", rc);
goto done;
}
portTestEnv->log("Environment:\n\n");
l = 0;
while (omrsysinfo_env_iterator_hasNext(&state)) {
rc = omrsysinfo_env_iterator_next(&state, &element);
if (0 == rc) {
portTestEnv->log("%s\n", element.nameAndValue);
l++;
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_next returned: %i when 0 was expected\n", rc);
goto done;
break;
}
}
/* Test3: Buffer that is big enough to contain an env var, but not big enough for the entire environment */
omrmem_free_memory(buffer);
bufferSizeBytes = (uint32_t)envSize - 100;
buffer = omrmem_allocate_memory(bufferSizeBytes, OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == buffer) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "OutOfMemory allocating buffer for test - where's all the memory?");
goto done;
}
rc = omrsysinfo_env_iterator_init(&state, buffer, bufferSizeBytes);
if (rc < 0) {
if (OMRPORT_ERROR_SYSINFO_ENV_INIT_CRASHED_COPYING_BUFFER == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tCrash passing in NULL buffer while running single-threaded. This is a failure because no-one else should have been able to modify it\n");
}
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_init returned: %i\n", rc);
goto done;
} else {
envSize = rc;
#if defined(SI_DEBUG)
portTestEnv->log("Should have a buffer of size %x bytes, using one of size %x instead, which will result in a truncated environment\n", envSize, bufferSizeBytes);
#endif
}
l = 0;
while (omrsysinfo_env_iterator_hasNext(&state)) {
rc = omrsysinfo_env_iterator_next(&state, &element);
#if defined(SI_DEBUG)
portTestEnv->log("si.c element.nameAndValue @ 0x%p: %s\n", element.nameAndValue, element.nameAndValue);
#endif
if (0 == rc) {
l++;
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_next returned: %i when 0 was expected\n", rc);
goto done;
break;
}
}
/* Test 4. non-null buffer that is not big enough to contain an env var. */
omrmem_free_memory(buffer);
bufferSizeBytes = 1;
buffer = omrmem_allocate_memory(bufferSizeBytes, OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == buffer) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "OutOfMemory allocating buffer for test - where's all the memory?");
goto done;
}
rc = omrsysinfo_env_iterator_init(&state, buffer, bufferSizeBytes);
if (rc < 0) {
if (OMRPORT_ERROR_SYSINFO_ENV_INIT_CRASHED_COPYING_BUFFER == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tCrash passing in NULL buffer while running single-threaded. This is a failure because no-one else should have been able to modify it\n");
}
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_init returned: %i\n", rc);
goto done;
} else {
envSize = rc;
#if defined(SI_DEBUG)
portTestEnv->log("Should have a buffer of size %x bytes, using one of size %x instead, which will result in a truncated environment\n", envSize, bufferSizeBytes);
#endif
}
l = 0;
while (omrsysinfo_env_iterator_hasNext(&state)) {
rc = omrsysinfo_env_iterator_next(&state, &element);
#if defined(SI_DEBUG)
portTestEnv->log("si.c element.nameAndValue @ 0x%p: %s\n", element.nameAndValue, element.nameAndValue);
#endif
if (0 == rc) {
l++;
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "\tomrsysinfo_env_iterator_next returned: %i when 0 was expected\n", rc);
goto done;
break;
}
}
done:
if (NULL != buffer) {
omrmem_free_memory(buffer);
}
portTestEnv->changeIndent(-1);
reportTestExit(OMRPORTLIB, testName);
}
/* sysinfo_set_limit and sysinfo_get_limit tests will not work on windows */
#if !defined(OMR_OS_WINDOWS)
#if !(defined(AIXPPC) || defined(J9ZOS390))
/**
*
* Test omrsysinfo_test_sysinfo_set_limit and omrsysinfo_test_sysinfo_get_limit
* with resourceID OMRPORT_RESOURCE_ADDRESS_SPACE
*
*/
TEST(PortSysinfoTest, sysinfo_test_sysinfo_set_limit_ADDRESS_SPACE)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test_sysinfo_set_limit_ADDRESS_SPACE";
intptr_t rc;
uint64_t originalCurLimit;
uint64_t originalMaxLimit;
uint64_t currentLimit;
#if defined(OSX)
const uint64_t as1 = 420000000000;
#else /* defined(OSX) */
const uint64_t as1 = 300000;
#endif /* defined(OSX) */
reportTestEntry(OMRPORTLIB, testName);
/* save original soft limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_ADDRESS_SPACE, &originalCurLimit);
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_ADDRESS_SPACE, as1);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_ADDRESS_SPACE, ¤tLimit);
if (as1 == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set ADDRESS_SPACE soft max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set ADDRESS_SPACE soft max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
/* save original hard limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, &originalMaxLimit);
/* lowering the hard limit is irreversible unless privileged */
if (geteuid()) {
/* we should be able to set the hard limit to the current value as an unprivileged user
When the hard limit is set to unlimited (-1) a regular user can change it to
any value, but not back to unlimited. */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, originalMaxLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1\n");
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, ¤tLimit);
if (originalMaxLimit == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set ADDRESS_SPACE hard max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set ADDRESS_SPACE hard max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
} else {
/* now try setting the hard limit */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, as1 + 1);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, ¤tLimit);
if (as1 + 1 == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set ADDRESS_SPACE hard max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set ADDRESS_SPACE hard max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
/* restore original hard limit */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_ADDRESS_SPACE | OMRPORT_LIMIT_HARD, originalMaxLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "restoring the original hard limit failed omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
}
/* restore original soft limit
The soft limit is always <= the hard limit. If the hard limit is lowered to below the soft limit and
then raised again the soft limit isn't automatically raised. */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_ADDRESS_SPACE, originalCurLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "restoring the original soft limit failed omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
reportTestExit(OMRPORTLIB, testName);
}
#endif /* !(defined(AIXPPC) || defined(J9ZOS390)) */
/**
*
* Test omrsysinfo_test_sysinfo_set_limit and omrsysinfo_test_sysinfo_get_limit
* with resourceID OMRPORT_RESOURCE_CORE_FILE
*
*/
TEST(PortSysinfoTest, sysinfo_test_sysinfo_set_limit_CORE_FILE)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test_sysinfo_set_limit_CORE_FILE";
intptr_t rc;
uint64_t originalCurLimit;
uint64_t originalMaxLimit;
uint64_t currentLimit;
const uint64_t coreFileSize = 42;
reportTestEntry(OMRPORTLIB, testName);
/* save original soft limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_CORE_FILE, &originalCurLimit);
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_CORE_FILE, coreFileSize);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_CORE_FILE, ¤tLimit);
if (coreFileSize == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set CORE_FILE soft max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set CORE_FILE soft max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
/* save original hard limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, &originalMaxLimit);
/* lowering the hard limit is irreversible unless privileged */
if (geteuid()) {
/* we should be able to set the hard limit to the current value as an unprivileged user
When the hard limit is set to unlimited (-1) a regular user can change it to
any value, but not back to unlimited. */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, originalMaxLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1\n");
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, ¤tLimit);
if (originalMaxLimit == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set CORE_FILE hard max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set CORE_FILE hard max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
} else {
/* now try setting the hard limit */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, coreFileSize + 1);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, ¤tLimit);
if (coreFileSize + 1 == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set CORE_FILE hard max successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set CORE_FILE hard max FAILED\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
/* restore original hard limit */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_CORE_FILE | OMRPORT_LIMIT_HARD, originalMaxLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "restoring the original hard limit failed omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
}
/* restore original soft limit
The soft limit is always <= the hard limit. If the hard limit is lowered to below the soft limit and
then raised again the soft limit isn't automatically raised. */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_CORE_FILE, originalCurLimit);
if (rc == -1) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "restoring the original soft limit failed omrsysinfo_set_limit returns -1.\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
reportTestExit(OMRPORTLIB, testName);
}
/**
*
* Test omrsysinfo_test_sysinfo_set_limit and omrsysinfo_test_sysinfo_get_limit
* with resourceID OMRPORT_RESOURCE_FILE_DESCRIPTORS
*
*/
TEST(PortSysinfoTest, sysinfo_test_sysinfo_set_limit_FILE_DESCRIPTORS)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_test_sysinfo_set_limit_FILE_DESCRIPTORS";
uint32_t rc = OMRPORT_LIMIT_UNKNOWN;
uint64_t originalSoftLimit = 0;
uint64_t softSetToHardLimit = 0;
uint64_t originalHardLimit = 0;
uint64_t currentLimit = 0;
const uint64_t descriptorLimit = 256;
reportTestEntry(OMRPORTLIB, testName);
/* save original soft limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, &originalSoftLimit);
if (OMRPORT_LIMIT_UNKNOWN == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: OMRPORT_LIMIT_UNKNOWN\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
portTestEnv->log(LEVEL_ERROR, "originalSoftLimit=%llu\n", originalSoftLimit);
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, descriptorLimit);
if (0 != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit FAILED rc=%d\n", rc);
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, ¤tLimit);
if (OMRPORT_LIMIT_UNKNOWN == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: OMRPORT_LIMIT_UNKNOWN\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
if (descriptorLimit == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set FILE_DESCRIPTORS soft limit successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS,
"omrsysinfo_set_limit set FILE_DESCRIPTORS soft limit FAILED originalSoftLimit=%lld Expected=%lld actual==%lld\n",
originalSoftLimit, descriptorLimit, currentLimit);
reportTestExit(OMRPORTLIB, testName);
return;
}
/* save original hard limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS | OMRPORT_LIMIT_HARD, &originalHardLimit);
if (OMRPORT_LIMIT_UNKNOWN == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: OMRPORT_LIMIT_UNKNOWN\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
portTestEnv->log(LEVEL_ERROR, "originalHardLimit=%llu\n", originalHardLimit);
/* set soft limit to hard limit */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, originalHardLimit);
if (0 != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit soft = hard FAILED rc=%d\n", rc);
reportTestExit(OMRPORTLIB, testName);
return;
}
/* get new soft limit */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, &softSetToHardLimit);
if (OMRPORT_LIMIT_UNKNOWN == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: OMRPORT_LIMIT_UNKNOWN\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
portTestEnv->log(LEVEL_ERROR, "soft set to hard limit=%llu\n", softSetToHardLimit);
/* set soft limit to old value */
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, originalSoftLimit);
if (0 != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit reset soft FAILED rc=%d\n", rc);
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS | OMRPORT_LIMIT_HARD, ¤tLimit);
if (currentLimit != originalHardLimit) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: hard limit changed\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
/* lowering the hard limit is irreversible unless privileged */
if (0 != geteuid()) { /* normal user */
/* setting the hard limit from unlimited to a finite value has unpredictable results:
* the actual value may be much smaller than requested.
* In that case, just try setting it to its current value (softSetToHardLimit) or a value slightly lower.
* Ensure that we don't try to set the hard limit to a value less than the current soft limit
* (i.e. originalSoftLimit).
*/
uint64_t newHardLimit = ((OMRPORT_LIMIT_UNLIMITED == rc) || (originalSoftLimit == softSetToHardLimit))
? softSetToHardLimit: softSetToHardLimit - 1;
rc = omrsysinfo_set_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS | OMRPORT_LIMIT_HARD, newHardLimit);
if (0 != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_set_limit set hard limit=%lld FAILED rc=%d\n", rc, newHardLimit);
reportTestExit(OMRPORTLIB, testName);
return;
}
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS | OMRPORT_LIMIT_HARD, ¤tLimit);
if (OMRPORT_LIMIT_UNKNOWN == rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_get_limit FAILED: OMRPORT_LIMIT_UNKNOWN\n");
reportTestExit(OMRPORTLIB, testName);
return;
}
if (newHardLimit == currentLimit) {
portTestEnv->log("omrsysinfo_set_limit set FILE_DESCRIPTORS hard limit successful\n");
} else {
outputErrorMessage(PORTTEST_ERROR_ARGS,
"omrsysinfo_set_limit set FILE_DESCRIPTORS hard limit FAILED originalHardLimit=%lld Expected=%lld actual==%lld\n",
originalHardLimit, newHardLimit, currentLimit);
reportTestExit(OMRPORTLIB, testName);
return;
}
/* Verify that soft limit is unchanged. */
rc = omrsysinfo_get_limit(OMRPORT_RESOURCE_FILE_DESCRIPTORS, ¤tLimit);