-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathOSCachesysv.cpp
2986 lines (2676 loc) · 107 KB
/
OSCachesysv.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 2001
*
* 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
*******************************************************************************/
/**
* @file
* @ingroup Shared_Common
*/
#include <string.h>
#include "j2sever.h"
#include "j9cfg.h"
#include "j9port.h"
#include "pool_api.h"
#include "ut_j9shr.h"
#include "j9shrnls.h"
#include "util_api.h"
#include "OSCachesysv.hpp"
#include "CompositeCacheImpl.hpp"
#include "CacheMap.hpp"
#include "OSCacheFile.hpp"
#include "UnitTest.hpp"
#define OSCACHESYSV_RESTART 4
#define OSCACHESYSV_OPENED 3
#define OSCACHESYSV_CREATED 2
#define OSCACHESYSV_EXIST 1
#define OSCACHESYSV_NOT_EXIST 0
#define OSCACHESYSV_FAILURE -1
#define OSCACHESYSV_SUCCESS 0
#define SEM_HEADERLOCK 0
#define J9SH_OSCACHE_RETRYMAX 30
#define SHM_CACHEHEADERSIZE SHC_PAD(sizeof(OSCachesysv_header_version_current), SHC_WORDALIGN)
#define SHM_CACHEDATASIZE(size) (size-SHM_CACHEHEADERSIZE)
#define SHM_DATASTARTFROMHEADER(header) SRP_GET(header->oscHdr.dataStart, void*);
/**
* Create/Open a new OSCache Instance
*
* This is a wrapper method for @ref SH_OSCache::startup
* This c'tor is currently used during unit testing only. Therefore we pass J9SH_DIRPERM_ABSENT as cacheDirPerm to startup().
*
* @param [in] portLibrary The Port library
* @param [in] sharedClassConfig
* @param [in] cacheName The name of the cache to be opened/created
* @param [in] piconfig Pointer to a configuration structure
* @param [in] numLocks The number of locks to be initialized
* @param [in] createFlag Indicates whether cache is to be opened or created.
* \args J9SH_OSCACHE_CREATE Create the cache if it does not exists, otherwise open existing cache
* \args J9SH_OSCACHE_OPEXIST Open an existing cache only, failed if it doesn't exist.
* @param [in] verboseFlags Verbose flags
* @param [in] openMode Mode to open the cache in. One of the following flags:
* \args J9OSCACHE_OPEN_MODE_DO_READONLY - open the cache readonly
* \args J9OSCACHE_OPEN_MODE_TRY_READONLY_ON_FAIL - if the cache could not be opened read/write - try readonly
* \args J9OSCACHE_OPEN_MODE_GROUPACCESS - creates a cache with group access
* @param [in] versionData Version data of the cache to connect to
* @param [in] i Pointer to an initializer to be used to initialize the data
* area of a new cache
*/
SH_OSCachesysv::SH_OSCachesysv(J9PortLibrary* portLibrary, J9JavaVM* vm, const char* cachedirname, const char* cacheName, J9SharedClassPreinitConfig* piconfig, IDATA numLocks,
UDATA createFlag, UDATA verboseFlags, U_64 runtimeFlags, I_32 openMode, J9PortShcVersion* versionData, SH_OSCache::SH_OSCacheInitializer* i)
{
Trc_SHR_OSC_Constructor_Entry(cacheName, piconfig->sharedClassCacheSize, createFlag);
initialize(portLibrary, NULL, OSCACHE_CURRENT_CACHE_GEN, OSCACHE_CURRENT_LAYER_LAYER);
startup(vm, cachedirname, J9SH_DIRPERM_ABSENT, cacheName, piconfig, numLocks, createFlag, verboseFlags, runtimeFlags, openMode, 0, versionData, i, SHR_STARTUP_REASON_NORMAL);
Trc_SHR_OSC_Constructor_Exit(cacheName);
}
/**
* Method to initialize object variables
*
* @param [in] portLib The Port library
* @param [in] memForConstructor Pointer to the memory to build the OSCachemmap into
* @param [in] generation The generation of this cache
* @param [in] layer The layer number of this cache
*/
void
SH_OSCachesysv::initialize(J9PortLibrary* portLib, char* memForConstructor, UDATA generation, I_8 layer)
{
commonInit(portLib, generation, layer);
_attach_count = 0;
_shmhandle = NULL;
_semhandle = NULL;
_actualCacheSize = 0;
_shmFileName = NULL;
_semFileName = NULL;
_openSharedMemory = false;
_semid = 0;
_groupPerm = 0;
_corruptionCode = NO_CORRUPTION;
_corruptValue = NO_CORRUPTION;
_semAccess = J9SH_SEM_ACCESS_ALLOWED;
_shmAccess = J9SH_SHM_ACCESS_ALLOWED;
}
/**
* setup an OSCache Instance
* This method will create necessary Operating System resources to support cross-process shared memory
* If successful a memory area which can be attached by other process will be created
* User can query the result of the setup operation using getError() method. If the startup has failed,
* all further operation on the shared cache will be failed.
*
* @param [in] cacheName The name of the cache to be opened/created
* @param [in] sharedClassConfig
* @param [in] piconfig Pointer to a configuration structure
* @param [in] numLocks The number of locks to be initialized
* @param [in] createFlag Indicates whether cache is to be opened or created.
* \args J9SH_OSCACHE_CREATE Create the cache if it does not exists, otherwise open existing cache
* \args J9SH_OSCACHE_OPEXIST Open an existing cache only, failed if it doesn't exist.
* @param [in] verboseFlags Verbose flags
* @param [in] openMode Mode to open the cache in. One of the following flags:
* \args J9OSCACHE_OPEN_MODE_DO_READONLY - open the cache readonly
* \args J9OSCACHE_OPEN_MODE_TRY_READONLY_ON_FAIL - if the cache could not be opened read/write - try readonly
* \args J9OSCACHE_OPEN_MODE_GROUPACCESS - creates a cache with group access
* @param [in] versionData Version data of the cache to connect to
* @param [in] i Pointer to an initializer to be used to initialize the data
* area of a new cache
* @param [in] reason Reason for starting up the cache. Not used for non-persistent cache startup
*
* @return true on success, false on failure
*/
bool
SH_OSCachesysv::startup(J9JavaVM* vm, const char* ctrlDirName, UDATA cacheDirPerm, const char* cacheName, J9SharedClassPreinitConfig* piconfig, IDATA numLocks, UDATA create,
UDATA verboseFlags_, U_64 runtimeFlags_, I_32 openMode, UDATA storageKeyTesting, J9PortShcVersion* versionData, SH_OSCache::SH_OSCacheInitializer* i, UDATA reason)
{
IDATA retryCount;
IDATA shsemrc = 0;
IDATA semLength = 0;
LastErrorInfo lastErrorInfo;
UDATA defaultCacheSize = J9_SHARED_CLASS_CACHE_DEFAULT_SIZE;
#if defined(J9VM_ENV_DATA64)
#if defined(OPENJ9_BUILD)
defaultCacheSize = J9_SHARED_CLASS_CACHE_DEFAULT_SIZE_64BIT_PLATFORM;
#else /* defined(OPENJ9_BUILD) */
if (J2SE_VERSION(vm) >= J2SE_V11) {
defaultCacheSize = J9_SHARED_CLASS_CACHE_DEFAULT_SIZE_64BIT_PLATFORM;
}
#endif /* defined(OPENJ9_BUILD) */
#endif /* defined(J9VM_ENV_DATA64) */
PORT_ACCESS_FROM_PORT(_portLibrary);
Trc_SHR_OSC_startup_Entry(cacheName, (piconfig!= NULL)? piconfig->sharedClassCacheSize : defaultCacheSize, create);
if (openMode & J9OSCACHE_OPEN_MODE_GROUPACCESS) {
_groupPerm = 1;
}
versionData->cacheType = J9PORT_SHR_CACHE_TYPE_NONPERSISTENT;
_cacheSize = (piconfig!= NULL) ? (U_32)piconfig->sharedClassCacheSize : (U_32)defaultCacheSize;
_initializer = i;
_totalNumSems = numLocks + 1; /* +1 because of header mutex */
_userSemCntr = 0;
retryCount=J9SH_OSCACHE_RETRYMAX;
_storageKeyTesting = storageKeyTesting;
if (commonStartup(vm, ctrlDirName, cacheDirPerm, cacheName, piconfig, create, verboseFlags_, runtimeFlags_, openMode, versionData) != 0) {
Trc_SHR_OSC_startup_commonStartupFailure();
setError(J9SH_OSCACHE_FAILURE);
return false;
}
Trc_SHR_OSC_startup_commonStartupSuccess();
_shmFileName = _cacheNameWithVGen;
#if defined(WIN32)
_semFileName = _cacheNameWithVGen;
#else /* defined(WIN32) */
semLength = strlen(_cacheNameWithVGen) + (strlen(J9SH_SEMAPHORE_ID) - strlen(J9SH_MEMORY_ID)) + 1;
/* Unfortunate case is that Java5 and early Java6 caches did not have _G append on the semaphore file,
* so to connect with a generation 1 or 2 cache (Java5 was only ever G01), remove the _G01 from the semaphore file name
*/
if (_activeGeneration < 3) {
semLength -= strlen("_GXX");
}
if (!(_semFileName = (char*)j9mem_allocate_memory(semLength, J9MEM_CATEGORY_CLASSES))) {
Trc_SHR_OSC_startup_nameAllocateFailure();
OSC_ERR_TRACE(J9NLS_SHRC_OSCACHE_ALLOC_FAILED);
return false;
}
getCacheVersionAndGen(PORTLIB, vm, _semFileName, semLength, cacheName, versionData, _activeGeneration, false, _layer);
#endif /* defined(WIN32) */
while (retryCount>0) {
IDATA rc;
if (_openMode & J9OSCACHE_OPEN_MODE_DO_READONLY) {
/* Try read-only mode only if shared memory control file exists
* because we can't create a new control file when running in read-only mode.
*/
if (!statCache(_portLibrary, _cacheDirName, _shmFileName, false)) {
OSC_ERR_TRACE(J9NLS_SHRC_OSCACHE_STARTUP_CACHE_CREATION_NOT_ALLOWED_READONLY_MODE);
Trc_SHR_OSC_startup_cacheCreationNotAllowed_readOnlyMode();
rc = OSCACHESYSV_FAILURE;
break;
}
/* Don't get the semaphore set when running read-only, but pretend that we did */
shsemrc = J9PORT_INFO_SHSEM_OPENED;
_semhandle = NULL;
} else {
#if !defined(WIN32)
shsemrc = OpenSysVSemaphoreHelper(versionData, &lastErrorInfo);
#else /* !defined(WIN32) */
/* Currently on windows, "flags" passed to j9shsem_deprecated_open() are not used, but its better to pass correct flags */
UDATA flags = J9SHSEM_NO_FLAGS;
if (J9SH_OSCACHE_OPEXIST_STATS == _createFlags) {
flags = J9SHSEM_OPEN_FOR_STATS;
} else if (J9SH_OSCACHE_OPEXIST_DESTROY == _createFlags) {
flags = J9SHSEM_OPEN_FOR_DESTROY;
} else if (J9SH_OSCACHE_OPEXIST_DO_NOT_CREATE == _createFlags) {
flags = J9SHSEM_OPEN_DO_NOT_CREATE;
}
shsemrc = j9shsem_deprecated_open(_cacheDirName, _groupPerm, &_semhandle, _semFileName, (int)_totalNumSems, 0, flags, NULL);
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
#endif /* !defined(WIN32) */
}
if (shsemrc == J9PORT_INFO_SHSEM_PARTIAL) {
/*
* J9PORT_INFO_SHSEM_PARTIAL indicates one of the following cases:
* - j9shsem_deprecated_openDeprecated() was called and the control files for the semaphore does not exist, or
* - j9shsem_deprecated_openDeprecated() was called and the sysv object matching the control file has been deleted, or
* - j9shsem_deprecated_open() failed and flag OPEXIST_STATS is set, or
* - j9shsem_deprecated_open() was called with flag OPEXIST_DESTROY and control files for the semaphore does not exist
*
* In such cases continue without the semaphore as readonly
*
* If we are starting up the cache for 'destroy' i.e. OPEXIST_DESTROY is set,
* then do not set READONLY flag as it may prevent unlinking of control files in port library if required.
*/
if (J9_ARE_NO_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY)) {
_openMode |= J9OSCACHE_OPEN_MODE_DO_READONLY;
}
shsemrc = J9PORT_INFO_SHSEM_OPENED;
_semhandle = NULL;
}
switch (shsemrc) {
case J9PORT_INFO_SHSEM_CREATED:
#if !defined(WIN32)
if (J9_ARE_ALL_BITS_SET(_openMode, J9OSCACHE_OPEN_MODE_GROUPACCESS)) {
/* Verify if the group access has been set */
struct J9FileStat statBuf;
char pathFileName[J9SH_MAXPATH];
I_32 semid = j9shsem_deprecated_getid(_semhandle);
I_32 groupAccessRc = verifySemaphoreGroupAccess(&lastErrorInfo);
if (0 == groupAccessRc) {
Trc_SHR_OSC_startup_setSemaphoreGroupAccessFailed(semid);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SEMAPHORE_SET_GROUPACCESS_FAILED, semid);
} else if (-1 == groupAccessRc) {
/* Fail to get stats of the semaphore */
Trc_SHR_OSC_startup_badSemaphoreStat(semid);
errorHandler(J9NLS_SHRC_OSCACHE_INTERNAL_ERROR_CHECKING_SEMAPHORE_ACCESS, &lastErrorInfo);
rc = OSCACHESYSV_FAILURE;
break;
}
getCachePathName(PORTLIB, _cacheDirName, pathFileName, J9SH_MAXPATH, _semFileName);
/* No check for return value of getCachePathName() as it always return 0 */
memset(&statBuf, 0, sizeof(statBuf));
if (0 == j9file_stat(pathFileName, 0, &statBuf)) {
if (1 != statBuf.perm.isGroupReadable) {
/* Control file needs to be group readable */
Trc_SHR_OSC_startup_setGroupAccessFailed(pathFileName);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SEM_CONTROL_FILE_SET_GROUPACCESS_FAILED, pathFileName);
}
} else {
Trc_SHR_OSC_startup_badFileStat(pathFileName);
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_FILE_STAT, &lastErrorInfo);
rc = OSCACHESYSV_FAILURE;
break;
}
}
#endif /* !defined(WIN32) */
/*no break here, simply fall thru to the next case and open/create the cache*/
case J9PORT_INFO_SHSEM_OPENED:
/* Avoid any checks for semaphore access if
* - running in readonly mode as we don't use semaphore, or
* - user has specified a cache directory, or
* - destroying an existing cache
*/
if (J9_ARE_NO_BITS_SET(_openMode, J9OSCACHE_OPEN_MODE_DO_READONLY)
&& (J9PORT_INFO_SHSEM_OPENED == shsemrc)
&& (!_isUserSpecifiedCacheDir)
&& (J9_ARE_NO_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY))
) {
_semAccess = checkSemaphoreAccess(&lastErrorInfo);
}
/* Ignore _semAccess when opening cache for printing stats, but we do need it later to display cache usability */
if (J9_ARE_ALL_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_STATS)
|| (J9SH_SEM_ACCESS_ALLOWED == _semAccess)
) {
IDATA headerMutexRc = 0;
if (J9_ARE_NO_BITS_SET(_runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE_CHECK)) {
/* When running "restoreFromSnapshot" utility, headerMutex has already been acquired in the first call of SH_OSCachesysv::startup()*/
headerMutexRc = enterHeaderMutex(&lastErrorInfo);
}
if (0 == headerMutexRc) {
rc = openCache(_cacheDirName, versionData, (shsemrc == J9PORT_INFO_SHSEM_CREATED));
if (J9_ARE_NO_BITS_SET(_runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE | J9SHR_RUNTIMEFLAG_RESTORE_CHECK)) {
/* When running "restoreFromSnapshot" utility, do not release headerMutex here */
if (0 != exitHeaderMutex(&lastErrorInfo)) {
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_EXIT_HDR_MUTEX, &lastErrorInfo);
rc = OSCACHESYSV_FAILURE;
}
}
} else {
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_ENTER_HDR_MUTEX, &lastErrorInfo);
rc = OSCACHESYSV_FAILURE;
}
} else {
switch (_semAccess) {
case J9SH_SEM_ACCESS_CANNOT_BE_DETERMINED:
errorHandler(J9NLS_SHRC_OSCACHE_INTERNAL_ERROR_CHECKING_SEMAPHORE_ACCESS, &lastErrorInfo);
break;
case J9SH_SEM_ACCESS_OWNER_NOT_CREATOR:
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OWNER_NOT_CREATOR, NULL);
break;
case J9SH_SEM_ACCESS_GROUP_ACCESS_REQUIRED:
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_GROUPACCESS_REQUIRED, NULL);
break;
case J9SH_SEM_ACCESS_OTHERS_NOT_ALLOWED:
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OTHERS_ACCESS_NOT_ALLOWED, NULL);
break;
default:
Trc_SHR_Assert_ShouldNeverHappen();
}
rc = OSCACHESYSV_FAILURE;
}
break;
case J9PORT_ERROR_SHSEM_OPFAILED:
case J9PORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_LOCK_FAILED:
case J9PORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_CORRUPT:
case J9PORT_ERROR_SHSEM_OPFAILED_SEMID_MISMATCH:
case J9PORT_ERROR_SHSEM_OPFAILED_SEM_KEY_MISMATCH:
case J9PORT_ERROR_SHSEM_OPFAILED_SEM_SIZE_CHECK_FAILED:
case J9PORT_ERROR_SHSEM_OPFAILED_SEM_MARKER_CHECK_FAILED:
case J9PORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND:
if (J9_ARE_ALL_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY)
&& (J9PORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND == shsemrc)
) {
/* No semaphore set was found when opening for "destroy". Avoid printing any error message. */
rc = OSCACHESYSV_SUCCESS;
} else {
I_32 semid = 0;
/* For some error codes, portlibrary stores semaphore set id obtained from the control file
* enabling us to display it in the error message. Retrieve the id and free memory allocated to handle.
*/
if (NULL != _semhandle) {
semid = j9shsem_deprecated_getid(_semhandle);
j9mem_free_memory(_semhandle);
}
if ((J9PORT_ERROR_SHSEM_OPFAILED == shsemrc) || (J9PORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND == shsemrc)) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED, &lastErrorInfo);
if ((J9PORT_ERROR_SHSEM_OPFAILED == shsemrc) && (0 != semid)) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_DISPLAY_SEMID, semid);
}
} else if (J9PORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_LOCK_FAILED == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_CONTROL_FILE_LOCK_FAILED, &lastErrorInfo);
} else if (J9PORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_CORRUPT == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_CONTROL_FILE_CORRUPT, &lastErrorInfo);
} else if (J9PORT_ERROR_SHSEM_OPFAILED_SEMID_MISMATCH == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_SEMID_MISMATCH, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_DISPLAY_SEMID, semid);
} else if (J9PORT_ERROR_SHSEM_OPFAILED_SEM_KEY_MISMATCH == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_SEM_KEY_MISMATCH, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_DISPLAY_SEMID, semid);
} else if (J9PORT_ERROR_SHSEM_OPFAILED_SEM_SIZE_CHECK_FAILED == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_SEM_SIZE_CHECK_FAILED, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_DISPLAY_SEMID, semid);
} else if (J9PORT_ERROR_SHSEM_OPFAILED_SEM_MARKER_CHECK_FAILED == shsemrc) {
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_SEM_MARKER_CHECK_FAILED, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_OPFAILED_DISPLAY_SEMID, semid);
}
/* Report any error that occurred during unlinking of control file */
if (J9PORT_INFO_CONTROL_FILE_UNLINK_FAILED == _controlFileStatus.status) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SEMAPHORE_CONTROL_FILE_UNLINK_FAILED, _semFileName);
OSC_ERR_TRACE1(J9NLS_SHRC_OSCACHE_PORT_ERROR_NUMBER, _controlFileStatus.errorCode);
OSC_ERR_TRACE1(J9NLS_SHRC_OSCACHE_PORT_ERROR_MESSAGE, _controlFileStatus.errorMsg);
}
}
/* While opening shared cache for "destroy" if some error occurs when opening the semaphore set,
* don't bail out just yet but try to open the shared memory.
*/
if (J9_ARE_ALL_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY)) {
_semhandle = NULL;
/* Skip acquiring header mutex as the semaphore handle is NULL */
rc = openCache(_cacheDirName, versionData, false);
} else if (J9_ARE_ALL_BITS_SET(_openMode, J9OSCACHE_OPEN_MODE_TRY_READONLY_ON_FAIL)) {
/* Try read-only mode for 'nonfatal' option only if shared memory control file exists
* because we can't create a new control file when running in read-only mode.
*/
if (statCache(_portLibrary, _cacheDirName, _shmFileName, false)) {
OSC_TRACE(J9NLS_SHRC_OSCACHE_STARTUP_NONFATAL_TRY_READONLY);
Trc_SHR_OSC_startup_attemptNonfatalReadOnly();
_openMode |= J9OSCACHE_OPEN_MODE_DO_READONLY;
rc = OSCACHESYSV_RESTART;
} else {
rc=OSCACHESYSV_FAILURE;
}
} else {
rc=OSCACHESYSV_FAILURE;
}
break;
case J9PORT_ERROR_SHSEM_WAIT_FOR_CREATION_MUTEX_TIMEDOUT:
errorHandler(J9NLS_SHRC_OSCACHE_SEMAPHORE_WAIT_FOR_CREATION_MUTEX_TIMEDOUT, &lastErrorInfo);
rc=OSCACHESYSV_FAILURE;
break;
default:
errorHandler(J9NLS_SHRC_OSCACHE_UNKNOWN_ERROR, &lastErrorInfo);
rc=OSCACHESYSV_FAILURE;
break;
}
switch (rc) {
case OSCACHESYSV_CREATED:
if (_verboseFlags & J9SHR_VERBOSEFLAG_ENABLE_VERBOSE) {
OSC_TRACE1(J9NLS_SHRC_OSCACHE_SHARED_CACHE_CREATEDA, _cacheName);
}
#if !defined(WIN32)
if (J9_ARE_ALL_BITS_SET(_openMode, J9OSCACHE_OPEN_MODE_GROUPACCESS)) {
/* Verify if the group access has been set */
struct J9FileStat statBuf;
I_32 shmid = j9shmem_getid(_shmhandle);
I_32 groupAccessRc = verifySharedMemoryGroupAccess(&lastErrorInfo);
if (0 == groupAccessRc) {
Trc_SHR_OSC_startup_setSharedMemoryGroupAccessFailed(shmid);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SHARED_MEMORY_SET_GROUPACCESS_FAILED, shmid);
} else if (-1 == groupAccessRc) {
Trc_SHR_OSC_startup_badSharedMemoryStat(shmid);
errorHandler(J9NLS_SHRC_OSCACHE_INTERNAL_ERROR_CHECKING_SHARED_MEMORY_ACCESS, &lastErrorInfo);
retryCount = 0;
continue;
}
memset(&statBuf, 0, sizeof(statBuf));
if (0 == j9file_stat(_cachePathName, 0, &statBuf)) {
if (1 != statBuf.perm.isGroupReadable) {
/* Control file needs to be group readable */
Trc_SHR_OSC_startup_setGroupAccessFailed(_cachePathName);
OSC_WARNING_TRACE1(J9NLS_SHRC_OSCACHE_SHM_CONTROL_FILE_SET_GROUPACCESS_FAILED, _cachePathName);
}
} else {
Trc_SHR_OSC_startup_badFileStat(_cachePathName);
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_FILE_STAT, &lastErrorInfo);
retryCount = 0;
continue;
}
}
#endif /* !defined(WIN32) */
setError(J9SH_OSCACHE_CREATED);
getTotalSize();
Trc_SHR_OSC_startup_Exit_Created(cacheName);
_startupCompleted=true;
return true;
case OSCACHESYSV_OPENED:
if (_verboseFlags & J9SHR_VERBOSEFLAG_ENABLE_VERBOSE) {
if (_runningReadOnly) {
OSC_TRACE1(J9NLS_SHRC_OSCACHE_SYSV_STARTUP_OPENED_READONLY, _cacheName);
} else {
OSC_TRACE1(J9NLS_SHRC_OSCACHE_SHARED_CACHE_OPENEDA, _cacheName);
}
}
setError(J9SH_OSCACHE_OPENED);
getTotalSize();
Trc_SHR_OSC_startup_Exit_Opened(cacheName);
_startupCompleted=true;
return true;
case OSCACHESYSV_RESTART:
Trc_SHR_OSC_startup_attempt_Restart(cacheName);
break;
case OSCACHESYSV_FAILURE:
retryCount = 0;
continue;
case OSCACHESYSV_NOT_EXIST:
/* Currently, this case occurs only when J9SH_OSCACHE_OPEXIST_STATS, J9SH_OSCACHE_OPEXIST_DESTROY or J9SH_OSCACHE_OPEXIST_DO_NOT_CREATE is set and shared memory does not exist. */
setError(J9SH_OSCACHE_NO_CACHE);
Trc_SHR_OSC_startup_Exit_NoCache(cacheName);
return false;
default:
break;
}
retryCount--;
} /* END while (retryCount>0) */
setError(J9SH_OSCACHE_FAILURE);
Trc_SHR_OSC_startup_Exit_Failed(cacheName);
return false;
}
/* The caller should hold the mutex */
IDATA
SH_OSCachesysv::openCache(const char* cacheDirName, J9PortShcVersion* versionData, bool semCreated)
{
/* we are attaching to existing cache! */
Trc_SHR_OSC_openCache_Entry(_cacheName);
IDATA rc;
IDATA result = OSCACHESYSV_FAILURE;
LastErrorInfo lastErrorInfo;
PORT_ACCESS_FROM_PORT(_portLibrary);
rc = shmemOpenWrapper(_shmFileName, &lastErrorInfo);
Trc_SHR_OSC_openCache_shmem_open(_shmFileName, _cacheSize);
switch (rc) {
case J9PORT_ERROR_SHMEM_OPEN_ATTACHED_FAILED:
_openSharedMemory = true;
/* FALLTHROUGH */
case J9PORT_ERROR_SHMEM_CREATE_ATTACHED_FAILED:
/*We failed to attach to the memory.*/
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPEN_ATTACHED_FAILED, &lastErrorInfo);
Trc_SHR_OSC_openCache_ExitAttachFailed();
result = OSCACHESYSV_FAILURE;
break;
case J9PORT_INFO_SHMEM_OPENED:
/* Avoid any checks if
* - user has specified a cache directory, or
* - destroying an existing cache
*/
if (!_isUserSpecifiedCacheDir && (J9_ARE_NO_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY))) {
_shmAccess = checkSharedMemoryAccess(&lastErrorInfo);
}
/* Ignore _shmAccess when opening cache for printing stats, but we do need it later to display cache usability */
if (J9_ARE_ALL_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_STATS)
|| (J9SH_SHM_ACCESS_ALLOWED == _shmAccess)
) {
/* ALL SET */
Trc_SHR_OSC_openCache_Exit_Opened(_cacheName);
result = OSCACHESYSV_OPENED;
} else {
switch (_shmAccess) {
case J9SH_SHM_ACCESS_CANNOT_BE_DETERMINED:
errorHandler(J9NLS_SHRC_OSCACHE_INTERNAL_ERROR_CHECKING_SHARED_MEMORY_ACCESS, &lastErrorInfo);
break;
case J9SH_SHM_ACCESS_OWNER_NOT_CREATOR:
errorHandler(J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OWNER_NOT_CREATOR, NULL);
break;
case J9SH_SHM_ACCESS_GROUP_ACCESS_REQUIRED:
errorHandler(J9NLS_SHRC_OSCACHE_SHARED_MEMORY_GROUPACCESS_REQUIRED, NULL);
break;
case J9SH_SHM_ACCESS_GROUP_ACCESS_READONLY_REQUIRED:
errorHandler(J9NLS_SHRC_OSCACHE_SHARED_MEMORY_GROUPACCESS_READONLY_REQUIRED, NULL);
break;
case J9SH_SHM_ACCESS_OTHERS_NOT_ALLOWED:
errorHandler(J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OTHERS_ACCESS_NOT_ALLOWED, NULL);
break;
default:
Trc_SHR_Assert_ShouldNeverHappen();
}
Trc_SHR_OSC_openCache_ExitAccessNotAllowed(_shmAccess);
result = OSCACHESYSV_FAILURE;
}
break;
case J9PORT_INFO_SHMEM_CREATED:
/* We opened semaphore yet created the cache area -
* we should set it up, but don't need to init semaphore
*/
rc = initializeHeader(cacheDirName, versionData, lastErrorInfo);
if (rc == OSCACHESYSV_FAILURE) {
Trc_SHR_OSC_openCache_Exit_CreatedHeaderInitFailed(_cacheName);
result = OSCACHESYSV_FAILURE;
break;
}
Trc_SHR_OSC_openCache_Exit_Created(_cacheName);
result = OSCACHESYSV_CREATED;
break;
case J9PORT_ERROR_SHMEM_WAIT_FOR_CREATION_MUTEX_TIMEDOUT:
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPEN_WAIT_FOR_CREATION_MUTEX_TIMEDOUT, &lastErrorInfo);
Trc_SHR_OSC_openCache_Exit4();
result = OSCACHESYSV_FAILURE;
break;
case J9PORT_INFO_SHMEM_PARTIAL:
/* If J9PORT_INFO_SHMEM_PARTIAL then ::startup() was called by j9shr_destroy_cache().
* Returning OSCACHESYSV_OPENED will cause j9shr_destroy_cache() to call ::destroy(),
* which will cleanup any control files that have there SysV IPC objects del
*/
result = OSCACHESYSV_OPENED;
break;
case J9PORT_ERROR_SHMEM_OPFAILED:
case J9PORT_ERROR_SHMEM_OPFAILED_CONTROL_FILE_LOCK_FAILED:
case J9PORT_ERROR_SHMEM_OPFAILED_CONTROL_FILE_CORRUPT:
case J9PORT_ERROR_SHMEM_OPFAILED_SHMID_MISMATCH:
case J9PORT_ERROR_SHMEM_OPFAILED_SHM_KEY_MISMATCH:
case J9PORT_ERROR_SHMEM_OPFAILED_SHM_GROUPID_CHECK_FAILED:
case J9PORT_ERROR_SHMEM_OPFAILED_SHM_USERID_CHECK_FAILED:
case J9PORT_ERROR_SHMEM_OPFAILED_SHM_SIZE_CHECK_FAILED:
case J9PORT_ERROR_SHMEM_OPFAILED_SHARED_MEMORY_NOT_FOUND:
default:
if (J9_ARE_ANY_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_STATS | J9SH_OSCACHE_OPEXIST_DESTROY | J9SH_OSCACHE_OPEXIST_DO_NOT_CREATE)
&& (J9PORT_ERROR_SHMEM_OPFAILED_SHARED_MEMORY_NOT_FOUND == rc)
) {
if (J9_ARE_ALL_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DESTROY)) {
/* Absence of shared memory is equivalent to non-existing cache. Do not display any error message,
* but do call cleanupSysVResources() to remove any semaphore set in case we opened it successfully.
*/
cleanupSysvResources();
} else if (J9_ARE_ANY_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_STATS | J9SH_OSCACHE_OPEXIST_DO_NOT_CREATE)) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_NOT_EXIST);
}
Trc_SHR_OSC_openCache_Exit3();
result = OSCACHESYSV_NOT_EXIST;
} else {
I_32 shmid = 0;
/* For some error codes, portlibrary stores shared memory id obtained from the control file
* enabling us to display it in the error message. Retrieve the id and free memory allocated to handle.
*/
if (NULL != _shmhandle) {
shmid = j9shmem_getid(_shmhandle);
j9mem_free_memory(_shmhandle);
}
if ((J9PORT_ERROR_SHMEM_OPFAILED == rc) || (J9PORT_ERROR_SHMEM_OPFAILED_SHARED_MEMORY_NOT_FOUND == rc)) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_V1, &lastErrorInfo);
if ((J9PORT_ERROR_SHMEM_OPFAILED == rc) && (0 != shmid)) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
}
} else if (J9PORT_ERROR_SHMEM_OPFAILED_CONTROL_FILE_LOCK_FAILED == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_CONTROL_FILE_LOCK_FAILED, &lastErrorInfo);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_CONTROL_FILE_CORRUPT == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_CONTROL_FILE_CORRUPT, &lastErrorInfo);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_SHMID_MISMATCH == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_SHMID_MISMATCH, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_SHM_KEY_MISMATCH == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_SHM_KEY_MISMATCH, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_SHM_GROUPID_CHECK_FAILED == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_SHM_GROUPID_CHECK_FAILED, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_SHM_USERID_CHECK_FAILED == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_SHM_USERID_CHECK_FAILED, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
} else if (J9PORT_ERROR_SHMEM_OPFAILED_SHM_SIZE_CHECK_FAILED == rc) {
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_OPFAILED_SHM_SIZE_CHECK_FAILED, &lastErrorInfo);
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_OPFAILED_DISPLAY_SHMID, shmid);
}
/* Report any error that occurred during unlinking of control file */
if (J9PORT_INFO_CONTROL_FILE_UNLINK_FAILED == _controlFileStatus.status) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_SHRC_OSCACHE_SHARED_MEMORY_CONTROL_FILE_UNLINK_FAILED, _shmFileName);
OSC_ERR_TRACE1(J9NLS_SHRC_OSCACHE_PORT_ERROR_NUMBER, _controlFileStatus.errorCode);
OSC_ERR_TRACE1(J9NLS_SHRC_OSCACHE_PORT_ERROR_MESSAGE, _controlFileStatus.errorMsg);
}
#if !defined(WIN32)
if (0 != (_createFlags & J9SH_OSCACHE_OPEXIST_STATS)) {
OSC_TRACE(J9NLS_SHRC_OSCACHE_CONTROL_FILE_RECREATE_PROHIBITED_FETCHING_CACHE_STATS);
} else if (J9_ARE_ANY_BITS_SET(_createFlags, J9SH_OSCACHE_OPEXIST_DO_NOT_CREATE)) {
OSC_TRACE(J9NLS_SHRC_OSCACHE_CONTROL_FILE_RECREATE_PROHIBITED);
} else if (0 != (_openMode & J9OSCACHE_OPEN_MODE_DO_READONLY)) {
OSC_TRACE(J9NLS_SHRC_OSCACHE_CONTROL_FILE_RECREATE_PROHIBITED_RUNNING_READ_ONLY);
}
#endif /* !defined(WIN32) */
Trc_SHR_OSC_openCache_Exit3();
result = OSCACHESYSV_FAILURE;
}
break;
}
return result;
}
IDATA
SH_OSCachesysv::verifyCacheHeader(J9PortShcVersion* versionData)
{
IDATA headerRc = J9SH_OSCACHE_HEADER_OK;
OSCachesysv_header_version_current* header = (OSCachesysv_header_version_current*)_headerStart;
IDATA retryCntr = 0;
LastErrorInfo lastErrorInfo;
IDATA rc = 0;
PORT_ACCESS_FROM_PORT(_portLibrary);
if (header == NULL) {
return J9SH_OSCACHE_HEADER_MISSING;
}
/* In readonly, we can't get a header lock, so if the cache is mid-init, give it a chance to complete initialization */
if (_runningReadOnly) {
while ((!header->oscHdr.cacheInitComplete) && (retryCntr < J9SH_OSCACHE_READONLY_RETRY_COUNT)) {
omrthread_sleep(J9SH_OSCACHE_READONLY_RETRY_SLEEP_MILLIS);
++retryCntr;
}
if (!header->oscHdr.cacheInitComplete) {
return J9SH_OSCACHE_HEADER_MISSING;
}
}
if (J9_ARE_NO_BITS_SET(_runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE | J9SHR_RUNTIMEFLAG_RESTORE_CHECK)) {
/* When running "restoreFromSnapshot" utility, headerMutex is already acquired */
rc = enterHeaderMutex(&lastErrorInfo);
}
if (0 == rc) {
/* First, check the eyecatcher */
if (strcmp(header->eyecatcher, J9PORT_SHMEM_EYECATCHER)) {
OSC_ERR_TRACE(J9NLS_SHRC_OSCACHE_ERROR_WRONG_EYECATCHER);
Trc_SHR_OSC_recreateSemaphore_Exit1();
OSC_ERR_TRACE1(J9NLS_SHRC_OSCACHE_CORRUPT_CACHE_HEADER_BAD_EYECATCHER, header->eyecatcher);
setCorruptionContext(CACHE_HEADER_BAD_EYECATCHER, (UDATA)(header->eyecatcher));
headerRc = J9SH_OSCACHE_HEADER_CORRUPT;
}
if (J9SH_OSCACHE_HEADER_OK == headerRc) {
headerRc = checkOSCacheHeader(&(header->oscHdr), versionData, SHM_CACHEHEADERSIZE);
}
if (J9SH_OSCACHE_HEADER_OK == headerRc) {
if (NULL != _semhandle) {
/*Note: _semhandle will likely be NULL for two reasons:
* - the cache was opened readonly, due to the use of the 'readonly' option
* - the cache was opened readonly, due to the use of 'nonfatal' option
* In both cases we ignore the below check.
*/
_semid = j9shsem_deprecated_getid(_semhandle);
if ((_runtimeFlags & J9SHR_RUNTIMEFLAG_ENABLE_SEMAPHORE_CHECK) && (header->attachedSemid != 0) && (header->attachedSemid != _semid)) {
Trc_SHR_OSC_recreateSemaphore_Exit4(header->attachedSemid, _semid);
OSC_ERR_TRACE2(J9NLS_SHRC_OSCACHE_CORRUPT_CACHE_SEMAPHORE_MISMATCH, header->attachedSemid, _semid);
setCorruptionContext(CACHE_SEMAPHORE_MISMATCH, (UDATA)_semid);
headerRc = J9SH_OSCACHE_SEMAPHORE_MISMATCH;
}
}
}
if (J9_ARE_NO_BITS_SET(_runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE | J9SHR_RUNTIMEFLAG_RESTORE_CHECK)) {
/* When running "restoreFromSnapshot" utility, do not release headerMutex here */
rc = exitHeaderMutex(&lastErrorInfo);
}
if (0 != rc) {
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_EXIT_HDR_MUTEX, &lastErrorInfo);
if (J9SH_OSCACHE_HEADER_OK == headerRc) {
headerRc = J9SH_OSCACHE_HEADER_MISSING;
}
}
} else {
errorHandler(J9NLS_SHRC_OSCACHE_ERROR_ENTER_HDR_MUTEX, &lastErrorInfo);
headerRc = J9SH_OSCACHE_HEADER_MISSING;
}
return headerRc;
}
IDATA
SH_OSCachesysv::detachRegion(void)
{
IDATA rc = OSCACHESYSV_FAILURE;
PORT_ACCESS_FROM_PORT(_portLibrary);
Trc_SHR_OSC_detachRegion_Entry();
if (_shmhandle != NULL) {
Trc_SHR_OSC_detachRegion_Debug(_dataStart, _headerStart);
rc = j9shmem_detach(&_shmhandle);
if (rc == -1) {
LastErrorInfo lastErrorInfo;
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_DETACH, &lastErrorInfo);
} else {
rc = OSCACHESYSV_SUCCESS;
}
_dataStart = 0;
_headerStart = 0;
}
Trc_SHR_OSC_detachRegion_Exit();
return rc;
}
/**
* This is the deconstructor routine.
*
* It will remove any memory allocated by this class.
* However it will not remove any shared memory / mutex resources from the underlying OS.
* User who wishes to remove a shared memory region should use @ref SH_OSCache::destroy method
*/
void
SH_OSCachesysv::cleanup(void)
{
PORT_ACCESS_FROM_PORT(_portLibrary);
Trc_SHR_OSC_cleanup_Entry();
detachRegion();
/* now free the handles */
if (_shmhandle != NULL) {
j9shmem_close(&_shmhandle);
}
if (_semhandle != NULL) {
j9shsem_deprecated_close(&_semhandle);
}
commonCleanup();
#if !defined(WIN32)
if (_semFileName) {
j9mem_free_memory(_semFileName);
}
#endif /* !defined(WIN32) */
Trc_SHR_OSC_cleanup_Exit();
}
IDATA
SH_OSCachesysv::detach(void)
{
IDATA rc=OSCACHESYSV_FAILURE;
Trc_SHR_OSC_detach_Entry();
if (_shmhandle == NULL) {
Trc_SHR_OSC_detach_Exit1();
return OSCACHESYSV_SUCCESS;
}
Trc_SHR_OSC_detach_Debug(_cacheName, _dataStart);
_attach_count--;
if (_attach_count == 0) {
detachRegion();
rc=OSCACHESYSV_SUCCESS;
}
Trc_SHR_OSC_detach_Exit();
return rc;
}
/* The caller is responsible for locking the shared memory area */
IDATA
SH_OSCachesysv::initializeHeader(const char* cacheDirName, J9PortShcVersion* versionData, LastErrorInfo lastErrorInfo)
{
PORT_ACCESS_FROM_PORT(_portLibrary);
void* region;
OSCachesysv_header_version_current* myHeader;
U_32 dataLength;
U_32 readWriteBytes = (U_32)((_config->sharedClassReadWriteBytes > 0) ? _config->sharedClassReadWriteBytes : 0);
U_32 totalSize = getTotalSize();
U_32 softMaxSize = (U_32)_config->sharedClassSoftMaxBytes;
if (_config->sharedClassSoftMaxBytes < 0) {
softMaxSize = (U_32)-1;
} else if (softMaxSize > totalSize) {
Trc_SHR_OSC_Sysv_initializeHeader_softMaxBytesTooBig(totalSize);
softMaxSize = totalSize;
}
if (_cacheSize <= SHM_CACHEHEADERSIZE) {
errorHandler(J9NLS_SHRC_OSCACHE_TOOSMALL, &lastErrorInfo);
return OSCACHESYSV_FAILURE;
} else {
dataLength = SHM_CACHEDATASIZE(_cacheSize);
}
region = j9shmem_attach(_shmhandle, J9MEM_CATEGORY_CLASSES_SHC_CACHE);
if (region == NULL) {
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_ATTACH, &lastErrorInfo);
Trc_SHR_OSC_add_Exit1();
return OSCACHESYSV_FAILURE;
}
/* We can now setup the header */
_headerStart = region;
_dataStart = (void*)((UDATA)region + SHM_CACHEHEADERSIZE);
_dataLength = dataLength;
myHeader = (OSCachesysv_header_version_current*) region;
memset(myHeader, 0, SHM_CACHEHEADERSIZE);
memcpy(myHeader->eyecatcher, J9PORT_SHMEM_EYECATCHER, J9PORT_SHMEM_EYECATCHER_LENGTH);
initOSCacheHeader(&(myHeader->oscHdr), versionData, SHM_CACHEHEADERSIZE);
myHeader->attachedSemid = j9shsem_deprecated_getid(_semhandle);
/* If this is not being written into the default control file dir, mark it as such so that it cannot be auto-deleted */
myHeader->inDefaultControlDir = (cacheDirName == NULL);
/* jump over to the data area*/
region = SHM_DATASTARTFROMHEADER(myHeader);
if (_initializer != NULL) {
_initializer->init((char*)region, dataLength, (I_32)_config->sharedClassMinAOTSize, (I_32)_config->sharedClassMaxAOTSize, (I_32)_config->sharedClassMinJITSize, (I_32)_config->sharedClassMaxJITSize, readWriteBytes, softMaxSize);
}
if (J9_ARE_NO_BITS_SET(_runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE)) {
/* Do not set oscHdr.cacheInitComplete to 1 if the cache is to be restored */
myHeader->oscHdr.cacheInitComplete = 1;
}
/*ALL DONE */
return OSCACHESYSV_SUCCESS;
}
/**
* Attaches the shared memory into the process address space, and returns the address of the mapped
* shared memory.
*
* This method send a request to the operating system to map the shared memory into the caller's address
* space if it hasn't already been done so. This will also checks the memory region for the correct header
* if the region is being mapped for the first time.
*
* @param [in] expectedVersionData If not NULL, function checks the version data of the cache against the values in this struct
*
* @return The address of the memory mapped area for the caller's process - This is not guaranteed to be the same
* for two different process.
*/
void *
SH_OSCachesysv::attach(J9VMThread *currentThread, J9PortShcVersion* expectedVersionData)
{
J9JavaVM *vm = currentThread->javaVM;
PORT_ACCESS_FROM_PORT(_portLibrary);
IDATA headerRc;
Trc_SHR_OSC_attach_Entry();
if (_shmhandle == NULL) {
/* _shmhandle == NULL means previous op failed */
Trc_SHR_OSC_attach_Exit1();
return NULL;
}
if ((((_runtimeFlags & J9SHR_RUNTIMEFLAG_CREATE_OLD_GEN) == 0) && (_activeGeneration != getCurrentCacheGen())) ||
(((_runtimeFlags & J9SHR_RUNTIMEFLAG_CREATE_OLD_GEN) != 0) && (_activeGeneration != getCurrentCacheGen()-1))
) {
Trc_SHR_OSC_attach_ExitWrongGen();
return NULL;
}
/* Cache is opened attached, the call here will simply return the
* address memory already attached.
*
* Note: Unless ::detach was called ... which I believe does not currently occur.
*/
Trc_SHR_OSC_attach_Try_Attach1(UnitTest::unitTest);
void* request = j9shmem_attach(_shmhandle, J9MEM_CATEGORY_CLASSES_SHC_CACHE);
if (request == NULL) {
LastErrorInfo lastErrorInfo;
lastErrorInfo.lastErrorCode = j9error_last_error_number();
lastErrorInfo.lastErrorMsg = j9error_last_error_message();
errorHandler(J9NLS_SHRC_OSCACHE_SHMEM_ATTACH, &lastErrorInfo);
_dataStart = NULL;
_attach_count = 0;
Trc_SHR_OSC_attach_Exit2();
return NULL;
}
Trc_SHR_OSC_attach_Debug1(request);
Trc_SHR_OSC_attach_Debug2(sizeof(OSCachesysv_header_version_current));
_headerStart = request;