1 | #ifndef SCHMORP_PERL_H_ |
1 | #ifndef SCHMORP_PERL_H_ |
2 | #define SCHMORP_PERL_H_ |
2 | #define SCHMORP_PERL_H_ |
3 | |
3 | |
4 | /* WARNING |
4 | /* WARNING |
5 | * This header file is a shared resource between many modules. |
5 | * This header file is a shared resource between many modules. |
|
|
6 | * perl header files MUST already be included. |
6 | */ |
7 | */ |
7 | |
8 | |
8 | #include <signal.h> |
9 | #include <signal.h> |
9 | #include <errno.h> |
10 | #include <errno.h> |
10 | |
11 | |
11 | #ifndef _WIN32 |
12 | #if defined(WIN32 ) || defined(_MINIX) |
|
|
13 | # define SCHMORP_H_PREFER_SELECT 1 |
|
|
14 | #endif |
|
|
15 | |
|
|
16 | #if !SCHMORP_H_PREFER_SELECT |
12 | # include <poll.h> |
17 | # include <poll.h> |
13 | #endif |
18 | #endif |
14 | |
19 | |
15 | /* useful stuff, used by schmorp mostly */ |
20 | /* useful stuff, used by schmorp mostly */ |
16 | |
21 | |
… | |
… | |
20 | (PERL_REVISION > (a) \ |
25 | (PERL_REVISION > (a) \ |
21 | || (PERL_REVISION == (a) \ |
26 | || (PERL_REVISION == (a) \ |
22 | && (PERL_VERSION > (b) \ |
27 | && (PERL_VERSION > (b) \ |
23 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
28 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
24 | |
29 | |
|
|
30 | #ifndef PERL_MAGIC_ext |
|
|
31 | # define PERL_MAGIC_ext '~' |
|
|
32 | #endif |
|
|
33 | |
25 | #if !PERL_VERSION_ATLEAST (5,6,0) |
34 | #if !PERL_VERSION_ATLEAST (5,6,0) |
26 | # ifndef PL_ppaddr |
35 | # ifndef PL_ppaddr |
27 | # define PL_ppaddr ppaddr |
36 | # define PL_ppaddr ppaddr |
28 | # endif |
37 | # endif |
29 | # ifndef call_sv |
38 | # ifndef call_sv |
… | |
… | |
40 | # endif |
49 | # endif |
41 | # ifndef IS_PADCONST |
50 | # ifndef IS_PADCONST |
42 | # define IS_PADCONST(v) 0 |
51 | # define IS_PADCONST(v) 0 |
43 | # endif |
52 | # endif |
44 | #endif |
53 | #endif |
|
|
54 | |
|
|
55 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
56 | #if IVSIZE >= 8 |
|
|
57 | typedef IV VAL64; |
|
|
58 | # define SvVAL64(sv) SvIV (sv) |
|
|
59 | # define newSVval64(i64) newSViv (i64) |
|
|
60 | # define sv_setval64(sv,i64) sv_setiv ((sv), (i64)) |
|
|
61 | #else |
|
|
62 | typedef NV VAL64; |
|
|
63 | # define SvVAL64(sv) SvNV (sv) |
|
|
64 | # define newSVval64(i64) newSVnv (i64) |
|
|
65 | # define sv_setval64(sv,i64) sv_setnv ((sv), (i64)) |
|
|
66 | #endif |
|
|
67 | |
|
|
68 | /* typemap for the above */ |
|
|
69 | /* |
|
|
70 | VAL64 T_VAL64 |
|
|
71 | |
|
|
72 | INPUT |
|
|
73 | |
|
|
74 | T_VAL64 |
|
|
75 | $var = ($type)SvVAL64 ($arg); |
|
|
76 | |
|
|
77 | OUTPUT |
|
|
78 | |
|
|
79 | T_VAL64 |
|
|
80 | $arg = newSVval64 ($var); |
|
|
81 | */ |
45 | |
82 | |
46 | /* 5.11 */ |
83 | /* 5.11 */ |
47 | #ifndef CxHASARGS |
84 | #ifndef CxHASARGS |
48 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
85 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
49 | #endif |
86 | #endif |
… | |
… | |
153 | s_get_cv (SV *cb_sv) |
190 | s_get_cv (SV *cb_sv) |
154 | { |
191 | { |
155 | dTHX; |
192 | dTHX; |
156 | HV *st; |
193 | HV *st; |
157 | GV *gvp; |
194 | GV *gvp; |
158 | |
195 | |
159 | return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
196 | return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
160 | } |
197 | } |
161 | |
198 | |
162 | static SV * |
199 | static SV * |
163 | s_get_cv_croak (SV *cb_sv) |
200 | s_get_cv_croak (SV *cb_sv) |
… | |
… | |
197 | } |
234 | } |
198 | |
235 | |
199 | /*****************************************************************************/ |
236 | /*****************************************************************************/ |
200 | /* portable pipe/socketpair */ |
237 | /* portable pipe/socketpair */ |
201 | |
238 | |
202 | #ifdef USE_SOCKETS_AS_HANDLES |
239 | #if defined(USE_SOCKETS_AS_HANDLES) || PERL_VERSION_ATLEAST(5,18,0) |
203 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
240 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
204 | #else |
241 | #else |
205 | # define S_TO_HANDLE(x) ((HANDLE)x) |
242 | # define S_TO_HANDLE(x) ((HANDLE)x) |
206 | #endif |
243 | #endif |
207 | |
244 | |
… | |
… | |
218 | struct sockaddr_in adr2; |
255 | struct sockaddr_in adr2; |
219 | int adr2_size = sizeof (adr2); |
256 | int adr2_size = sizeof (adr2); |
220 | SOCKET listener; |
257 | SOCKET listener; |
221 | SOCKET sock [2] = { -1, -1 }; |
258 | SOCKET sock [2] = { -1, -1 }; |
222 | |
259 | |
223 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
260 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
224 | return -1; |
261 | return -1; |
225 | |
262 | |
226 | addr.sin_family = AF_INET; |
263 | addr.sin_family = AF_INET; |
227 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
264 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
228 | addr.sin_port = 0; |
265 | addr.sin_port = 0; |
… | |
… | |
234 | goto fail; |
271 | goto fail; |
235 | |
272 | |
236 | if (listen (listener, 1)) |
273 | if (listen (listener, 1)) |
237 | goto fail; |
274 | goto fail; |
238 | |
275 | |
239 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
276 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
240 | goto fail; |
277 | goto fail; |
241 | |
278 | |
242 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
279 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
243 | goto fail; |
280 | goto fail; |
244 | |
281 | |
… | |
… | |
269 | || addr.sin_port != adr2.sin_port) |
306 | || addr.sin_port != adr2.sin_port) |
270 | goto fail; |
307 | goto fail; |
271 | |
308 | |
272 | closesocket (listener); |
309 | closesocket (listener); |
273 | |
310 | |
274 | #ifdef USE_SOCKETS_AS_HANDLES |
311 | #if defined(USE_SOCKETS_AS_HANDLES) || PERL_VERSION_ATLEAST(5,18,0) |
275 | /* when select isn't winsocket, we also expect socket, connect, accept etc. |
312 | /* when select isn't winsocket, we also expect socket, connect, accept etc. |
276 | * to work on fds */ |
313 | * to work on fds */ |
277 | filedes [0] = sock [0]; |
314 | filedes [0] = sock [0]; |
278 | filedes [1] = sock [1]; |
315 | filedes [1] = sock [1]; |
279 | #else |
316 | #else |
… | |
… | |
322 | || fcntl (fd, F_SETFD, FD_CLOEXEC); |
359 | || fcntl (fd, F_SETFD, FD_CLOEXEC); |
323 | } |
360 | } |
324 | |
361 | |
325 | #endif |
362 | #endif |
326 | |
363 | |
|
|
364 | #if HAVE_EVENTFD |
|
|
365 | # include <sys/eventfd.h> |
|
|
366 | #else |
327 | #if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
367 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
|
|
368 | # define SCHMORP_H_HAVE_EVENTFD 1 |
328 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
369 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
329 | # include <stdint.h> |
370 | # include <stdint.h> |
330 | # ifdef __cplusplus |
371 | # ifdef __cplusplus |
331 | extern "C" { |
372 | extern "C" { |
332 | # endif |
373 | # endif |
333 | int eventfd (unsigned int initval, int flags); |
374 | int eventfd (unsigned int initval, int flags); |
334 | # ifdef __cplusplus |
375 | # ifdef __cplusplus |
335 | } |
376 | } |
336 | # endif |
377 | # endif |
337 | #else |
378 | # else |
338 | # define eventfd(initval,flags) -1 |
379 | # define eventfd(initval,flags) -1 |
|
|
380 | # endif |
339 | #endif |
381 | #endif |
340 | |
382 | |
341 | typedef struct { |
383 | typedef struct { |
342 | int fd[2]; /* read, write fd, might be equal */ |
384 | int fd[2]; /* read, write fd, might be equal */ |
343 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
385 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
… | |
… | |
440 | if (epp->len) |
482 | if (epp->len) |
441 | { |
483 | { |
442 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
484 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
443 | croak ("unable to dup over old event pipe"); /* should not croak */ |
485 | croak ("unable to dup over old event pipe"); /* should not croak */ |
444 | |
486 | |
445 | if (epp->fd [1] != epp->fd [0]) |
|
|
446 | close (epn.fd [0]); |
487 | close (epn.fd [0]); |
|
|
488 | |
|
|
489 | if (epn.fd [0] == epn.fd [1]) |
|
|
490 | epn.fd [1] = epp->fd [0]; |
447 | |
491 | |
448 | epn.fd [0] = epp->fd [0]; |
492 | epn.fd [0] = epp->fd [0]; |
449 | } |
493 | } |
450 | |
494 | |
451 | *epp = epn; |
495 | *epp = epn; |
… | |
… | |
457 | |
501 | |
458 | static int |
502 | static int |
459 | s_epipe_wait (s_epipe *epp) |
503 | s_epipe_wait (s_epipe *epp) |
460 | { |
504 | { |
461 | dTHX; |
505 | dTHX; |
462 | #ifdef _WIN32 |
506 | #if SCHMORP_H_PREFER_SELECT |
463 | fd_set rfd; |
507 | fd_set rfd; |
464 | int fd = s_epipe_fd (epp); |
508 | int fd = s_epipe_fd (epp); |
465 | |
509 | |
466 | FD_ZERO (&rfd); |
510 | FD_ZERO (&rfd); |
467 | FD_SET (fd, &rfd); |
511 | FD_SET (fd, &rfd); |
… | |
… | |
472 | struct pollfd pfd; |
516 | struct pollfd pfd; |
473 | |
517 | |
474 | pfd.fd = s_epipe_fd (epp); |
518 | pfd.fd = s_epipe_fd (epp); |
475 | pfd.events = POLLIN; |
519 | pfd.events = POLLIN; |
476 | |
520 | |
477 | return poll (&pfd, 1, 0); |
521 | return poll (&pfd, 1, -1); |
478 | #endif |
522 | #endif |
479 | } |
523 | } |
480 | |
524 | |
481 | #endif |
525 | #endif |
482 | |
526 | |