source: branches/1.2/mmap.c @ 334

Last change on this file since 334 was 334, checked in by moo, 8 years ago

fix #56, more robust error message/handling

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