| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| |
| 8 |
|
| #ifndef WIN32 |
| 9 |
|
| #include <sys/mman.h> |
| 10 |
|
| #include <unistd.h> |
| 11 |
|
| #endif |
| 12 |
|
| |
| 13 |
|
| #ifdef __cplusplus |
| 14 |
|
| } |
| 15 |
|
| #endif |
| 16 |
|
| |
| 17 |
|
| #ifdef NEED_NOT_HERE |
| 18 |
|
| static int |
| 19 |
|
| not_here(s) |
| 20 |
|
| char *s; |
| 21 |
|
| { |
| 22 |
|
| croak("%s not implemented on this architecture", s); |
| 23 |
|
| return -1; |
| 24 |
|
| } |
| 25 |
|
| #endif |
| 26 |
|
| |
| 27 |
|
| #ifdef WIN32 |
| 28 |
|
| |
| 29 |
|
| #define MAP_ANON 1 |
| 30 |
|
| #define MAP_ANONYMOUS 1 |
| 31 |
|
| #define MAP_FILE 2 |
| 32 |
|
| #define MAP_PRIVATE 4 |
| 33 |
|
| #define MAP_SHARED 8 |
| 34 |
|
| #define PROT_READ 1 |
| 35 |
|
| #define PROT_WRITE 2 |
| 36 |
|
| |
| 37 |
|
| static double |
| 38 |
|
| constant(name, arg) |
| 39 |
|
| char *name; |
| 40 |
|
| int arg; |
| 41 |
|
| { |
| 42 |
|
| switch (*name) { |
| 43 |
|
| case 'M': |
| 44 |
|
| if (strEQ(name, "MAP_ANON")) |
| 45 |
|
| return MAP_ANON; |
| 46 |
|
| else if (strEQ(name, "MAP_ANONYMOUS")) |
| 47 |
|
| return MAP_ANONYMOUS; |
| 48 |
|
| |
| 49 |
|
| else if (strEQ(name, "MAP_FILE")) |
| 50 |
|
| return MAP_FILE; |
| 51 |
|
| else if (strEQ(name, "MAP_PRIVATE")) |
| 52 |
|
| return MAP_PRIVATE; |
| 53 |
|
| else if (strEQ(name, "MAP_SHARED")) |
| 54 |
|
| return MAP_SHARED; |
| 55 |
|
| |
| 56 |
|
| break; |
| 57 |
|
| |
| 58 |
|
| case 'P': |
| 59 |
|
| if (strEQ(name, "PROT_READ")) |
| 60 |
|
| return PROT_READ; |
| 61 |
|
| |
| 62 |
|
| else if (strEQ(name, "PROT_WRITE")) |
| 63 |
|
| return PROT_WRITE; |
| 64 |
|
| break; |
| 65 |
|
| |
| 66 |
|
| default: |
| 67 |
|
| break; |
| 68 |
|
| } |
| 69 |
|
| return 0; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| #else |
| 73 |
|
| |
| 74 |
|
| static double |
| 75 |
85 |
| constant(name, arg) |
| 76 |
|
| char *name; |
| 77 |
|
| int arg; |
| 78 |
|
| { |
| 79 |
85 |
| errno = 0; |
| 80 |
85 |
| switch (*name) { |
| 81 |
30 |
| case 'M': |
| 82 |
30 |
▶ | if (strEQ(name, "MAP_ANON")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "MAP_ANON")) { |
16 |
14 |
30 |
|
| 83 |
|
| #ifdef MAP_ANON |
| 84 |
16 |
| return MAP_ANON; |
| 85 |
|
| #else |
| 86 |
|
| errno = ENOENT; |
| 87 |
|
| return 0; |
| 88 |
|
| #endif |
| 89 |
|
| } |
| 90 |
14 |
▶ | if (strEQ(name, "MAP_ANONYMOUS")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "MAP_ANONYMOUS")) { |
0 |
14 |
14 |
|
| 91 |
|
| #ifdef MAP_ANONYMOUS |
| 92 |
0 |
| return MAP_ANONYMOUS; |
| 93 |
|
| #else |
| 94 |
|
| errno = ENOENT; |
| 95 |
|
| return 0; |
| 96 |
|
| #endif |
| 97 |
|
| } |
| 98 |
14 |
▶ | if (strEQ(name, "MAP_FILE")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "MAP_FILE")) { |
7 |
7 |
14 |
|
| 99 |
|
| #ifdef MAP_FILE |
| 100 |
7 |
| return MAP_FILE; |
| 101 |
|
| #else |
| 102 |
|
| errno = ENOENT; |
| 103 |
|
| return 0; |
| 104 |
|
| #endif |
| 105 |
|
| } |
| 106 |
7 |
▶ | if (strEQ(name, "MAP_PRIVATE")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "MAP_PRIVATE")) { |
0 |
7 |
7 |
|
| 107 |
|
| #ifdef MAP_PRIVATE |
| 108 |
0 |
| return MAP_PRIVATE; |
| 109 |
|
| #else |
| 110 |
|
| errno = ENOENT; |
| 111 |
|
| return 0; |
| 112 |
|
| #endif |
| 113 |
|
| } |
| 114 |
7 |
▶ | if (strEQ(name, "MAP_SHARED")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "MAP_SHARED")) { |
7 |
0 |
7 |
|
| 115 |
|
| #ifdef MAP_SHARED |
| 116 |
7 |
| return MAP_SHARED; |
| 117 |
|
| #else |
| 118 |
|
| errno = ENOENT; |
| 119 |
|
| return 0; |
| 120 |
|
| #endif |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
|
| case 'P': |
| 124 |
55 |
▶ | if (strEQ(name, "PROT_READ")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "PROT_READ")) { |
33 |
22 |
55 |
|
| 125 |
|
| #ifdef PROT_READ |
| 126 |
33 |
| return PROT_READ; |
| 127 |
|
| #else |
| 128 |
|
| errno = ENOENT; |
| 129 |
|
| return 0; |
| 130 |
|
| #endif |
| 131 |
|
| } |
| 132 |
22 |
▶ | if (strEQ(name, "PROT_WRITE")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "PROT_WRITE")) { |
22 |
0 |
22 |
|
| 133 |
|
| #ifdef PROT_WRITE |
| 134 |
22 |
| return PROT_WRITE; |
| 135 |
|
| #else |
| 136 |
|
| errno = ENOENT; |
| 137 |
|
| return 0; |
| 138 |
|
| #endif |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| default: |
| 142 |
0 |
| break; |
| 143 |
|
| } |
| 144 |
0 |
| errno = EINVAL; |
| 145 |
0 |
| return 0; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| static size_t pagesize; |
| 149 |
|
| |
| 150 |
|
| #endif /* not Win32 */ |
| 151 |
|
| |
| 152 |
|
| MODULE = IPC::Mmap PACKAGE = IPC::Mmap |
| 153 |
|
| |
| 154 |
|
| double |
| 155 |
|
| constant(name,arg) |
| 156 |
|
| char * name |
| 157 |
|
| int arg |
| 158 |
|
| |
| 159 |
|
| # read len bytes starting at off from mmap's region defined |
| 160 |
|
| # by addr, into the Perl scalar var returns the actual length read |
| 161 |
|
| |
| 162 |
|
| void |
| 163 |
|
| mmap_read(addr, maxlen, off, var, len) |
| 164 |
|
| SV * addr |
| 165 |
|
| size_t maxlen |
| 166 |
|
| int off |
| 167 |
|
| SV * var |
| 168 |
|
| size_t len |
| 169 |
|
| PROTOTYPE: $$$$$ |
| 170 |
|
| PPCODE: |
| 171 |
9 |
| UV tmp = SvUV(addr); |
| 172 |
9 |
| caddr_t lcladdr = INT2PTR(caddr_t, (tmp + off)); |
| 173 |
|
| |
| 174 |
9 |
▶ | if (len > maxlen - off) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > maxlen - off) |
4 |
5 |
9 |
|
| 175 |
4 |
| len = maxlen - off; |
| 176 |
9 |
| # printf("\nmmap_read: length is %i\n", len); |
| 177 |
9 |
▶ | sv_setpvn(var, lcladdr, len); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setpvn(var, lcladdr, len); |
0 |
9 |
9 |
|
| 178 |
9 |
| SvSETMAGIC(var); |
| 179 |
9 |
| ST(0) = sv_2mortal(newSVnv(len)); |
| 180 |
|
| XSRETURN(1); |
| 181 |
|
| |
| 182 |
|
| # write len bytes starting at off from mmap's region defined |
| 183 |
|
| # by addr, from the Perl scalar var. If no len |
| 184 |
|
| # specified, writes complete length of var; if length of |
| 185 |
|
| # var is < len, then only writes var's length; if len of |
| 186 |
|
| # var length exceeds the mmaped region length, |
| 187 |
|
| # only the allowable length will be written |
| 188 |
|
| # returns the number of bytes actually written |
| 189 |
|
| |
| 190 |
|
| void |
| 191 |
|
| mmap_write(addr, maxlen, off, var, len) |
| 192 |
|
| SV * addr |
| 193 |
|
| int maxlen |
| 194 |
|
| int off |
| 195 |
|
| SV * var |
| 196 |
|
| int len |
| 197 |
|
| PROTOTYPE: $$$$$ |
| 198 |
|
| PPCODE: |
| 199 |
8 |
| UV tmp = SvUV(addr); |
| 200 |
8 |
| caddr_t lcladdr = INT2PTR(caddr_t, (tmp + off)); |
| 201 |
|
| STRLEN varlen; |
| 202 |
|
| char * ptr; |
| 203 |
|
| |
| 204 |
|
| # printf("\nmmap_write: addr %p maxlen %i off %i len %i\n", |
| 205 |
8 |
| # lcladdr, maxlen, off, len); |
| 206 |
8 |
▶ | |
Branch: 1 branch
| Branch | True | False | Total |
|
1 |
7 |
8 |
|
| 207 |
1 |
| ptr = SvPV(var, varlen); |
| 208 |
8 |
▶ | if (len > (int)varlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > (int)varlen) |
1 |
7 |
8 |
|
| 209 |
1 |
| len = varlen; |
| 210 |
8 |
| if (len > (maxlen - off)) |
| 211 |
8 |
| len = maxlen - off; |
| 212 |
8 |
| # printf("\n lcladdr is %p offset is %d length is %d ptr is %p\n", addr, off, len, ptr); |
| 213 |
|
| # printf("\ncopying memory\n"); |
| 214 |
|
| # dest = (char *)(lcladdr + off); |
| 215 |
|
| memcpy(lcladdr, ptr, len); |
| 216 |
|
| # dest[len] = (char)0; |
| 217 |
|
| # printf("\nmemory copied\n"); |
| 218 |
|
| # printf("\n%s\n", dest); |
| 219 |
|
| ST(0) = sv_2mortal(newSVnv((int) len)); |
| 220 |
|
| XSRETURN(1); |
| 221 |
|
| |
| 222 |
|
| #ifndef WIN32 |
| 223 |
|
| |
| 224 |
|
| MODULE = IPC::Mmap PACKAGE = IPC::Mmap::POSIX |
| 225 |
|
| |
| 226 |
|
| double |
| 227 |
|
| constant(name,arg) |
| 228 |
|
| char * name |
| 229 |
|
| int arg |
| 230 |
|
| |
| 231 |
|
| void |
| 232 |
|
| _mmap_anon(len, prot, flags) |
| 233 |
|
| size_t len |
| 234 |
|
| int prot |
| 235 |
|
| int flags |
| 236 |
|
| PROTOTYPE: $$$ |
| 237 |
|
| PPCODE: |
| 238 |
|
| int fd; |
| 239 |
|
| void * addr; |
| 240 |
|
| int slop; |
| 241 |
|
| /* struct stat st; */ |
| 242 |
|
| |
| 243 |
0 |
▶ | # printf("\n_mmap: len %i prot %i flags %i\n", len, prot, flags); |
Branch: 1 branch
| Branch | True | False | Total |
| # printf("\n_mmap: len %i prot %i flags %i\n", len, prot, flags); |
0 |
0 |
0 |
|
| 244 |
0 |
| EXTEND(SP, 3); |
| 245 |
0 |
▶ | fd = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| fd = -1; |
0 |
0 |
0 |
|
| 246 |
0 |
| if (!len) { |
| 247 |
|
| croak("mmap: MAP_ANON specified, but no length specified. cannot infer length from file"); |
| 248 |
|
| PUSHs(&PL_sv_undef); |
| 249 |
|
| PUSHs(&PL_sv_undef); |
| 250 |
|
| PUSHs(&PL_sv_undef); |
| 251 |
|
| XSRETURN(3); |
| 252 |
|
| } |
| 253 |
0 |
▶ | |
Branch: 1 branch
| Branch | True | False | Total |
|
0 |
0 |
0 |
|
| 254 |
0 |
| if (pagesize == 0) { |
| 255 |
|
| pagesize = getpagesize(); |
| 256 |
|
| } |
| 257 |
0 |
| |
| 258 |
|
| # slop = off % pagesize; |
| 259 |
0 |
| slop = 0; |
| 260 |
|
| |
| 261 |
|
| # addr = mmap(0, len + slop, prot, flags, fd, off - slop); |
| 262 |
0 |
▶ | addr = mmap(0, len + slop, prot, flags, fd, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| addr = mmap(0, len + slop, prot, flags, fd, 0); |
0 |
0 |
0 |
|
| 263 |
0 |
| |
| 264 |
|
| # printf("\n_mmap: fileno %i slop %i addr %u\n", fd, slop, addr); |
| 265 |
|
| |
| 266 |
|
| if (addr == NULL) { |
| 267 |
|
| croak("mmap: mmap call failed: errno: %d errmsg: %s ", errno, strerror(errno)); |
| 268 |
|
| PUSHs(&PL_sv_undef); |
| 269 |
0 |
| PUSHs(&PL_sv_undef); |
| 270 |
0 |
| PUSHs(&PL_sv_undef); |
| 271 |
0 |
| XSRETURN(3); |
| 272 |
0 |
| } |
| 273 |
|
| # |
| 274 |
|
| # return the address, the length (incl slop), |
| 275 |
|
| # and the offset (adjusted to nearest page) |
| 276 |
|
| # |
| 277 |
|
| PUSHs(sv_2mortal(newSVuv(PTR2UV(addr)))); |
| 278 |
|
| PUSHs(sv_2mortal(newSVnv((int) len + slop))); |
| 279 |
|
| PUSHs(sv_2mortal(newSVnv((int) slop))); |
| 280 |
|
| XSRETURN(3); |
| 281 |
|
| |
| 282 |
|
| void |
| 283 |
|
| _mmap(len, prot, flags, fh) |
| 284 |
|
| size_t len |
| 285 |
|
| int prot |
| 286 |
|
| int flags |
| 287 |
|
| FILE * fh |
| 288 |
|
| PROTOTYPE: $$$* |
| 289 |
|
| PPCODE: |
| 290 |
|
| int fd; |
| 291 |
|
| void * addr; |
| 292 |
|
| int slop; |
| 293 |
|
| struct stat st; |
| 294 |
|
| |
| 295 |
8 |
▶ | # printf("\n_mmap: len %i prot %i flags %i\n", len, prot, flags); |
Branch: 1 branch
| Branch | True | False | Total |
| # printf("\n_mmap: len %i prot %i flags %i\n", len, prot, flags); |
0 |
8 |
8 |
|
| 296 |
8 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
8 |
8 |
|
| 297 |
0 |
| if (flags&MAP_ANON) { |
| 298 |
0 |
▶ | fd = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| fd = -1; |
0 |
0 |
0 |
|
| 299 |
0 |
| if (!len) { |
| 300 |
|
| croak("mmap: MAP_ANON specified, but no length specified. cannot infer length from file"); |
| 301 |
|
| PUSHs(&PL_sv_undef); |
| 302 |
|
| PUSHs(&PL_sv_undef); |
| 303 |
|
| PUSHs(&PL_sv_undef); |
| 304 |
|
| XSRETURN(3); |
| 305 |
|
| } |
| 306 |
|
| } |
| 307 |
8 |
| else { |
| 308 |
8 |
▶ | fd = fileno(fh); |
Branch: 1 branch
| Branch | True | False | Total |
| fd = fileno(fh); |
0 |
8 |
8 |
|
| 309 |
0 |
| if (fd < 0) { |
| 310 |
|
| croak("mmap: file not open or does not have associated fileno"); |
| 311 |
|
| PUSHs(&PL_sv_undef); |
| 312 |
|
| PUSHs(&PL_sv_undef); |
| 313 |
|
| PUSHs(&PL_sv_undef); |
| 314 |
|
| XSRETURN(3); |
| 315 |
8 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
0 |
8 |
8 |
|
| 316 |
0 |
| if (fstat(fd, &st) == -1) { |
| 317 |
|
| croak("mmap: no len provided, fstat failed, unable to infer length"); |
| 318 |
|
| PUSHs(&PL_sv_undef); |
| 319 |
|
| PUSHs(&PL_sv_undef); |
| 320 |
|
| PUSHs(&PL_sv_undef); |
| 321 |
|
| XSRETURN(3); |
| 322 |
8 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
1 |
7 |
8 |
|
| 323 |
1 |
| if (!len) { |
| 324 |
|
| len = st.st_size; |
| 325 |
7 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
0 |
7 |
7 |
|
| 326 |
0 |
| else if (len > st.st_size) { |
| 327 |
|
| croak("_mmap: file size %i too small for specified length %i", st.st_size, len); |
| 328 |
|
| PUSHs(&PL_sv_undef); |
| 329 |
|
| PUSHs(&PL_sv_undef); |
| 330 |
|
| PUSHs(&PL_sv_undef); |
| 331 |
|
| XSRETURN(3); |
| 332 |
|
| } |
| 333 |
|
| # printf("\n_mmap: fileno %i len %i\n", fd, len); |
| 334 |
8 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
4 |
4 |
8 |
|
| 335 |
4 |
| |
| 336 |
|
| if (pagesize == 0) { |
| 337 |
|
| pagesize = getpagesize(); |
| 338 |
8 |
| } |
| 339 |
|
| |
| 340 |
8 |
| # slop = off % pagesize; |
| 341 |
|
| slop = 0; |
| 342 |
|
| |
| 343 |
8 |
▶ | # addr = mmap(0, len + slop, prot, flags, fd, off - slop); |
Branch: 1 branch
| Branch | True | False | Total |
| # addr = mmap(0, len + slop, prot, flags, fd, off - slop); |
0 |
8 |
8 |
|
| 344 |
0 |
| addr = mmap(0, len + slop, prot, flags, fd, 0); |
| 345 |
|
| |
| 346 |
|
| # printf("\n_mmap: fileno %i slop %i addr %u\n", fd, slop, addr); |
| 347 |
|
| |
| 348 |
|
| if (addr == NULL) { |
| 349 |
|
| croak("mmap: mmap call failed: errno: %d errmsg: %s ", errno, strerror(errno)); |
| 350 |
8 |
| PUSHs(&PL_sv_undef); |
| 351 |
8 |
| PUSHs(&PL_sv_undef); |
| 352 |
8 |
| PUSHs(&PL_sv_undef); |
| 353 |
8 |
| XSRETURN(3); |
| 354 |
|
| } |
| 355 |
|
| # |
| 356 |
|
| # return the address, the length (incl slop), |
| 357 |
|
| # and the offset (adjusted to nearest page) |
| 358 |
|
| # |
| 359 |
|
| PUSHs(sv_2mortal(newSVuv(PTR2UV(addr)))); |
| 360 |
|
| PUSHs(sv_2mortal(newSVnv((int) len + slop))); |
| 361 |
|
| PUSHs(sv_2mortal(newSVnv((int) slop))); |
| 362 |
|
| XSRETURN(3); |
| 363 |
|
| |
| 364 |
|
| void |
| 365 |
|
| _munmap(addr, len) |
| 366 |
|
| void * addr |
| 367 |
|
| size_t len |
| 368 |
|
| PROTOTYPE: $$ |
| 369 |
|
| PPCODE: |
| 370 |
0 |
▶ | # |
Branch: 1 branch
| Branch | True | False | Total |
| # |
0 |
0 |
0 |
|
| 371 |
0 |
| # XXX refrain from dumping core if this |
| 372 |
|
| # var wasnt previously mmap'd |
| 373 |
|
| # |
| 374 |
|
| # printf("_munmap: addr %p len %i\n", addr, len); |
| 375 |
0 |
| if (munmap(addr, len) == -1) { |
| 376 |
0 |
| # printf("_munmap failed! errno %d\n", errno); |
| 377 |
|
| croak("munmap failed! errno %d %s\n", errno, strerror(errno)); |
| 378 |
|
| ST(0) = &PL_sv_undef; |
| 379 |
|
| } |
| 380 |
|
| else |
| 381 |
|
| ST(0) = &PL_sv_yes; |
| 382 |
|
| # printf("leaving _munmap\n"); |
| 383 |
|
| XSRETURN(1); |
| 384 |
|
| |
| 385 |
|
| #endif |