-
Notifications
You must be signed in to change notification settings - Fork 392
/
omrshsem_deprecated.c
1272 lines (1121 loc) · 47.8 KB
/
omrshsem_deprecated.c
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 2020
*
* 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 Port
* @brief Shared Semaphores
*/
#include <errno.h>
#include <stddef.h>
#include "omrportpriv.h"
#include "omrport.h"
#include "portnls.h"
#include "ut_omrport.h"
#include "omrsharedhelper.h"
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "omrshsem.h"
#include "omrshsemun.h"
#include "omrsysv_ipcwrappers.h"
/*These flags are only used internally*/
#define CREATE_ERROR -10
#define OPEN_ERROR -11
#define WRITE_ERROR -12
#define READ_ERROR -13
#define MALLOC_ERROR -14
#define OMRSH_NO_DATA -21
#define OMRSH_BAD_DATA -22
#define RETRY_COUNT 10
#define SLEEP_TIME 5
#define SEMMARKER_INITIALIZED 769
#define SHSEM_FTOK_PROJID 0x81
static intptr_t omrshsem_checkMarker(OMRPortLibrary *portLibrary, int semid, int semsetsize);
static intptr_t omrshsem_checkSize(OMRPortLibrary *portLibrary, int semid, int32_t numsems);
static intptr_t omrshsem_createSemaphore(struct OMRPortLibrary *portLibrary, intptr_t fd, BOOLEAN isReadOnlyFD, char *baseFile, int32_t setSize, omrshsem_baseFileFormat* controlinfo, uintptr_t groupPerm);
static intptr_t omrshsem_openSemaphore(struct OMRPortLibrary *portLibrary, intptr_t fd, char *baseFile, omrshsem_baseFileFormat* controlinfo, uintptr_t groupPerm, uintptr_t cacheFileType);
static void omrshsem_createSemHandle(struct OMRPortLibrary *portLibrary, int semid, int nsems, char* baseFile, omrshsem_handle * handle);
static intptr_t omrshsem_readOlderNonZeroByteControlFile(OMRPortLibrary *portLibrary, intptr_t fd, omrshsem_baseFileFormat * info);
static void omrshsem_initShsemStatsBuffer(OMRPortLibrary *portLibrary, OMRPortShsemStatistic *statbuf);
intptr_t
omrshsem_deprecated_openDeprecated (struct OMRPortLibrary *portLibrary, const char* cacheDirName, uintptr_t groupPerm, struct omrshsem_handle** handle, const char* semname, uintptr_t cacheFileType)
{
intptr_t retval = OMRPORT_ERROR_SHSEM_OPFAILED;
omrshsem_baseFileFormat controlinfo;
char baseFile[OMRSH_MAXPATH];
struct omrshsem_handle * tmphandle = NULL;
intptr_t baseFileLength = 0;
intptr_t handleStructLength = sizeof(struct omrshsem_handle);
intptr_t fd = -1;
intptr_t fileIsLocked = OMRSH_SUCCESS;
BOOLEAN isReadOnlyFD = FALSE;
union semun semctlArg;
struct semid_ds buf;
semctlArg.buf = &buf;
*handle = NULL;
Trc_PRT_shsem_omrshsem_openDeprecated_Entry();
omr_clearPortableError(portLibrary);
if (cacheDirName == NULL) {
Trc_PRT_shsem_omrshsem_deprecated_openDeprecated_ExitNullCacheDirName();
goto error;
}
omrstr_printf(portLibrary, baseFile, OMRSH_MAXPATH, "%s%s", cacheDirName, semname);
baseFileLength = strlen(baseFile) + 1;
tmphandle = omrmem_allocate_memory(portLibrary, (handleStructLength + baseFileLength), OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == tmphandle) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: could not alloc handle.");
goto error;
}
tmphandle->baseFile = (char *) (((char *) tmphandle) + handleStructLength);
fileIsLocked = omr_ControlFileOpenWithWriteLock(portLibrary, &fd, &isReadOnlyFD, FALSE, baseFile, 0);
if (fileIsLocked == OMRSH_FILE_DOES_NOT_EXIST) {
/* This will occur if the shared memory control files, exists but the semaphore file does not.
*
* In this case OMRPORT_INFO_SHSEM_PARTIAL is returned to suggest the
* caller try doing there work without a lock (if it can).
*/
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: control file does not exist.");
retval = OMRPORT_INFO_SHSEM_PARTIAL;
goto error;
} else if ( fileIsLocked != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: could not lock semaphore control file.");
goto error;
}
if (cacheFileType == OMRSH_SYSV_OLDER_EMPTY_CONTROL_FILE) {
int semid = -1;
int projid = 0xad;
key_t fkey;
int semflags_open = groupPerm ? OMRSHSEM_SEMFLAGS_GROUP_OPEN : OMRSHSEM_SEMFLAGS_OPEN;
fkey = omr_ftokWrapper(portLibrary, baseFile, projid);
if (fkey == -1) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: ftok failed.");
goto error;
}
/*Check that the semaphore is still on the system.*/
semid = omr_semgetWrapper(portLibrary, fkey, 0, semflags_open);
if (-1 == semid) {
int32_t lasterrno = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
const char * errormsg = omrerror_last_error_message(portLibrary);
Trc_PRT_shsem_omrshsem_openDeprecated_semgetFailed(fkey, lasterrno, errormsg);
if (lasterrno == OMRPORT_ERROR_SYSV_IPC_ERRNO_EINVAL) {
Trc_PRT_shsem_omrshsem_openDeprecated_semgetEINVAL(fkey);
retval = OMRPORT_INFO_SHSEM_PARTIAL;
/*Clean up the control file since its SysV object is deleted.*/
omrfile_unlink(portLibrary, baseFile);
}
goto error;
}
/*get buf.sem_nsems size for omrshsem_createSemHandle()*/
if (omr_semctlWrapper(portLibrary, TRUE, semid, 0, IPC_STAT, semctlArg) == -1) {
int32_t lasterrno = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
const char * errormsg = omrerror_last_error_message(portLibrary);
Trc_PRT_shsem_omrshsem_openDeprecated_semctlFailed(semid, lasterrno, errormsg);
goto error;
}
omrshsem_createSemHandle(portLibrary, semid, buf.sem_nsems, baseFile, tmphandle);
tmphandle->timestamp = omrfile_lastmod(portLibrary, baseFile);
retval = OMRPORT_INFO_SHSEM_OPENED;
} else if (cacheFileType == OMRSH_SYSV_OLDER_CONTROL_FILE) {
if (omrshsem_readOlderNonZeroByteControlFile(portLibrary, fd, &controlinfo) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: could not read deprecated control file.");
goto error;
}
retval = omrshsem_openSemaphore(portLibrary, fd, baseFile, &controlinfo, groupPerm, cacheFileType);
if (OMRPORT_INFO_SHSEM_OPENED != retval) {
if (OMRPORT_ERROR_SHSEM_OPFAILED_DONT_UNLINK == retval) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Control file was not unlinked.");
retval = OMRPORT_ERROR_SHSEM_OPFAILED;
} else {
/* Clean up the control file */
if (-1 == omrfile_unlink(portLibrary, baseFile)) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Control file could not be unlinked.");
} else {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Control file was unlinked.");
}
retval = OMRPORT_INFO_SHSEM_PARTIAL;
}
goto error;
}
omrshsem_createSemHandle(portLibrary, controlinfo.semid, controlinfo.semsetSize, baseFile, tmphandle);
tmphandle->timestamp = omrfile_lastmod(portLibrary, baseFile);
retval = OMRPORT_INFO_SHSEM_OPENED;
} else {
Trc_PRT_shsem_omrshsem_openDeprecated_BadCacheType(cacheFileType);
}
error:
if (fileIsLocked == OMRSH_SUCCESS) {
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_openDeprecated_Message("Error: could not unlock semaphore control file.");
retval = OMRPORT_ERROR_SHSEM_OPFAILED;
}
}
if (OMRPORT_INFO_SHSEM_OPENED != retval){
if (NULL != tmphandle) {
omrmem_free_memory(portLibrary, tmphandle);
}
*handle = NULL;
Trc_PRT_shsem_omrshsem_openDeprecated_Exit("Exit: failed to open older semaphore");
} else {
*handle = tmphandle;
Trc_PRT_shsem_omrshsem_openDeprecated_Exit("Opened shared semaphore.");
}
return retval;
}
intptr_t
omrshsem_deprecated_open (struct OMRPortLibrary *portLibrary, const char* cacheDirName, uintptr_t groupPerm, struct omrshsem_handle **handle, const char *semname, int setSize, int permission, uintptr_t flags, OMRControlFileStatus *controlFileStatus)
{
/* TODO: needs to be longer? dynamic?*/
char baseFile[OMRSH_MAXPATH];
intptr_t fd;
omrshsem_baseFileFormat controlinfo;
int32_t rc;
BOOLEAN isReadOnlyFD;
intptr_t baseFileLength = 0;
intptr_t handleStructLength = sizeof(struct omrshsem_handle);
char * exitmsg = 0;
intptr_t retryIfReadOnlyCount = 10;
struct omrshsem_handle * tmphandle = NULL;
BOOLEAN freeHandleOnError = TRUE;
Trc_PRT_shsem_omrshsem_open_Entry(semname, setSize, permission);
omr_clearPortableError(portLibrary);
if (cacheDirName == NULL) {
Trc_PRT_shsem_omrshsem_deprecated_open_ExitNullCacheDirName();
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
omrstr_printf(portLibrary, baseFile, OMRSH_MAXPATH, "%s%s", cacheDirName, semname);
Trc_PRT_shsem_omrshsem_open_Debug1(baseFile);
baseFileLength = strlen(baseFile) + 1;
*handle = NULL;
tmphandle = omrmem_allocate_memory(portLibrary, (handleStructLength + baseFileLength), OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == tmphandle) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Error: could not alloc handle.");
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
tmphandle->baseFile = (char *) (((char *) tmphandle) + handleStructLength);
if (NULL != controlFileStatus) {
memset(controlFileStatus, 0, sizeof(OMRControlFileStatus));
}
for (retryIfReadOnlyCount = 10; retryIfReadOnlyCount > 0; retryIfReadOnlyCount -= 1) {
/*Open control file with write lock*/
BOOLEAN canCreateFile = TRUE;
if (OMR_ARE_ANY_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_FOR_DESTROY | OMRSHSEM_OPEN_DO_NOT_CREATE)) {
canCreateFile = FALSE;
}
if (omr_ControlFileOpenWithWriteLock(portLibrary, &fd, &isReadOnlyFD, canCreateFile, baseFile, groupPerm) != OMRSH_SUCCESS) {
omrmem_free_memory(portLibrary, tmphandle);
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Error: could not lock semaphore control file.");
if (OMR_ARE_ANY_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_FOR_DESTROY | OMRSHSEM_OPEN_DO_NOT_CREATE)) {
return OMRPORT_INFO_SHSEM_PARTIAL;
}
return OMRPORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_LOCK_FAILED;
}
/*Try to read data from the file*/
rc = omrfile_read(portLibrary, fd, &controlinfo, sizeof(omrshsem_baseFileFormat));
if (rc == -1) {
/* The file is empty.*/
if (OMR_ARE_NO_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_FOR_DESTROY | OMRSHSEM_OPEN_DO_NOT_CREATE)) {
rc = omrshsem_createSemaphore(portLibrary, fd, isReadOnlyFD, baseFile, setSize, &controlinfo, groupPerm);
if (rc == OMRPORT_ERROR_SHSEM_OPFAILED) {
exitmsg = "Error: create of the semaphore has failed.";
if (isReadOnlyFD == FALSE) {
if (omr_unlinkControlFile(portLibrary, baseFile, controlFileStatus)) {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is unlinked after call to omrshsem_createSemaphore.");
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Failed to unlink control file after call to omrshsem_createSemaphore.");
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is not unlinked after call to omrshsem_createSemaphore(isReadOnlyFD == TRUE).");
}
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file cannot be read. Do not attempt to create semaphore as OMRSHSEM_OPEN_FOR_STATS, OMRSHSEM_OPEN_FOR_DESTROY or OMRSHSEM_OPEN_DO_NOT_CREATE is set");
rc = OMRPORT_ERROR_SHSEM_OPFAILED;
}
} else if (rc == sizeof(omrshsem_baseFileFormat)) {
/*The control file contains data, and we have successfully read in our structure*/
/*Note: there is no unlink here if the file is read-only ... the next function call manages this behaviour*/
rc = omrshsem_openSemaphore(portLibrary, fd, baseFile, &controlinfo, groupPerm, OMRSH_SYSV_REGULAR_CONTROL_FILE);
if (OMRPORT_INFO_SHSEM_OPENED != rc) {
exitmsg = "Error: open of the semaphore has failed.";
if ((OMRPORT_ERROR_SHSEM_OPFAILED_DONT_UNLINK != rc)
&& OMR_ARE_NO_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_DO_NOT_CREATE)
) {
/* PR 74306: If the control file is read-only, do not unlink it unless the control file was created by an older JVM without this fix,
* which is identified by the check for major and minor level in the control file.
* Reason being if the control file is read-only, JVM acquires a shared lock in omr_ControlFileOpenWithWriteLock().
* This implies in multiple JVM scenario it is possible that when one JVM has unlinked and created a new control file,
* another JVM unlinks the newly created control file, and creates another control file.
* For shared class cache, this may result into situations where the two JVMs, accessing the same cache,
* would create two different semaphore sets for same shared memory region causing one of them to detect cache as corrupt
* due to semid mismatch.
*/
if ((FALSE == isReadOnlyFD)
|| ((OMRSH_GET_MOD_MAJOR_LEVEL(controlinfo.modlevel) == OMRSH_MOD_MAJOR_LEVEL_ALLOW_READONLY_UNLINK)
&& (OMRSH_SEM_GET_MOD_MINOR_LEVEL(controlinfo.modlevel) <= OMRSH_MOD_MINOR_LEVEL_ALLOW_READONLY_UNLINK)
&& (OMRPORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND == rc))
) {
if (omr_unlinkControlFile(portLibrary, baseFile, controlFileStatus)) {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is unlinked after call to omrshsem_openSemaphore.");
if (OMR_ARE_ALL_BITS_SET(flags, OMRSHSEM_OPEN_FOR_DESTROY)) {
if (OMRPORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND != rc) {
tmphandle->semid = controlinfo.semid;
freeHandleOnError = FALSE;
}
} else if (retryIfReadOnlyCount > 1) {
rc = OMRPORT_INFO_SHSEM_OPEN_UNLINKED;
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Failed to unlink control file after call to openSemaphoe.");
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is not unlinked after omrshsem_openSemaphore because it is read-only and (its modLevel does not allow unlinking or rc != SEMAPHORE_NOT_FOUND).");
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is not unlinked after call to omrshsem_openSemaphore.");
}
}
} else {
/*Should never get here ... this means the control file was corrupted*/
exitmsg = "Error: Semaphore control file is corrupted.";
rc = OMRPORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_CORRUPT;
if (OMR_ARE_NO_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_DO_NOT_CREATE)) {
if (isReadOnlyFD == FALSE) {
if (omr_unlinkControlFile(portLibrary, baseFile, controlFileStatus)) {
Trc_PRT_shsem_omrshsem_open_Msg("Info: The corrupt control file was unlinked.");
if (retryIfReadOnlyCount > 1) {
rc = OMRPORT_INFO_SHSEM_OPEN_UNLINKED;
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Failed to remove the corrupt control file.");
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Corrupt control file is not unlinked (isReadOnlyFD == TRUE).");
}
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Info: Control file is found to be corrupt but is not unlinked (OMRSHSEM_OPEN_FOR_STATS or OMRSHSEM_OPEN_DO_NOT_CREATE is set).");
}
}
if ((OMRPORT_INFO_SHSEM_CREATED != rc) && (OMRPORT_INFO_SHSEM_OPENED != rc)) {
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Error: could not unlock semaphore control file.");
omrmem_free_memory(portLibrary, tmphandle);
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
if (OMR_ARE_ANY_BITS_SET(flags, OMRSHSEM_OPEN_FOR_STATS | OMRSHSEM_OPEN_DO_NOT_CREATE)) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg(exitmsg);
omrmem_free_memory(portLibrary, tmphandle);
return OMRPORT_INFO_SHSEM_PARTIAL;
}
if (OMR_ARE_ALL_BITS_SET(flags, OMRSHSEM_OPEN_FOR_DESTROY)) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg(exitmsg);
/* If we get an error while opening semaphore and thereafter unlinked control file successfully,
* then we may have stored the semid in tmphandle to be used by caller for error reporting.
* Do not free tmphandle in such case. Caller is responsible for freeing it.
*/
if (TRUE == freeHandleOnError) {
omrmem_free_memory(portLibrary, tmphandle);
}
if (OMRPORT_ERROR_SHSEM_OPFAILED_DONT_UNLINK == rc) {
rc = OMRPORT_ERROR_SHSEM_OPFAILED;
}
return rc;
}
if (((isReadOnlyFD == TRUE) || (rc == OMRPORT_INFO_SHSEM_OPEN_UNLINKED)) && (retryIfReadOnlyCount > 1)) {
/*Try loop again ... we sleep first to mimic old retry loop*/
if (rc != OMRPORT_INFO_SHSEM_OPEN_UNLINKED) {
Trc_PRT_shsem_omrshsem_open_Msg("Retry with usleep(100).");
usleep(100);
} else {
Trc_PRT_shsem_omrshsem_open_Msg("Retry.");
}
continue;
} else {
if (retryIfReadOnlyCount <= 1) {
Trc_PRT_shsem_omrshsem_open_Msg("Info no more retries.");
}
Trc_PRT_shsem_omrshsem_open_ExitWithMsg(exitmsg);
omrmem_free_memory(portLibrary, tmphandle);
if (OMRPORT_ERROR_SHSEM_OPFAILED_DONT_UNLINK == rc) {
rc = OMRPORT_ERROR_SHSEM_OPFAILED;
}
/* Here 'rc' can be:
* - OMRPORT_ERROR_SHSEM_OPFAILED (returned either by omrshsem_createSemaphore or omrshsem_openSemaphore)
* - OMRPORT_ERROR_SHSEM_OPFAILED_CONTROL_FILE_CORRUPT
* - OMRPORT_ERROR_SHSEM_OPFAILED_SEMID_MISMATCH (returned by omrshsem_openSemaphore)
* - OMRPORT_ERROR_SHSEM_OPFAILED_SEM_KEY_MISMATCH (returned by omrshsem_openSemaphore)
* - OMRPORT_ERROR_SHSEM_OPFAILED_SEM_SIZE_CHECK_FAILED (returned by omrshsem_openSemaphore)
* - OMRPORT_ERROR_SHSEM_OPFAILED_SEM_MARKER_CHECK_FAILED (returned by omrshsem_openSemaphore)
* - OMRPORT_ERROR_SHSEM_OPFAILED_SEMAPHORE_NOT_FOUND ( returned by omrshsem_openSemaphore)
*/
return rc;
}
}
/*If we hit here we break out of the loop*/
break;
}
omrshsem_createSemHandle(portLibrary, controlinfo.semid, controlinfo.semsetSize, baseFile, tmphandle);
if (rc == OMRPORT_INFO_SHSEM_CREATED) {
tmphandle->timestamp = omrfile_lastmod(portLibrary, baseFile);
}
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Error: could not unlock semaphore control file.");
omrmem_free_memory(portLibrary, tmphandle);
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
if (rc == OMRPORT_INFO_SHSEM_CREATED) {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Successfully created a new semaphore.");
} else {
Trc_PRT_shsem_omrshsem_open_ExitWithMsg("Successfully opened an existing semaphore.");
}
*handle = tmphandle;
return rc;
}
intptr_t
omrshsem_deprecated_post(struct OMRPortLibrary *portLibrary, struct omrshsem_handle* handle, uintptr_t semset, uintptr_t flag)
{
struct sembuf buffer;
intptr_t rc;
Trc_PRT_shsem_omrshsem_post_Entry1(handle, semset, flag, handle ? handle->semid : -1);
if(handle == NULL) {
Trc_PRT_shsem_omrshsem_post_Exit1();
return OMRPORT_ERROR_SHSEM_HANDLE_INVALID;
}
if(semset >= handle->nsems) {
Trc_PRT_shsem_omrshsem_post_Exit2();
return OMRPORT_ERROR_SHSEM_SEMSET_INVALID;
}
buffer.sem_num = semset;
buffer.sem_op = 1; /* post */
if(flag & OMRPORT_SHSEM_MODE_UNDO) {
buffer.sem_flg = SEM_UNDO;
} else {
buffer.sem_flg = 0;
}
rc = omr_semopWrapper(portLibrary,handle->semid, &buffer, 1);
if (-1 == rc) {
int32_t myerrno = omrerror_last_error_number(portLibrary);
Trc_PRT_shsem_omrshsem_post_Exit3(rc, myerrno);
} else {
Trc_PRT_shsem_omrshsem_post_Exit(rc);
}
return rc;
}
intptr_t
omrshsem_deprecated_wait(struct OMRPortLibrary *portLibrary, struct omrshsem_handle* handle, uintptr_t semset, uintptr_t flag)
{
struct sembuf buffer;
intptr_t rc;
Trc_PRT_shsem_omrshsem_wait_Entry1(handle, semset, flag, handle ? handle->semid : -1);
if(handle == NULL) {
Trc_PRT_shsem_omrshsem_wait_Exit1();
return OMRPORT_ERROR_SHSEM_HANDLE_INVALID;
}
if(semset >= handle->nsems) {
Trc_PRT_shsem_omrshsem_wait_Exit2();
return OMRPORT_ERROR_SHSEM_SEMSET_INVALID;
}
buffer.sem_num = semset;
buffer.sem_op = -1; /* wait */
if( flag & OMRPORT_SHSEM_MODE_UNDO ) {
buffer.sem_flg = SEM_UNDO;
} else {
buffer.sem_flg = 0;
}
if( flag & OMRPORT_SHSEM_MODE_NOWAIT ) {
buffer.sem_flg = buffer.sem_flg | IPC_NOWAIT;
}
rc = omr_semopWrapper(portLibrary,handle->semid, &buffer, 1);
if (-1 == rc) {
int32_t myerrno = omrerror_last_error_number(portLibrary);
Trc_PRT_shsem_omrshsem_wait_Exit3(rc, myerrno);
} else {
Trc_PRT_shsem_omrshsem_wait_Exit(rc);
}
return rc;
}
intptr_t
omrshsem_deprecated_getVal(struct OMRPortLibrary *portLibrary, struct omrshsem_handle* handle, uintptr_t semset)
{
intptr_t rc;
Trc_PRT_shsem_omrshsem_getVal_Entry1(handle, semset, handle ? handle->semid : -1);
if(handle == NULL) {
Trc_PRT_shsem_omrshsem_getVal_Exit1();
return OMRPORT_ERROR_SHSEM_HANDLE_INVALID;
}
if(semset >= handle->nsems) {
Trc_PRT_shsem_omrshsem_getVal_Exit2();
return OMRPORT_ERROR_SHSEM_SEMSET_INVALID;
}
rc = omr_semctlWrapper(portLibrary, TRUE, handle->semid, semset, GETVAL);
if (-1 == rc) {
int32_t myerrno = omrerror_last_error_number(portLibrary);
Trc_PRT_shsem_omrshsem_getVal_Exit3(rc, myerrno);
} else {
Trc_PRT_shsem_omrshsem_getVal_Exit(rc);
}
return rc;
}
intptr_t
omrshsem_deprecated_setVal(struct OMRPortLibrary *portLibrary, struct omrshsem_handle* handle, uintptr_t semset, intptr_t value)
{
semctlUnion sem_union;
intptr_t rc;
Trc_PRT_shsem_omrshsem_setVal_Entry1(handle, semset, value, handle ? handle->semid : -1);
if(handle == NULL) {
Trc_PRT_shsem_omrshsem_setVal_Exit1();
return OMRPORT_ERROR_SHSEM_HANDLE_INVALID;
}
if(semset >= handle->nsems) {
Trc_PRT_shsem_omrshsem_setVal_Exit2();
return OMRPORT_ERROR_SHSEM_SEMSET_INVALID;
}
sem_union.val = value;
rc = omr_semctlWrapper(portLibrary, TRUE, handle->semid, semset, SETVAL, sem_union);
if (-1 == rc) {
int32_t myerrno = omrerror_last_error_number(portLibrary);
Trc_PRT_shsem_omrshsem_setVal_Exit3(rc, myerrno);
} else {
Trc_PRT_shsem_omrshsem_setVal_Exit(rc);
}
return rc;
}
intptr_t
omrshsem_deprecated_handle_stat(struct OMRPortLibrary *portLibrary, struct omrshsem_handle *handle, struct OMRPortShsemStatistic *statbuf)
{
intptr_t rc = OMRPORT_ERROR_SHSEM_STAT_FAILED;
struct semid_ds seminfo;
union semun semctlArg;
Trc_PRT_shsem_omrshsem_deprecated_handle_stat_Entry1(handle, handle ? handle->semid : -1);
omr_clearPortableError(portLibrary);
if (NULL == handle) {
Trc_PRT_shsem_omrshsem_deprecated_handle_stat_ErrorNullHandle();
rc = OMRPORT_ERROR_SHSEM_HANDLE_INVALID;
goto _end;
}
if (NULL == statbuf) {
Trc_PRT_shsem_omrshsem_deprecated_handle_stat_ErrorNullBuffer();
rc = OMRPORT_ERROR_SHSEM_STAT_BUFFER_INVALID;
goto _end;
} else {
omrshsem_initShsemStatsBuffer(portLibrary, statbuf);
}
semctlArg.buf = &seminfo;
rc = omr_semctlWrapper(portLibrary, TRUE, handle->semid, 0, IPC_STAT, semctlArg);
if (OMRSH_FAILED == rc) {
int32_t lasterrno = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
const char *errormsg = omrerror_last_error_message(portLibrary);
Trc_PRT_shsem_omrshsem_deprecated_handle_stat_semctlFailed(handle->semid, lasterrno, errormsg);
rc = OMRPORT_ERROR_SHSEM_STAT_FAILED;
goto _end;
}
statbuf->semid = handle->semid;
statbuf->ouid = seminfo.sem_perm.uid;
statbuf->ogid = seminfo.sem_perm.gid;
statbuf->cuid = seminfo.sem_perm.cuid;
statbuf->cgid = seminfo.sem_perm.cgid;
statbuf->lastOpTime = seminfo.sem_otime;
statbuf->lastChangeTime = seminfo.sem_ctime;
statbuf->nsems = seminfo.sem_nsems;
/* Same code as in j9file_stat(). Probably a new function to translate operating system modes to J9 modes is required. */
if (seminfo.sem_perm.mode & S_IWUSR) {
statbuf->perm.isUserWriteable = 1;
}
if (seminfo.sem_perm.mode & S_IRUSR) {
statbuf->perm.isUserReadable = 1;
}
if (seminfo.sem_perm.mode & S_IWGRP) {
statbuf->perm.isGroupWriteable = 1;
}
if (seminfo.sem_perm.mode & S_IRGRP) {
statbuf->perm.isGroupReadable = 1;
}
if (seminfo.sem_perm.mode & S_IWOTH) {
statbuf->perm.isOtherWriteable = 1;
}
if (seminfo.sem_perm.mode & S_IROTH) {
statbuf->perm.isOtherReadable = 1;
}
rc = OMRPORT_INFO_SHSEM_STAT_PASSED;
_end:
Trc_PRT_shsem_omrshsem_deprecated_handle_stat_Exit(rc);
return rc;
}
void
omrshsem_deprecated_close (struct OMRPortLibrary *portLibrary, struct omrshsem_handle **handle)
{
Trc_PRT_shsem_omrshsem_close_Entry1(*handle, (*handle) ? (*handle)->semid : -1);
/* On Unix you don't need to close the semaphore handles*/
if (NULL == *handle) {
Trc_PRT_shsem_omrshsem_close_ExitNullHandle();
return;
}
omrmem_free_memory(portLibrary, *handle);
*handle=NULL;
Trc_PRT_shsem_omrshsem_close_Exit();
}
intptr_t
omrshsem_deprecated_destroy (struct OMRPortLibrary *portLibrary, struct omrshsem_handle **handle)
{
/*pre: user has not closed the handle, and assume user has perm to remove */
intptr_t rc = -1;
intptr_t lockFile, rcunlink;
intptr_t fd;
BOOLEAN isReadOnlyFD;
omrshsem_baseFileFormat controlinfo;
Trc_PRT_shsem_omrshsem_destroy_Entry1(*handle, (*handle) ? (*handle)->semid : -1);
if (*handle == NULL) {
Trc_PRT_shsem_omrshsem_destroy_ExitNullHandle();
return 0;
}
lockFile = omr_ControlFileOpenWithWriteLock(portLibrary, &fd, &isReadOnlyFD, FALSE, (*handle)->baseFile, 0);
if (lockFile == OMRSH_FILE_DOES_NOT_EXIST) {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: control file not found");
if (omr_semctlWrapper(portLibrary, TRUE, (*handle)->semid, 0, IPC_RMID, 0) == -1) {
int32_t lastError = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
if ((OMRPORT_ERROR_SYSV_IPC_ERRNO_EINVAL == lastError) || (OMRPORT_ERROR_SYSV_IPC_ERRNO_EIDRM == lastError)) {
Trc_PRT_shsem_omrshsem_destroy_Msg("SysV obj is already deleted");
rc = 0;
} else {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: could not delete SysV obj");
rc = -1;
}
} else {
Trc_PRT_shsem_omrshsem_destroy_Msg("Deleted SysV obj");
rc = 0;
}
goto omrshsem_deprecated_destroy_exitWithRC;
} else if (lockFile != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: could not open and lock control file");
goto omrshsem_deprecated_destroy_exitWithError;
}
/*Try to read data from the file*/
rc = omrfile_read(portLibrary, fd, &controlinfo, sizeof(omrshsem_baseFileFormat));
if (rc != sizeof(omrshsem_baseFileFormat)) {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: can not read control file");
goto omrshsem_deprecated_destroy_exitWithErrorAndUnlock;
}
/* check that the modlevel and version is okay */
if (OMRSH_GET_MOD_MAJOR_LEVEL(controlinfo.modlevel) != OMRSH_MOD_MAJOR_LEVEL) {
Trc_PRT_shsem_omrshsem_destroy_BadMajorModlevel(controlinfo.modlevel, OMRSH_MODLEVEL);
goto omrshsem_deprecated_destroy_exitWithErrorAndUnlock;
}
if (controlinfo.semid == (*handle)->semid) {
if (omr_semctlWrapper(portLibrary, TRUE, (*handle)->semid, 0, IPC_RMID, 0) == -1) {
Trc_PRT_shsem_omrshsem_destroy_Debug2((*handle)->semid, omrerror_last_error_number(portLibrary));
rc = -1;
} else {
rc = 0;
}
}
if (0 == rc) {
/* PR 74306: Allow unlinking of readonly control file if it was created by an older JVM without this fix
* which is identified by different modlevel.
*/
if ((FALSE == isReadOnlyFD)
|| ((OMRSH_GET_MOD_MAJOR_LEVEL(controlinfo.modlevel) == OMRSH_MOD_MAJOR_LEVEL_ALLOW_READONLY_UNLINK)
&& (OMRSH_SEM_GET_MOD_MINOR_LEVEL(controlinfo.modlevel) <= OMRSH_MOD_MINOR_LEVEL_ALLOW_READONLY_UNLINK))
) {
rcunlink = omrfile_unlink(portLibrary, (*handle)->baseFile);
Trc_PRT_shsem_omrshsem_destroy_Debug1((*handle)->baseFile, rcunlink, omrerror_last_error_number(portLibrary));
}
}
omrshsem_deprecated_close(portLibrary, handle);
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: failed to unlock control file.");
goto omrshsem_deprecated_destroy_exitWithError;
}
if (0 == rc) {
Trc_PRT_shsem_omrshsem_destroy_Exit();
} else {
Trc_PRT_shsem_omrshsem_destroy_Exit1();
}
omrshsem_deprecated_destroy_exitWithRC:
return rc;
omrshsem_deprecated_destroy_exitWithErrorAndUnlock:
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_destroy_Msg("Error: failed to unlock control file (after version check fail).");
}
omrshsem_deprecated_destroy_exitWithError:
Trc_PRT_shsem_omrshsem_destroy_Exit1();
return -1;
}
intptr_t
omrshsem_deprecated_destroyDeprecated (struct OMRPortLibrary *portLibrary, struct omrshsem_handle **handle, uintptr_t cacheFileType)
{
intptr_t retval = -1;
intptr_t fd;
BOOLEAN isReadOnlyFD = FALSE;
BOOLEAN fileIsLocked = FALSE;
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Entry(*handle, (*handle)->semid);
if (cacheFileType == OMRSH_SYSV_OLDER_EMPTY_CONTROL_FILE) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Message("Info: cacheFileType == OMRSH_SYSV_OLDER_EMPTY_CONTROL_FILE.");
if (omr_ControlFileOpenWithWriteLock(portLibrary, &fd, &isReadOnlyFD, FALSE, (*handle)->baseFile, 0) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_ExitWithMessage("Error: could not lock semaphore control file.");
goto error;
} else {
fileIsLocked = TRUE;
}
if (omr_semctlWrapper(portLibrary, TRUE, (*handle)->semid, 0, IPC_RMID, 0) == -1) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_ExitWithMessage("Error: failed to remove SysV object.");
goto error;
}
if (0 == omrfile_unlink(portLibrary, (*handle)->baseFile)) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Message("Unlinked control file");
} else {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Message("Failed to unlink control file");
}
omrshsem_deprecated_close(portLibrary, handle);
retval = 0;
error:
if (fileIsLocked == TRUE) {
if (omr_ControlFileCloseAndUnLock(portLibrary, fd) != OMRSH_SUCCESS) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Message("Error: could not unlock semaphore control file.");
retval = -1;
}
}
} else if (cacheFileType == OMRSH_SYSV_OLDER_CONTROL_FILE) {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_Message("Info: cacheFileType == OMRSH_SYSV_OLDER_CONTROL_FILE.");
retval = omrshsem_deprecated_destroy(portLibrary, handle);
} else {
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_BadCacheType(cacheFileType);
}
Trc_PRT_shsem_omrshsem_deprecated_destroyDeprecated_ExitWithMessage("Exit");
return retval;
}
int32_t
omrshsem_deprecated_startup(struct OMRPortLibrary *portLibrary)
{
return 0;
}
void
omrshsem_deprecated_shutdown(struct OMRPortLibrary *portLibrary)
{
/* Don't need to do anything for now, but maybe we will need to clean up
* some directories, etc over here.
*/
}
static intptr_t
omrshsem_readOlderNonZeroByteControlFile(OMRPortLibrary *portLibrary, intptr_t fd, omrshsem_baseFileFormat * info)
{
intptr_t rc;
rc = omrfile_read(portLibrary, fd, info, sizeof(omrshsem_baseFileFormat));
if (rc < 0) {
return OMRSH_FAILED;/*READ_ERROR;*/
} else if (rc == 0) {
return OMRSH_FAILED;/*OMRSH_NO_DATA;*/
} else if (rc < sizeof(omrshsem_baseFileFormat)) {
return OMRSH_FAILED;/*OMRSH_BAD_DATA;*/
}
return OMRSH_SUCCESS;
}
/**
* @internal
* Create a new SysV semaphore
*
* @param[in] portLibrary
* @param[in] fd read/write file descriptor
* @param[in] isReadOnlyFD set to TRUE if control file is read-only, FALSE otherwise
* @param[in] baseFile filename matching fd
* @param[in] setSize size of semaphore set
* @param[out] controlinfo information about the new sysv object
* @param[in] groupPerm group permission of the semaphore
*
* @return OMRPORT_INFO_SHSEM_CREATED on success
*/
static intptr_t
omrshsem_createSemaphore(struct OMRPortLibrary *portLibrary, intptr_t fd, BOOLEAN isReadOnlyFD, char *baseFile, int32_t setSize, omrshsem_baseFileFormat* controlinfo, uintptr_t groupPerm)
{
union semun sem_union;
intptr_t i = 0;
intptr_t j = 0;
intptr_t projId = SHSEM_FTOK_PROJID;
intptr_t maxProjIdRetries = 20;
int semflags = groupPerm ? OMRSHSEM_SEMFLAGS_GROUP : OMRSHSEM_SEMFLAGS;
Trc_PRT_shsem_omrshsem_createsemaphore_EnterWithMessage("start");
if (isReadOnlyFD == TRUE) {
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: can not write control file b/c file descriptor is opened read only.");
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
if (controlinfo == NULL) {
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: controlinfo == NULL");
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
for (i = 0; i < maxProjIdRetries; i += 1, projId += 1) {
int semid = -1;
key_t fkey = -1;
intptr_t rc = 0;
fkey = omr_ftokWrapper(portLibrary, baseFile, projId);
if (-1 == fkey) {
int32_t lastError = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
if (lastError == OMRPORT_ERROR_SYSV_IPC_ERRNO_ENOENT) {
Trc_PRT_shsem_omrshsem_createsemaphore_Msg("ftok: ENOENT");
} else if (lastError == OMRPORT_ERROR_SYSV_IPC_ERRNO_EACCES) {
Trc_PRT_shsem_omrshsem_createsemaphore_Msg("ftok: EACCES");
} else {
/*By this point other errno's should not happen*/
Trc_PRT_shsem_omrshsem_createsemaphore_MsgWithError("ftok: unexpected errno, portable errorCode = ", lastError);
}
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: fkey == -1");
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
semid = omr_semgetWrapper(portLibrary, fkey, setSize + 1, semflags);
if (-1 == semid) {
int32_t lastError = omrerror_last_error_number(portLibrary) | OMRPORT_ERROR_SYSTEM_CALL_ERRNO_MASK;
if (lastError == OMRPORT_ERROR_SYSV_IPC_ERRNO_EEXIST || lastError == OMRPORT_ERROR_SYSV_IPC_ERRNO_EACCES) {
Trc_PRT_shsem_omrshsem_createsemaphore_Msg("Retry (errno == EEXIST || errno == EACCES) was found.");
continue;
}
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessageAndError("Error: semid = -1 with portable errorCode = ", lastError);
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
controlinfo->version = OMRSH_VERSION;
controlinfo->modlevel = OMRSH_MODLEVEL;
controlinfo->proj_id = projId;
controlinfo->ftok_key = fkey;
controlinfo->semid = semid;
controlinfo->creator_pid = omrsysinfo_get_pid(portLibrary);
controlinfo->semsetSize = setSize;
if (-1 == omrfile_seek(portLibrary, fd, 0, EsSeekSet)) {
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: could not seek to start of control file");
/* Do not set error code if semctl fails, else j9file_seek() error code will be lost. */
if (omr_semctlWrapper(portLibrary, FALSE, semid, 0, IPC_RMID, 0) == -1) {
/*Already in an error condition ... don't worry about failure here*/
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
rc = omrfile_write(portLibrary, fd, controlinfo, sizeof(omrshsem_baseFileFormat));
if (rc != sizeof(omrshsem_baseFileFormat)) {
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: write to control file failed");
/* Do not set error code if semctl fails, else j9file_write() error code will be lost. */
if (omr_semctlWrapper(portLibrary, FALSE, semid, 0, IPC_RMID, 0) == -1) {
/*Already in an error condition ... don't worry about failure here*/
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
for (j=0; j<setSize; j++) {
struct omrshsem_handle tmphandle;
tmphandle.semid=semid;
tmphandle.nsems = setSize;
tmphandle.baseFile = baseFile;
if (portLibrary->shsem_deprecated_post(portLibrary, &tmphandle, j, OMRPORT_SHSEM_MODE_DEFAULT) != 0) {
/* The code should never ever fail here b/c we just created, and obviously own the new semaphore.
* If semctl call below fails we are already in an error condition, so
* don't worry about failure here.
* Do not set error code if semctl fails, else omrshsem_deprecated_post() error code will be lost.
*/
omr_semctlWrapper(portLibrary, FALSE, semid, 0, IPC_RMID, 0);
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
}
sem_union.val = SEMMARKER_INITIALIZED;
if (omr_semctlWrapper(portLibrary, TRUE, semid, setSize, SETVAL, sem_union) == -1) {
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Could not mark semaphore as initialized initialized.");
/* The code should never ever fail here b/c we just created, and obviously own the new semaphore.
* If semctl call below fails we are already in an error condition, so
* don't worry about failure here.
* Do not set error code if the next semctl fails, else the previous semctl error code will be lost.
*/
omr_semctlWrapper(portLibrary, FALSE, semid, 0, IPC_RMID, 0);
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Successfully created semaphore");
return OMRPORT_INFO_SHSEM_CREATED;
}
Trc_PRT_shsem_omrshsem_createsemaphore_ExitWithMessage("Error: end of retries. Clean up SysV using 'ipcs'");
return OMRPORT_ERROR_SHSEM_OPFAILED;
}
/**
* @internal
* Open a new SysV semaphore
*
* @param[in] portLibrary
* @param[in] fd read/write file descriptor
* @param[in] baseFile filename matching fd
* @param[in] controlinfo copy of information in control file
* @param[in] groupPerm the group permission of the semaphore
* @param[in] cacheFileType type of control file (regular or older or older-empty)
*
* @return OMRPORT_INFO_SHSEM_OPENED on success
*/
static intptr_t
omrshsem_openSemaphore(struct OMRPortLibrary *portLibrary, intptr_t fd, char *baseFile, omrshsem_baseFileFormat* controlinfo, uintptr_t groupPerm, uintptr_t cacheFileType)
{
/*The control file contains data, and we have successfully read in our structure*/
int semid = -1;
int semflags = groupPerm ? OMRSHSEM_SEMFLAGS_GROUP_OPEN : OMRSHSEM_SEMFLAGS_OPEN;
intptr_t rc = -1;
#if defined (J9ZOS390)
IPCQPROC info;
#endif
union semun semctlArg;
struct semid_ds buf;
semctlArg.buf = &buf;