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> |
|
|
10 | #include <errno.h> |
9 | |
11 | |
10 | #ifndef _WIN32 |
12 | #if defined(WIN32 ) || defined(_MINIX) |
|
|
13 | # define SCHMORP_H_PREFER_SELECT 1 |
|
|
14 | #endif |
|
|
15 | |
|
|
16 | #if !SCHMORP_H_PREFER_SELECT |
11 | # include <poll.h> |
17 | # include <poll.h> |
12 | #endif |
18 | #endif |
13 | |
19 | |
14 | /* useful stuff, used by schmorp mostly */ |
20 | /* useful stuff, used by schmorp mostly */ |
15 | |
21 | |
… | |
… | |
19 | (PERL_REVISION > (a) \ |
25 | (PERL_REVISION > (a) \ |
20 | || (PERL_REVISION == (a) \ |
26 | || (PERL_REVISION == (a) \ |
21 | && (PERL_VERSION > (b) \ |
27 | && (PERL_VERSION > (b) \ |
22 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
28 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
23 | |
29 | |
|
|
30 | #ifndef PERL_MAGIC_ext |
|
|
31 | # define PERL_MAGIC_ext '~' |
|
|
32 | #endif |
|
|
33 | |
24 | #if !PERL_VERSION_ATLEAST (5,6,0) |
34 | #if !PERL_VERSION_ATLEAST (5,6,0) |
25 | # ifndef PL_ppaddr |
35 | # ifndef PL_ppaddr |
26 | # define PL_ppaddr ppaddr |
36 | # define PL_ppaddr ppaddr |
27 | # endif |
37 | # endif |
28 | # ifndef call_sv |
38 | # ifndef call_sv |
… | |
… | |
39 | # endif |
49 | # endif |
40 | # ifndef IS_PADCONST |
50 | # ifndef IS_PADCONST |
41 | # define IS_PADCONST(v) 0 |
51 | # define IS_PADCONST(v) 0 |
42 | # endif |
52 | # endif |
43 | #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 | */ |
44 | |
82 | |
45 | /* 5.11 */ |
83 | /* 5.11 */ |
46 | #ifndef CxHASARGS |
84 | #ifndef CxHASARGS |
47 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
85 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
48 | #endif |
86 | #endif |
… | |
… | |
152 | s_get_cv (SV *cb_sv) |
190 | s_get_cv (SV *cb_sv) |
153 | { |
191 | { |
154 | dTHX; |
192 | dTHX; |
155 | HV *st; |
193 | HV *st; |
156 | GV *gvp; |
194 | GV *gvp; |
157 | |
195 | |
158 | return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
196 | return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
159 | } |
197 | } |
160 | |
198 | |
161 | static SV * |
199 | static SV * |
162 | s_get_cv_croak (SV *cb_sv) |
200 | s_get_cv_croak (SV *cb_sv) |
… | |
… | |
196 | } |
234 | } |
197 | |
235 | |
198 | /*****************************************************************************/ |
236 | /*****************************************************************************/ |
199 | /* portable pipe/socketpair */ |
237 | /* portable pipe/socketpair */ |
200 | |
238 | |
201 | #ifdef USE_SOCKETS_AS_HANDLES |
239 | #if defined(USE_SOCKETS_AS_HANDLES) || PERL_VERSION_ATLEAST(5,18,0) |
202 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
240 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
203 | #else |
241 | #else |
204 | # define S_TO_HANDLE(x) ((HANDLE)x) |
242 | # define S_TO_HANDLE(x) ((HANDLE)x) |
205 | #endif |
243 | #endif |
206 | |
244 | |
… | |
… | |
208 | /* taken almost verbatim from libev's ev_win32.c */ |
246 | /* taken almost verbatim from libev's ev_win32.c */ |
209 | /* oh, the humanity! */ |
247 | /* oh, the humanity! */ |
210 | static int |
248 | static int |
211 | s_pipe (int filedes [2]) |
249 | s_pipe (int filedes [2]) |
212 | { |
250 | { |
|
|
251 | dTHX; |
|
|
252 | |
213 | struct sockaddr_in addr = { 0 }; |
253 | struct sockaddr_in addr = { 0 }; |
214 | int addr_size = sizeof (addr); |
254 | int addr_size = sizeof (addr); |
215 | struct sockaddr_in adr2; |
255 | struct sockaddr_in adr2; |
216 | int adr2_size = sizeof (adr2); |
256 | int adr2_size = sizeof (adr2); |
217 | SOCKET listener; |
257 | SOCKET listener; |
218 | SOCKET sock [2] = { -1, -1 }; |
258 | SOCKET sock [2] = { -1, -1 }; |
219 | |
259 | |
220 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
260 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
221 | return -1; |
261 | return -1; |
222 | |
262 | |
223 | addr.sin_family = AF_INET; |
263 | addr.sin_family = AF_INET; |
224 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
264 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
225 | addr.sin_port = 0; |
265 | addr.sin_port = 0; |
… | |
… | |
231 | goto fail; |
271 | goto fail; |
232 | |
272 | |
233 | if (listen (listener, 1)) |
273 | if (listen (listener, 1)) |
234 | goto fail; |
274 | goto fail; |
235 | |
275 | |
236 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
276 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
237 | goto fail; |
277 | goto fail; |
238 | |
278 | |
239 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
279 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
240 | goto fail; |
280 | goto fail; |
241 | |
281 | |
… | |
… | |
266 | || addr.sin_port != adr2.sin_port) |
306 | || addr.sin_port != adr2.sin_port) |
267 | goto fail; |
307 | goto fail; |
268 | |
308 | |
269 | closesocket (listener); |
309 | closesocket (listener); |
270 | |
310 | |
271 | #ifdef USE_SOCKETS_AS_HANDLES |
311 | #if defined(USE_SOCKETS_AS_HANDLES) || PERL_VERSION_ATLEAST(5,18,0) |
272 | /* 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. |
273 | * to work on fds */ |
313 | * to work on fds */ |
274 | filedes [0] = sock [0]; |
314 | filedes [0] = sock [0]; |
275 | filedes [1] = sock [1]; |
315 | filedes [1] = sock [1]; |
276 | #else |
316 | #else |
… | |
… | |
319 | || fcntl (fd, F_SETFD, FD_CLOEXEC); |
359 | || fcntl (fd, F_SETFD, FD_CLOEXEC); |
320 | } |
360 | } |
321 | |
361 | |
322 | #endif |
362 | #endif |
323 | |
363 | |
|
|
364 | #if HAVE_EVENTFD |
|
|
365 | # include <sys/eventfd.h> |
|
|
366 | #else |
324 | #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 |
325 | /* 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 */ |
326 | # include <stdint.h> |
370 | # include <stdint.h> |
327 | # ifdef __cplusplus |
371 | # ifdef __cplusplus |
328 | extern "C" { |
372 | extern "C" { |
329 | # endif |
373 | # endif |
330 | int eventfd (unsigned int initval, int flags); |
374 | int eventfd (unsigned int initval, int flags); |
331 | # ifdef __cplusplus |
375 | # ifdef __cplusplus |
332 | } |
376 | } |
333 | # endif |
377 | # endif |
334 | #else |
378 | # else |
335 | # define eventfd(initval,flags) -1 |
379 | # define eventfd(initval,flags) -1 |
|
|
380 | # endif |
336 | #endif |
381 | #endif |
337 | |
382 | |
338 | typedef struct { |
383 | typedef struct { |
339 | int fd[2]; /* read, write fd, might be equal */ |
384 | int fd[2]; /* read, write fd, might be equal */ |
340 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
385 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
341 | volatile sig_atomic_t sent; |
|
|
342 | } s_epipe; |
386 | } s_epipe; |
343 | |
387 | |
344 | static int |
388 | static int |
345 | s_epipe_new (s_epipe *epp) |
389 | s_epipe_new (s_epipe *epp) |
346 | { |
390 | { |
… | |
… | |
359 | return -1; |
403 | return -1; |
360 | |
404 | |
361 | if (s_fd_prepare (ep.fd [0]) |
405 | if (s_fd_prepare (ep.fd [0]) |
362 | || s_fd_prepare (ep.fd [1])) |
406 | || s_fd_prepare (ep.fd [1])) |
363 | { |
407 | { |
|
|
408 | dTHX; |
|
|
409 | |
364 | close (ep.fd [0]); |
410 | close (ep.fd [0]); |
365 | close (ep.fd [1]); |
411 | close (ep.fd [1]); |
366 | return -1; |
412 | return -1; |
367 | } |
413 | } |
368 | |
414 | |
369 | ep.len = 1; |
415 | ep.len = 1; |
370 | } |
416 | } |
371 | |
417 | |
372 | ep.sent = 0; |
|
|
373 | *epp = ep; |
418 | *epp = ep; |
374 | return 0; |
419 | return 0; |
375 | } |
420 | } |
376 | |
421 | |
377 | static void |
422 | static void |
378 | s_epipe_destroy (s_epipe *epp) |
423 | s_epipe_destroy (s_epipe *epp) |
379 | { |
424 | { |
|
|
425 | dTHX; |
|
|
426 | |
380 | close (epp->fd [0]); |
427 | close (epp->fd [0]); |
381 | |
428 | |
382 | if (epp->fd [1] != epp->fd [0]) |
429 | if (epp->fd [1] != epp->fd [0]) |
383 | close (epp->fd [1]); |
430 | close (epp->fd [1]); |
384 | |
431 | |
… | |
… | |
386 | } |
433 | } |
387 | |
434 | |
388 | static void |
435 | static void |
389 | s_epipe_signal (s_epipe *epp) |
436 | s_epipe_signal (s_epipe *epp) |
390 | { |
437 | { |
391 | if (epp->sent) |
|
|
392 | return; |
|
|
393 | |
|
|
394 | epp->sent = 1; |
|
|
395 | { |
|
|
396 | #ifdef _WIN32 |
438 | #ifdef _WIN32 |
397 | /* perl overrides send with a function that crashes in other threads. |
439 | /* perl overrides send with a function that crashes in other threads. |
398 | * unfortunately, it overrides it with an argument-less macro, so |
440 | * unfortunately, it overrides it with an argument-less macro, so |
399 | * there is no way to force usage of the real send function. |
441 | * there is no way to force usage of the real send function. |
400 | * incompetent windows programmers - is this redundant? |
442 | * incompetent windows programmers - is this redundant? |
401 | */ |
443 | */ |
402 | DWORD dummy; |
444 | DWORD dummy; |
403 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
445 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
404 | #else |
446 | #else |
405 | static uint64_t counter = 1; |
447 | static uint64_t counter = 1; |
|
|
448 | /* some modules accept fd's from outside, support eventfd here */ |
|
|
449 | if (write (epp->fd [1], &counter, epp->len) < 0 |
|
|
450 | && errno == EINVAL |
|
|
451 | && epp->len != 8) |
406 | write (epp->fd [1], &counter, epp->len); |
452 | write (epp->fd [1], &counter, (epp->len = 8)); |
407 | #endif |
453 | #endif |
408 | } |
|
|
409 | } |
454 | } |
410 | |
455 | |
411 | static void |
456 | static void |
412 | s_epipe_drain (s_epipe *epp) |
457 | s_epipe_drain (s_epipe *epp) |
413 | { |
458 | { |
|
|
459 | dTHX; |
414 | char buf [9]; |
460 | char buf [9]; |
415 | |
461 | |
416 | #ifdef _WIN32 |
462 | #ifdef _WIN32 |
417 | recv (epp->fd [0], buf, sizeof (buf), 0); |
463 | recv (epp->fd [0], buf, sizeof (buf), 0); |
418 | #else |
464 | #else |
419 | read (epp->fd [0], buf, sizeof (buf)); |
465 | read (epp->fd [0], buf, sizeof (buf)); |
420 | #endif |
466 | #endif |
421 | |
|
|
422 | epp->sent = 0; |
|
|
423 | } |
467 | } |
424 | |
468 | |
425 | /* like new, but dups over old */ |
469 | /* like new, but dups over old */ |
426 | static int |
470 | static int |
427 | s_epipe_renew (s_epipe *epp) |
471 | s_epipe_renew (s_epipe *epp) |
428 | { |
472 | { |
|
|
473 | dTHX; |
429 | s_epipe epn; |
474 | s_epipe epn; |
430 | |
475 | |
431 | if (epp->fd [1] != epp->fd [0]) |
476 | if (epp->fd [1] != epp->fd [0]) |
432 | close (epp->fd [1]); |
477 | close (epp->fd [1]); |
433 | |
478 | |
… | |
… | |
437 | if (epp->len) |
482 | if (epp->len) |
438 | { |
483 | { |
439 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
484 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
440 | croak ("unable to dup over old event pipe"); /* should not croak */ |
485 | croak ("unable to dup over old event pipe"); /* should not croak */ |
441 | |
486 | |
442 | if (epp->fd [1] != epp->fd [0]) |
|
|
443 | 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]; |
444 | |
491 | |
445 | epn.fd [0] = epp->fd [0]; |
492 | epn.fd [0] = epp->fd [0]; |
446 | } |
493 | } |
447 | |
494 | |
448 | *epp = epn; |
495 | *epp = epn; |
… | |
… | |
453 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
500 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
454 | |
501 | |
455 | static int |
502 | static int |
456 | s_epipe_wait (s_epipe *epp) |
503 | s_epipe_wait (s_epipe *epp) |
457 | { |
504 | { |
458 | #ifdef _WIN32 |
505 | dTHX; |
|
|
506 | #if SCHMORP_H_PREFER_SELECT |
459 | fd_set rfd; |
507 | fd_set rfd; |
460 | int fd = s_epipe_fd (epp); |
508 | int fd = s_epipe_fd (epp); |
461 | |
509 | |
462 | FD_ZERO (&rfd); |
510 | FD_ZERO (&rfd); |
463 | FD_SET (fd, &rfd); |
511 | FD_SET (fd, &rfd); |
… | |
… | |
468 | struct pollfd pfd; |
516 | struct pollfd pfd; |
469 | |
517 | |
470 | pfd.fd = s_epipe_fd (epp); |
518 | pfd.fd = s_epipe_fd (epp); |
471 | pfd.events = POLLIN; |
519 | pfd.events = POLLIN; |
472 | |
520 | |
473 | return poll (&pfd, 1, 0); |
521 | return poll (&pfd, 1, -1); |
474 | #endif |
522 | #endif |
475 | } |
523 | } |
476 | |
524 | |
477 | #endif |
525 | #endif |
478 | |
526 | |