forked from php/pecl-caching-wincache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwincache_filemap.c
1515 lines (1227 loc) · 44.6 KB
/
wincache_filemap.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
/*
+----------------------------------------------------------------------------------------------+
| Windows Cache for PHP |
+----------------------------------------------------------------------------------------------+
| Copyright (c) 2009, Microsoft Corporation. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, are |
| permitted provided that the following conditions are met: |
| - Redistributions of source code must retain the above copyright notice, this list of |
| conditions and the following disclaimer. |
| - Redistributions in binary form must reproduce the above copyright notice, this list of |
| conditions and the following disclaimer in the documentation and/or other materials provided |
| with the distribution. |
| - Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| used to endorse or promote products derived from this software without specific prior written|
| permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE|
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| OF THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------------------------+
| Module: wincache_filemap.c |
+----------------------------------------------------------------------------------------------+
| Author: Kanwaljeet Singla <[email protected]> |
+----------------------------------------------------------------------------------------------+
*/
#include "precomp.h"
#define FILEMAP_INFO_HEADER_SIZE ALIGNQWORD(sizeof(filemap_information_header))
#define FILEMAP_INFO_ENTRY_SIZE ALIGNQWORD(sizeof(filemap_information_entry))
static unsigned int getppid(TSRMLS_D);
static int create_rwlock(char * lockname, lock_context ** pplock TSRMLS_DC);
static void destroy_rwlock(lock_context * plock);
static int create_file_mapping(char * name, char * shmfilepath, unsigned char isfirst,size_t size, HANDLE * pshmfile, unsigned int * pexisting, HANDLE * pmap);
static void * map_viewof_file(HANDLE handle, void * baseaddr);
static int create_information_filemap(filemap_information ** ppinfo TSRMLS_DC);
static void destroy_information_filemap(filemap_information * pinfo);
/* Array of filemap prefixes, in the same order & value as FILEMAP_TYPE_*
* definitions. */
static char * g_filemap_prefix[] = {
"WINCACHE_FILEMAP_INVALID", /* FILEMAP_TYPE_INVALID */
FILEMAP_FILELIST_PREFIX, /* FILEMAP_TYPE_FILELIST */
FILEMAP_RESPATHS_PREFIX, /* FILEMAP_TYPE_RESPATHS */
FILEMAP_FILECONTENT_PREFIX, /* FILEMAP_TYPE_FILECONTENT */
FILEMAP_BYTECODES_PREFIX, /* FILEMAP_TYPE_BYTECODES */
FILEMAP_USERZVALS_PREFIX, /* FILEMAP_TYPE_USERZVALS */
FILEMAP_SESSZVALS_PREFIX, /* FILEMAP_TYPE_SESSZVALS */
};
/* Global information containing information */
/* about all the memory maps which got created */
unsigned short gfilemapid = 1;
/* private method to get parent process id */
static unsigned int getppid(TSRMLS_D)
{
int result = NONFATAL;
unsigned int pid = 0;
HANDLE hSnapShot = INVALID_HANDLE_VALUE;
PROCESSENTRY32 pe = {0};
int poolpid = -1;
dprintverbose("start getppid");
/* Parent process ID will remain constant */
/* Just return what was calculated last time */
if(WCG(parentpid) != 0)
{
goto Finished;
}
pid = GetCurrentProcessId();
/* If localheap setting is set, set ppid as pid */
if(WCG(localheap) != 0)
{
WCG(parentpid) = pid;
goto Finished;
}
/* Use CRC of user provided apppoolid as ppid if available */
poolpid = utils_apoolpid(TSRMLS_C);
if(poolpid != -1)
{
WCG(parentpid) = poolpid;
goto Finished;
}
/* Get the current snapshot and look for */
/* current process by matching PIDs */
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapShot == INVALID_HANDLE_VALUE)
{
error_setlasterror();
result = FATAL_FILEMAP_CREATE_SNAPSHOT;
goto Finished;
}
pe.dwSize = sizeof(PROCESSENTRY32);
/* Go through all the entries in the snapshot */
/* looking for current process */
if(Process32First(hSnapShot, &pe))
{
do
{
if(pe.th32ProcessID == pid)
{
/* If a debugger is present, find parent to parent processId */
if(IsDebuggerPresent())
{
dprintimportant("Debugger present. Finding parent to parent");
pid = pe.th32ParentProcessID;
if(Process32First(hSnapShot, &pe))
{
do
{
if(pe.th32ProcessID == pid)
{
WCG(parentpid) = pe.th32ParentProcessID;
break;
}
}while(Process32Next(hSnapShot, &pe));
}
}
else
{
WCG(parentpid) = pe.th32ParentProcessID;
}
break;
}
}
while(Process32Next(hSnapShot, &pe));
}
Finished:
if(hSnapShot != INVALID_HANDLE_VALUE)
{
CloseHandle(hSnapShot);
hSnapShot = INVALID_HANDLE_VALUE;
}
if(FAILED(result))
{
dprintimportant("failure %d in getppid", result);
}
dprintverbose("end getppid");
return WCG(parentpid);
}
static char * get_filemap_prefix(
unsigned short fmaptype
)
{
if (fmaptype > FILEMAP_TYPE_SESSZVALS)
{
fmaptype = FILEMAP_TYPE_INVALID;
}
return g_filemap_prefix[fmaptype];
}
static int build_filemap_name(
char * dest,
size_t dest_size,
char * namesalt,
unsigned short fmaptype,
unsigned short cachekey,
unsigned int pid
)
{
int ret;
if (namesalt == NULL)
{
ret = _snprintf_s(dest, dest_size, dest_size - 1, "%s_%u_%u", get_filemap_prefix(fmaptype), cachekey, pid);
}
else
{
ret = _snprintf_s(dest, dest_size, dest_size - 1, "%s_%u_%s_%u", get_filemap_prefix(fmaptype), cachekey, namesalt, pid);
}
return ret;
}
static int create_rwlock(char * lockname, lock_context ** pplock TSRMLS_DC)
{
int result = NONFATAL;
lock_context * plock = NULL;
dprintverbose("start create_rwlock");
_ASSERT(lockname != NULL);
_ASSERT(pplock != NULL);
*pplock = NULL;
/* Create a shared read exclusive write lock */
result = lock_create(&plock);
if(FAILED(result))
{
goto Finished;
}
result = lock_initialize(plock, lockname, 1, LOCK_TYPE_SHARED, LOCK_USET_XREAD_XWRITE, NULL TSRMLS_CC);
if(FAILED(result))
{
goto Finished;
}
*pplock = plock;
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in create_rwlock", result);
if(plock != NULL)
{
lock_terminate(plock);
lock_destroy(plock);
plock = NULL;
}
}
dprintverbose("end create_rwlock");
return result;
}
static void destroy_rwlock(lock_context * plock)
{
dprintverbose("start destroy_rwlock");
if(plock != NULL)
{
lock_terminate(plock);
lock_destroy(plock);
plock = NULL;
}
dprintverbose("end destroy_rwlock");
return;
}
static int create_file_mapping(
char * name,
char * shmfilepath,
unsigned char isfirst,
size_t size,
HANDLE * pshmfile,
unsigned int * pexisting,
HANDLE * pmap
)
{
int result = NONFATAL;
HANDLE filehandle = INVALID_HANDLE_VALUE;
HANDLE maphandle = NULL;
unsigned int isexisting = 0;
unsigned int attributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS;
unsigned int sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
unsigned int access = GENERIC_READ | GENERIC_WRITE;
unsigned char globalName[MAX_PATH+1];
dprintverbose("start create_file_mapping");
_ASSERT(name != NULL);
_ASSERT(size > 0);
_ASSERT(pmap != NULL);
/* If a shmfilepath is passed, map the file pointed by path */
if(shmfilepath != NULL)
{
_ASSERT(pshmfile != NULL);
_ASSERT(pexisting != NULL);
_ASSERT(*shmfilepath != '\0');
if (isfirst)
{
/* Delete the file, since we're creating it for the first time */
(void)DeleteFile(shmfilepath);
}
/* Create a new file or open existing */
filehandle = CreateFile(shmfilepath, access, sharemode, NULL, OPEN_ALWAYS, attributes, NULL);
if(filehandle == INVALID_HANDLE_VALUE)
{
error_setlasterror();
result = FATAL_FILEMAP_CREATEFILE;
goto Finished;
}
/* If file already exists, mark existing so that initialization is skipped */
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
isexisting = 1;
/* TBD?? Check file size and error out if its greater than size */
/* TBD?? If file never got initialized properly, mark isexisting = 0 */
/* TBD?? Detect memory corruption. Mark isexisting = 0 */
}
else
{
int aclRet = utils_set_apppool_acl(shmfilepath);
if ( FAILED(aclRet) )
{
/* TODO: If the ACL'ing fails, we should close the file and fall */
/* back to using the system page file. */
CloseHandle(filehandle);
filehandle = INVALID_HANDLE_VALUE;
dprintimportant( "create_file_mapping[%d]: failed to set acl on %s (%d).",
GetCurrentProcessId(),
shmfilepath,
aclRet);
}
}
}
if (WCG(apppoolid))
{
/* prefix the name with "Global\", to ensure the named filemap is in the global space. */
if ( -1 == sprintf_s(globalName, MAX_PATH+1, GLOBAL_SCOPE_PREFIX "%s", name) )
{
result = FATAL_FILEMAP_CREATEFILEMAP;
goto Finished;
}
name = globalName;
}
/* Call CreateFileMapping to create new or open existing file mapping object */
maphandle = CreateFileMapping(filehandle, NULL, PAGE_READWRITE, 0, size, name);
/* handle value null means a fatal error */
if(maphandle == NULL)
{
error_setlasterror();
result = FATAL_FILEMAP_CREATEFILEMAP;
goto Finished;
}
if(shmfilepath != NULL)
{
_ASSERT(filehandle != INVALID_HANDLE_VALUE);
*pshmfile = filehandle;
filehandle = INVALID_HANDLE_VALUE;
*pexisting = isexisting;
}
*pmap = maphandle;
maphandle = NULL;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in create_file_mapping", result);
if(maphandle != NULL)
{
CloseHandle(maphandle);
maphandle = NULL;
}
if(filehandle != INVALID_HANDLE_VALUE)
{
CloseHandle(filehandle);
filehandle = INVALID_HANDLE_VALUE;
}
}
dprintverbose("end create_file_mapping");
return result;
}
static void * map_viewof_file(HANDLE handle, void * baseaddr)
{
void * retaddr = NULL;
if(baseaddr != NULL)
{
dprintverbose("Mapping file map at address = %p", baseaddr);
}
else
{
dprintverbose("Mapping file map at any random address");
}
retaddr = MapViewOfFileEx(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0, baseaddr);
if(retaddr == NULL)
{
error_setlasterror();
goto Finished;
}
_ASSERT(retaddr != NULL);
Finished:
return retaddr;
}
inline size_t ALIGN_UP( size_t size, size_t page_size )
{
size_t pad = size % page_size;
if (pad)
{
size += (page_size - pad);
}
return size;
}
/*++
Routine Description:
Private method to find an open chunk of virtual memory, large enough to
accomodate an allocation of the requested size.
Arguments:
size - requested allocation size.
Return Value:
pointer to base address which should accomodate an allocation of the
requested size.
--*/
void * get_free_vm_base_address(size_t size)
{
SYSTEM_INFO SystemInfo;
MEMORY_BASIC_INFORMATION MemInfo;
unsigned char fDone = FALSE;
size_t cbUpSize;
size_t cbRet;
unsigned char *pBaseTemp;
GetSystemInfo( &SystemInfo );
/* Round up to a page-size aligned allocation */
cbUpSize = ALIGN_UP( size, SystemInfo.dwPageSize );
/* Pick the first candidate base address */
pBaseTemp = ((unsigned char *)SystemInfo.lpMaximumApplicationAddress - cbUpSize);
pBaseTemp++; /* address should now be page-size aligned */
while (!fDone)
{
cbRet = VirtualQuery( pBaseTemp, &MemInfo, sizeof(MEMORY_BASIC_INFORMATION) );
if (!cbRet)
{
pBaseTemp = NULL;
goto Finished;
}
if (MemInfo.State == MEM_FREE)
{
if (MemInfo.RegionSize >= cbUpSize)
{
fDone = TRUE;
}
else
{
pBaseTemp -= ALIGN_UP(cbUpSize - MemInfo.RegionSize, SystemInfo.dwPageSize);
}
}
else
{
pBaseTemp -= cbUpSize;
}
/* Did we run out of candidates? */
if (pBaseTemp <= (unsigned char *)SystemInfo.lpMinimumApplicationAddress)
{
pBaseTemp = NULL;
fDone = TRUE;
}
}
Finished:
return pBaseTemp;
}
static int create_information_filemap(filemap_information ** ppinfo TSRMLS_DC)
{
int result = NONFATAL;
int index = 0;
int size = 0;
int namelen = 0;
filemap_information * pinfo = NULL;
filemap_information_entry * pentry = NULL;
unsigned char isfirst = 1;
unsigned char islocked = 0;
unsigned int isexisting = 0;
DWORD ret = 0;
char * scopePrefix = "";
char * sectionName = NULL;
dprintverbose("start create_information_filemap");
_ASSERT(ppinfo != NULL);
*ppinfo = NULL;
/* Allocate memory for filemap_information */
pinfo = (filemap_information *)alloc_pemalloc(sizeof(filemap_information));
if(pinfo == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
/* Initialize structure values */
pinfo->hinfomap = NULL;
pinfo->infoname = NULL;
pinfo->infonlen = 0;
pinfo->header = NULL;
pinfo->hinitdone = NULL;
pinfo->hrwlock = NULL;
/* First thing to do is create the lock */
/* As the lock is xread_xwrite, doing this before mapping is fine */
result = create_rwlock("FILEMAP_INFO_HRWLOCK", &pinfo->hrwlock TSRMLS_CC);
if(FAILED(result))
{
goto Finished;
}
/* Use PPID in the information filemap so that processes under */
/* one w3wp can share filemap information. Add PID and 2 more for */
/* underscore and terminating NULL */
namelen = FILEMAP_INFORMATION_PREFIX_LENGTH + PID_MAX_LENGTH + 2;
/* If a name salt is specified, use _<namesalt> in name */
if(WCG(namesalt) != NULL)
{
namelen += strlen(WCG(namesalt)) + 1;
}
/* If we're on an app pool, we need to create all named objects in */
/* the Global scope. */
if (WCG(apppoolid))
{
scopePrefix = GLOBAL_SCOPE_PREFIX;
namelen += GLOBAL_SCOPE_PREFIX_LEN;
}
/* Allocate memory to keep name of the information filemap */
pinfo->infoname = (char *)alloc_pemalloc(namelen);
if(pinfo->infoname == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
ZeroMemory(pinfo->infoname, namelen);
/* Create name as FILE_INFORMATION_PREFIX_<ppid> */
if(WCG(namesalt) == NULL)
{
_snprintf_s(pinfo->infoname, namelen, namelen - 1, "%s%s_%u", scopePrefix, FILEMAP_INFORMATION_PREFIX, WCG(fmapgdata)->ppid);
}
else
{
_snprintf_s(pinfo->infoname, namelen, namelen - 1, "%s%s_%s_%u", scopePrefix, FILEMAP_INFORMATION_PREFIX, WCG(namesalt), WCG(fmapgdata)->ppid);
}
pinfo->infonlen = strlen(pinfo->infoname);
result = utils_create_init_event(pinfo->infoname, "_FCACHE_INIT", &pinfo->hinitdone, &isfirst);
if (FAILED(result))
{
result = FATAL_FCACHE_INIT_EVENT;
goto Finished;
}
islocked = 1;
/* Calculate size and try to get the filemap handle */
/* Adding two aligned qwords sizes will produce qword */
size = FILEMAP_INFO_HEADER_SIZE + (FILEMAP_MAX_COUNT * FILEMAP_INFO_ENTRY_SIZE);
if (WCG(apppoolid))
{
/* NOTE: We need to pass the un-Global'd prefixed name to create_file_mapping. */
sectionName = &pinfo->infoname[GLOBAL_SCOPE_PREFIX_LEN];
}
else
{
sectionName = pinfo->infoname;
}
/* shmfilepath = NULL, pfilehandle = NULL, pexisting = NULL */
result = create_file_mapping(sectionName, NULL, isfirst, size, NULL, &isexisting, &pinfo->hinfomap);
if(FAILED(result))
{
goto Finished;
}
if (!isfirst && isexisting)
{
dprintimportant("create_information_filemap[%d]: Warning: We thought we were first, but we found an existing file.\n", WCG(fmapgdata)->pid);
}
/* We have the handle to information file mapping object */
/* Map file mapping object in this process's virtual memory */
pinfo->header = (filemap_information_header *)map_viewof_file(pinfo->hinfomap, NULL);
if(pinfo->header == NULL)
{
result = FATAL_FILEMAP_INFOMAP;
goto Finished;
}
/* Initialize if its not already initialized */
if(isfirst)
{
lock_writelock(pinfo->hrwlock);
/* This is the first process which got the pointer */
/* to information filemap. This should initialize header. */
/* Other processes will be blocked trying to get writelock */
pinfo->header->size = size;
pinfo->header->mapcount = 1;
pinfo->header->maxcount = FILEMAP_MAX_COUNT;
pinfo->header->entry_count = 0;
/* Initialize all entries to type UNUSED */
pentry = (filemap_information_entry *)((char *)pinfo->header + FILEMAP_INFO_HEADER_SIZE);
for(index = 0; index < pinfo->header->maxcount; index++)
{
pentry->fmaptype = FILEMAP_TYPE_UNUSED;
pentry->cachekey = 0;
pentry = (filemap_information_entry *)((char *)pentry + FILEMAP_INFO_ENTRY_SIZE);
}
ReleaseMutex(pinfo->hinitdone);
islocked = 0;
lock_writeunlock(pinfo->hrwlock);
}
else
{
/* Increment the number of times information filemap is mapped */
InterlockedIncrement(&pinfo->header->mapcount);
}
*ppinfo = pinfo;
_ASSERT(SUCCEEDED(result));
Finished:
if(islocked)
{
ReleaseMutex(pinfo->hinitdone);
islocked = 0;
}
if(FAILED(result))
{
dprintimportant("failure %d in create_information_filemap", result);
if(pinfo != NULL)
{
if(pinfo->header != NULL)
{
UnmapViewOfFile((void *)pinfo->header);
pinfo->header = NULL;
}
if(pinfo->hinfomap != NULL)
{
CloseHandle(pinfo->hinfomap);
pinfo->hinfomap = NULL;
}
if(pinfo->hrwlock != NULL)
{
lock_terminate(pinfo->hrwlock);
lock_destroy(pinfo->hrwlock);
pinfo->hrwlock = NULL;
}
if(pinfo->infoname != NULL)
{
alloc_pefree(pinfo->infoname);
pinfo->infoname = NULL;
pinfo->infonlen = 0;
}
if(pinfo->hinitdone != NULL)
{
CloseHandle(pinfo->hinitdone);
pinfo->hinitdone = NULL;
}
alloc_pefree(pinfo);
pinfo = NULL;
}
}
dprintverbose("end create_information_filemap");
return result;
}
/* destroy the information filemap structure we created */
static void destroy_information_filemap(filemap_information * pinfo)
{
dprintverbose("start destroy_information_filemap");
if(pinfo != NULL)
{
if(pinfo->header != NULL)
{
InterlockedDecrement(&pinfo->header->mapcount);
UnmapViewOfFile((void *)pinfo->header);
pinfo->header = NULL;
}
if(pinfo->hinfomap != NULL)
{
CloseHandle(pinfo->hinfomap);
pinfo->hinfomap = NULL;
}
if(pinfo->hrwlock != NULL)
{
lock_terminate(pinfo->hrwlock);
lock_destroy(pinfo->hrwlock);
pinfo->hrwlock = NULL;
}
if(pinfo->infoname != NULL)
{
alloc_pefree(pinfo->infoname);
pinfo->infoname = NULL;
pinfo->infonlen = 0;
}
if(pinfo->hinitdone != NULL)
{
CloseHandle(pinfo->hinitdone);
pinfo->hinitdone = NULL;
}
alloc_pefree(pinfo);
pinfo = NULL;
}
dprintverbose("end destroy_information_filemap");
return;
}
/* Global initializer which should be called once per process */
int filemap_global_initialize(TSRMLS_D)
{
int result = NONFATAL;
filemap_global_context * fgcontext = NULL;
dprintverbose("start filemap_global_initialize");
/* If global_initialize has already been called, just return */
if(WCG(fmapgdata) != NULL)
{
goto Finished;
}
/* allocate persistent memory for fgcontext */
fgcontext = (filemap_global_context *)alloc_pemalloc(sizeof(filemap_global_context));
if(fgcontext == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
/* Set default values of structure members */
fgcontext->pid = GetCurrentProcessId();
fgcontext->ppid = getppid(TSRMLS_C);
fgcontext->info = NULL;
/* Set global as soon as pid and ppid are set */
WCG(fmapgdata) = fgcontext;
result = create_information_filemap(&fgcontext->info TSRMLS_CC);
if(FAILED(result))
{
goto Finished;
}
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in filemap_global_initialize", result);
if(fgcontext != NULL)
{
WCG(fmapgdata) = NULL;
if(fgcontext->info != NULL)
{
destroy_information_filemap(fgcontext->info);
fgcontext->info = NULL;
}
alloc_pefree(fgcontext);
fgcontext = NULL;
}
}
dprintverbose("end filemap_global_initialize");
return result;
}
/* Terminate global information including information filemap */
void filemap_global_terminate(TSRMLS_D)
{
dprintverbose("start filemap_global_terminate");
if(WCG(fmapgdata) != NULL)
{
if(WCG(fmapgdata)->info != NULL)
{
destroy_information_filemap(WCG(fmapgdata)->info);
WCG(fmapgdata)->info = NULL;
}
alloc_pefree(WCG(fmapgdata));
WCG(fmapgdata) = NULL;
}
dprintverbose("end filemap_global_terminate");
return;
}
/* API to get current process ID */
unsigned int filemap_getpid(TSRMLS_D)
{
_ASSERT(WCG(fmapgdata) != NULL);
return WCG(fmapgdata)->pid;
}
/* API tp get the parent process ID */
/* Use parent process identifier to create */
/* separate caches for processes under a process */
unsigned int filemap_getppid(TSRMLS_D)
{
_ASSERT(WCG(fmapgdata) != NULL);
return WCG(fmapgdata)->ppid;
}
/* create new filemap context */
int filemap_create(filemap_context ** ppfilemap)
{
int result = NONFATAL;
filemap_context * pfilemap = NULL;
dprintverbose("start filemap_create");
_ASSERT(ppfilemap != NULL);
*ppfilemap = NULL;
pfilemap = (filemap_context *)alloc_pemalloc(sizeof(filemap_context));
if(pfilemap == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
pfilemap->id = gfilemapid++;
pfilemap->islocal = 0;
pfilemap->infoentry = NULL;
pfilemap->hfilemap = NULL;
pfilemap->hshmfile = INVALID_HANDLE_VALUE;
pfilemap->existing = 0;
pfilemap->mapaddr = NULL;
*ppfilemap = pfilemap;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in filemap_create", result);
}
dprintverbose("end filemap_create");
return result;
}
void filemap_destroy(filemap_context * pfilemap)
{
dprintverbose("start filemap_destroy");
if(pfilemap != NULL)
{
alloc_pefree(pfilemap);
pfilemap = NULL;
}
dprintverbose("end filemap_destroy");
return;
}
int filemap_initialize(filemap_context * pfilemap, unsigned short fmaptype, unsigned short cachekey, unsigned short fmclass, unsigned int size_mb, unsigned char isfirst, char * shmfilepath TSRMLS_DC)
{
int result = NONFATAL;
unsigned int ffree = 0;
unsigned int size = 0;
unsigned int index = 0;
unsigned int found = 0;
void * mapaddr = NULL;
unsigned char flock = 0;
unsigned int fcreate_file_for_sm = 1;
char * sm_file_path = shmfilepath;
HANDLE hOriginalToken = NULL;
filemap_information * pinfo = NULL;
filemap_information_header * pinfoh = NULL;
filemap_information_entry * pentry = NULL;
dprintverbose("start filemap_initialize");
_ASSERT(WCG(fmapgdata) != NULL);
_ASSERT(cachekey != 0);
_ASSERT(pfilemap != NULL);
_ASSERT(size_mb > 0);
size = size_mb * 1024 * 1024;
/* If parentpid is greater than 99999, use shmfilepath */
/* Else don't create file backed shared memory */
if(WCG(fmapgdata)->ppid <= 99999)
{
sm_file_path = NULL;
fcreate_file_for_sm = 0;
}
/* See if this is already there in the list of filemaps */
/* If not create a new filemap and add to the list */
pinfo = WCG(fmapgdata)->info;
pinfoh = pinfo->header;
_ASSERT(pinfoh != NULL);
if(fmclass != FILEMAP_MAP_LRANDOM)
{
/* Check if fmaptype is already there in the list of filemaps available */
lock_writelock(pinfo->hrwlock);
flock = 1;
pentry = (filemap_information_entry *)((char *)pinfoh + FILEMAP_INFO_HEADER_SIZE);
found = 0;
for(index = 0; index < pinfoh->maxcount; index++)
{
if(pentry->fmaptype == FILEMAP_TYPE_UNUSED && ffree == 0)
{
ffree = index;
}
if(pentry->fmaptype == fmaptype && pentry->cachekey == cachekey)
{
found = 1;
break;