source: trunk/mmap.c @ 337

Last change on this file since 337 was 337, checked in by moo, 7 years ago

1.2->trunk: [334] robust error handling on shm init

File size: 6.6 KB
Line 
1
2#include <stdio.h>
3#include <assert.h>
4#include <limits.h>
5#include <string.h>
6#include <stdlib.h>
7
8/* mmap */
9#ifdef ZEND_WIN32
10#   define ftruncate chsize
11#   define getuid() 0
12#   include <process.h>
13#   define XCacheCreateFileMapping(size, perm, name) \
14        CreateFileMapping(INVALID_HANDLE_VALUE, NULL, perm, (sizeof(xc_shmsize_t) > 4) ? size >> 32 : 0, size & 0xffffffff, name)
15#   define XCACHE_MAP_FAILED NULL
16#   define munmap(p, s) UnmapViewOfFile(p)
17#else
18#   include <unistd.h>
19/* make sure to mark(change) it to NULL to keep consistent */
20#   define XCACHE_MAP_FAILED MAP_FAILED
21#endif
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26
27#ifndef ZEND_WIN32
28#include <sys/mman.h>
29#endif
30
31#include "php.h"
32#define XC_SHM_IMPL
33#include "xc_shm.h"
34#include "utils.h"
35
36#ifndef max
37#define max(a, b) ((a) < (b) ? (b) : (a))
38#endif
39
40/* {{{ xc_shm_t */
41struct _xc_shm_t {
42    xc_shm_handlers_t *handlers;
43    void *ptr;
44    void *ptr_ro;
45    long  diff;
46    xc_shmsize_t size;
47    char *name;
48    int newfile;
49    xc_shmsize_t memoffset;
50#ifdef ZEND_WIN32
51    HANDLE hmap;
52    HANDLE hmap_ro;
53#endif
54};
55
56/* }}} */
57#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
58#define PTR_ADD(ptr, v) (((char *) (ptr)) + (v))
59#define PTR_SUB(ptr, v) (((char *) (ptr)) - (v))
60
61static XC_SHM_CAN_READONLY(xc_mmap_can_readonly) /* {{{ */
62{
63    return shm->ptr_ro != NULL;
64}
65/* }}} */
66static XC_SHM_IS_READWRITE(xc_mmap_is_readwrite) /* {{{ */
67{
68    return p >= shm->ptr && (char *)p < (char *)shm->ptr + shm->size;
69}
70/* }}} */
71static XC_SHM_IS_READONLY(xc_mmap_is_readonly) /* {{{ */
72{
73    return xc_mmap_can_readonly(shm) && p >= shm->ptr_ro && (char *)p < (char *)shm->ptr_ro + shm->size;
74}
75/* }}} */
76static XC_SHM_TO_READWRITE(xc_mmap_to_readwrite) /* {{{ */
77{
78    if (shm->diff) {
79        assert(xc_mmap_is_readonly(shm, p));
80        p = PTR_SUB(p, shm->diff);
81    }
82    assert(xc_mmap_is_readwrite(shm, p));
83    return p;
84}
85/* }}} */
86static XC_SHM_TO_READONLY(xc_mmap_to_readonly) /* {{{ */
87{
88    assert(xc_mmap_is_readwrite(shm, p));
89    if (shm->diff) {
90        p = PTR_ADD(p, shm->diff);
91        assert(xc_mmap_is_readonly(shm, p));
92    }
93    return p;
94}
95/* }}} */
96
97static XC_SHM_DESTROY(xc_mmap_destroy) /* {{{ */
98{
99    if (shm->ptr_ro) {
100        munmap(shm->ptr_ro, shm->size);
101        /*
102        shm->ptr_ro = NULL;
103        */
104    }
105    if (shm->ptr) {
106        /* shm->size depends on shm->ptr */
107        munmap(shm->ptr, shm->size);
108        /*
109        shm->ptr = NULL;
110        */
111    }
112#ifdef ZEND_WIN32
113    if (shm->hmap) {
114        CloseHandle(shm->hmap);
115    }
116    if (shm->hmap_ro) {
117        CloseHandle(shm->hmap_ro);
118    }
119#endif
120
121    if (shm->name) {
122#ifdef __CYGWIN__
123        if (shm->newfile) {
124            unlink(shm->name);
125        }
126#endif
127        free(shm->name);
128    }
129    /*
130    shm->size = NULL;
131    shm->diff = 0;
132    */
133
134    free(shm);
135    return;
136}
137/* }}} */
138static XC_SHM_INIT(xc_mmap_init) /* {{{ */
139{
140#ifdef ZEND_WIN32
141#   define TMP_PATH "XCache"
142#else
143#   define TMP_PATH "/tmp/XCache"
144#endif
145    xc_shm_t *shm = NULL;
146    int fd = -1;
147    int ro_ok;
148    volatile void *romem;
149    char tmpname[sizeof(TMP_PATH) - 1 + 100];
150    const char *errstr = NULL;
151    const char *path = (const char *) arg1;
152
153    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
154    shm->size = size;
155
156    if (path == NULL || !path[0]) {
157        static int inc = 0;
158        snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d.%d", TMP_PATH, (int) getuid(), (int) getpid(), inc ++, rand());
159        path = tmpname;
160    }
161#ifdef ZEND_WIN32
162    else {
163        static int inc2 = 0;
164        snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d.%d", path, (int) getuid(), (int) getpid(), inc2 ++, rand());
165        path = tmpname;
166    }
167#endif
168
169    shm->name = strdup(path);
170
171#ifndef ZEND_WIN32
172#   define XCACHE_MMAP_PERMISSION (S_IRUSR | S_IWUSR)
173    fd = open(shm->name, O_RDWR, XCACHE_MMAP_PERMISSION);
174    if (fd == -1) {
175        /* do not create file in /dev */
176        if (strncmp(shm->name, "/dev", 4) == 0) {
177            perror(shm->name);
178            errstr = "Cannot open file set by xcache.mmap_path, check the xcache.size/var_size against system limitation";
179            goto err;
180        }
181        fd = open(shm->name, O_CREAT | O_RDWR, XCACHE_MMAP_PERMISSION);
182        shm->newfile = 1;
183        if (fd == -1) {
184            perror(shm->name);
185            errstr = "Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitation";
186            goto err;
187        }
188    }
189    ftruncate(fd, size);
190#endif
191
192#ifdef ZEND_WIN32
193    shm->hmap = XCacheCreateFileMapping(size, PAGE_READWRITE, shm->name);
194    shm->ptr = (LPSTR) MapViewOfFile(shm->hmap, FILE_MAP_WRITE, 0, 0, 0);
195#else
196    shm->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
197#endif
198
199    if (shm->ptr == XCACHE_MAP_FAILED) {
200        perror(shm->name);
201        errstr = "Failed creating file mappping";
202        shm->ptr = NULL;
203        goto err;
204    }
205
206    /* {{{ readonly protection, mmap it readonly and check if ptr_ro works */
207    if (readonly_protection) {
208        ro_ok = 0;
209
210#ifdef ZEND_WIN32
211        shm->hmap_ro = XCacheCreateFileMapping(size, PAGE_READONLY, shm->name);
212        shm->ptr_ro = (LPSTR) MapViewOfFile(shm->hmap_ro, FILE_MAP_READ, 0, 0, 0);
213#else
214        shm->ptr_ro = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
215#endif
216        if (shm->ptr_ro == XCACHE_MAP_FAILED) {
217            shm->ptr_ro = NULL;
218        }
219        romem = shm->ptr_ro;
220
221        do {
222            if (romem == NULL || romem == shm->ptr) {
223                break;
224            }
225            *(char *)shm->ptr = 1;
226            if (*(char *)romem != 1) {
227                break;
228            }
229            *(char *)shm->ptr = 2;
230            if (*(char *)romem != 2) {
231                break;
232            }
233            ro_ok = 1;
234        } while (0);
235
236        if (ro_ok) {
237            shm->diff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr);
238            /* no overlap */
239            assert(abs(shm->diff) >= size);
240        }
241        else {
242            if (shm->ptr_ro) {
243                munmap(shm->ptr_ro, size);
244            }
245#ifdef ZEND_WIN32
246            if (shm->hmap_ro) {
247                CloseHandle(shm->hmap_ro);
248            }
249#endif
250            shm->ptr_ro = NULL;
251            shm->diff = 0;
252        }
253    }
254
255    /* }}} */
256
257    close(fd);
258#ifndef __CYGWIN__
259    if (shm->newfile) {
260        unlink(shm->name);
261    }
262#endif
263
264    return shm;
265
266err:
267    if (fd != -1) {
268        close(fd);
269    }
270    if (shm) {
271        xc_mmap_destroy(shm);
272    }
273    if (errstr) {
274        fprintf(stderr, "%s\n", errstr);
275        zend_error(E_ERROR, "%s", errstr);
276    }
277    return NULL;
278}
279/* }}} */
280
281static XC_SHM_MEMINIT(xc_mmap_meminit) /* {{{ */
282{
283    xc_mem_t *mem;
284    if (shm->memoffset + size > shm->size) {
285        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
286        return NULL;
287    }
288    mem = (xc_mem_t *) PTR_ADD(shm->ptr, shm->memoffset);
289    shm->memoffset += size;
290    mem->handlers = shm->handlers->memhandlers;
291    mem->handlers->init(shm, mem, size);
292    return mem;
293}
294/* }}} */
295static XC_SHM_MEMDESTROY(xc_mmap_memdestroy) /* {{{ */
296{
297}
298/* }}} */
299
300static xc_shm_handlers_t xc_shm_mmap_handlers = XC_SHM_HANDLERS(mmap);
301void xc_shm_mmap_register() /* {{{ */
302{
303    CHECK(xc_shm_mmap_handlers.memhandlers = xc_mem_scheme_find("mem"), "cannot find mem handlers");
304    if (xc_shm_scheme_register("mmap", &xc_shm_mmap_handlers) == 0) {
305        zend_error(E_ERROR, "XCache: failed to register mmap shm_scheme");
306    }
307err:
308    return;
309}
310/* }}} */
Note: See TracBrowser for help on using the repository browser.