… | |
… | |
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 | */ |
6 | */ |
|
|
7 | |
|
|
8 | #include <signal.h> |
|
|
9 | #include <errno.h> |
|
|
10 | |
|
|
11 | #if defined(WIN32 ) || defined(_MINIX) |
|
|
12 | # define SCHMORP_H_PREFER_SELECT 1 |
|
|
13 | #endif |
|
|
14 | |
|
|
15 | #if !SCHMORP_H_PREFER_SELECT |
|
|
16 | # include <poll.h> |
|
|
17 | #endif |
7 | |
18 | |
8 | /* useful stuff, used by schmorp mostly */ |
19 | /* useful stuff, used by schmorp mostly */ |
9 | |
20 | |
10 | #include "patchlevel.h" |
21 | #include "patchlevel.h" |
11 | |
22 | |
… | |
… | |
13 | (PERL_REVISION > (a) \ |
24 | (PERL_REVISION > (a) \ |
14 | || (PERL_REVISION == (a) \ |
25 | || (PERL_REVISION == (a) \ |
15 | && (PERL_VERSION > (b) \ |
26 | && (PERL_VERSION > (b) \ |
16 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
27 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
17 | |
28 | |
|
|
29 | #ifndef PERL_MAGIC_ext |
|
|
30 | # define PERL_MAGIC_ext '~' |
|
|
31 | #endif |
|
|
32 | |
18 | #if !PERL_VERSION_ATLEAST (5,6,0) |
33 | #if !PERL_VERSION_ATLEAST (5,6,0) |
19 | # ifndef PL_ppaddr |
34 | # ifndef PL_ppaddr |
20 | # define PL_ppaddr ppaddr |
35 | # define PL_ppaddr ppaddr |
21 | # endif |
36 | # endif |
22 | # ifndef call_sv |
37 | # ifndef call_sv |
… | |
… | |
187 | S_GENSUB_ARG = arg; |
202 | S_GENSUB_ARG = arg; |
188 | |
203 | |
189 | return newRV_noinc ((SV *)cv); |
204 | return newRV_noinc ((SV *)cv); |
190 | } |
205 | } |
191 | |
206 | |
|
|
207 | /*****************************************************************************/ |
192 | /** portable pipe/socketpair */ |
208 | /* portable pipe/socketpair */ |
193 | |
209 | |
194 | #ifdef USE_SOCKETS_AS_HANDLES |
210 | #ifdef USE_SOCKETS_AS_HANDLES |
195 | # define S_TO_SOCKET(x) (win32_get_osfhandle (x)) |
211 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
196 | #else |
212 | #else |
197 | # define S_TO_SOCKET(x) (x) |
213 | # define S_TO_HANDLE(x) ((HANDLE)x) |
198 | #endif |
214 | #endif |
199 | |
215 | |
200 | #ifdef _WIN32 |
216 | #ifdef _WIN32 |
201 | /* taken almost verbatim from libev's ev_win32.c */ |
217 | /* taken almost verbatim from libev's ev_win32.c */ |
202 | /* oh, the humanity! */ |
218 | /* oh, the humanity! */ |
203 | static int |
219 | static int |
204 | s_pipe (int filedes [2]) |
220 | s_pipe (int filedes [2]) |
205 | { |
221 | { |
|
|
222 | dTHX; |
|
|
223 | |
206 | struct sockaddr_in addr = { 0 }; |
224 | struct sockaddr_in addr = { 0 }; |
207 | int addr_size = sizeof (addr); |
225 | int addr_size = sizeof (addr); |
208 | struct sockaddr_in adr2; |
226 | struct sockaddr_in adr2; |
209 | int adr2_size = sizeof (adr2); |
227 | int adr2_size = sizeof (adr2); |
210 | SOCKET listener; |
228 | SOCKET listener; |
… | |
… | |
282 | return -1; |
300 | return -1; |
283 | } |
301 | } |
284 | |
302 | |
285 | #define s_socketpair(domain,type,protocol,filedes) s_pipe (filedes) |
303 | #define s_socketpair(domain,type,protocol,filedes) s_pipe (filedes) |
286 | |
304 | |
|
|
305 | static int |
|
|
306 | s_fd_blocking (int fd, int blocking) |
|
|
307 | { |
|
|
308 | u_long nonblocking = !blocking; |
|
|
309 | |
|
|
310 | return ioctlsocket ((SOCKET)S_TO_HANDLE (fd), FIONBIO, &nonblocking); |
|
|
311 | } |
|
|
312 | |
|
|
313 | #define s_fd_prepare(fd) s_fd_blocking (fd, 0) |
|
|
314 | |
287 | #else |
315 | #else |
288 | |
316 | |
289 | #define s_socketpair(domain,type,protocol,filedes) socketpair (domain, type, protocol, filedes) |
317 | #define s_socketpair(domain,type,protocol,filedes) socketpair (domain, type, protocol, filedes) |
290 | #define s_pipe(filedes) pipe (filedes) |
318 | #define s_pipe(filedes) pipe (filedes) |
291 | |
319 | |
|
|
320 | static int |
|
|
321 | s_fd_blocking (int fd, int blocking) |
|
|
322 | { |
|
|
323 | return fcntl (fd, F_SETFL, blocking ? 0 : O_NONBLOCK); |
|
|
324 | } |
|
|
325 | |
|
|
326 | static int |
|
|
327 | s_fd_prepare (int fd) |
|
|
328 | { |
|
|
329 | return s_fd_blocking (fd, 0) |
|
|
330 | || fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
331 | } |
|
|
332 | |
|
|
333 | #endif |
|
|
334 | |
|
|
335 | #if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
|
|
336 | # define SCHMORP_H_HAVE_EVENTFD 1 |
|
|
337 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
|
|
338 | # include <stdint.h> |
|
|
339 | # ifdef __cplusplus |
|
|
340 | extern "C" { |
292 | #endif |
341 | # endif |
293 | |
342 | int eventfd (unsigned int initval, int flags); |
|
|
343 | # ifdef __cplusplus |
|
|
344 | } |
294 | #endif |
345 | # endif |
|
|
346 | #else |
|
|
347 | # define eventfd(initval,flags) -1 |
|
|
348 | #endif |
295 | |
349 | |
|
|
350 | typedef struct { |
|
|
351 | int fd[2]; /* read, write fd, might be equal */ |
|
|
352 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
|
|
353 | } s_epipe; |
|
|
354 | |
|
|
355 | static int |
|
|
356 | s_epipe_new (s_epipe *epp) |
|
|
357 | { |
|
|
358 | s_epipe ep; |
|
|
359 | |
|
|
360 | ep.fd [0] = ep.fd [1] = eventfd (0, 0); |
|
|
361 | |
|
|
362 | if (ep.fd [0] >= 0) |
|
|
363 | { |
|
|
364 | s_fd_prepare (ep.fd [0]); |
|
|
365 | ep.len = 8; |
|
|
366 | } |
|
|
367 | else |
|
|
368 | { |
|
|
369 | if (s_pipe (ep.fd)) |
|
|
370 | return -1; |
|
|
371 | |
|
|
372 | if (s_fd_prepare (ep.fd [0]) |
|
|
373 | || s_fd_prepare (ep.fd [1])) |
|
|
374 | { |
|
|
375 | dTHX; |
|
|
376 | |
|
|
377 | close (ep.fd [0]); |
|
|
378 | close (ep.fd [1]); |
|
|
379 | return -1; |
|
|
380 | } |
|
|
381 | |
|
|
382 | ep.len = 1; |
|
|
383 | } |
|
|
384 | |
|
|
385 | *epp = ep; |
|
|
386 | return 0; |
|
|
387 | } |
|
|
388 | |
|
|
389 | static void |
|
|
390 | s_epipe_destroy (s_epipe *epp) |
|
|
391 | { |
|
|
392 | dTHX; |
|
|
393 | |
|
|
394 | close (epp->fd [0]); |
|
|
395 | |
|
|
396 | if (epp->fd [1] != epp->fd [0]) |
|
|
397 | close (epp->fd [1]); |
|
|
398 | |
|
|
399 | epp->len = 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | static void |
|
|
403 | s_epipe_signal (s_epipe *epp) |
|
|
404 | { |
|
|
405 | #ifdef _WIN32 |
|
|
406 | /* perl overrides send with a function that crashes in other threads. |
|
|
407 | * unfortunately, it overrides it with an argument-less macro, so |
|
|
408 | * there is no way to force usage of the real send function. |
|
|
409 | * incompetent windows programmers - is this redundant? |
|
|
410 | */ |
|
|
411 | DWORD dummy; |
|
|
412 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
|
|
413 | #else |
|
|
414 | # if SCHMORP_H_HAVE_EVENTFD |
|
|
415 | static uint64_t counter = 1; |
|
|
416 | # else |
|
|
417 | static char counter [8]; |
|
|
418 | # endif |
|
|
419 | /* some modules accept fd's from outside, support eventfd here */ |
|
|
420 | if (write (epp->fd [1], &counter, epp->len) < 0 |
|
|
421 | && errno == EINVAL |
|
|
422 | && epp->len != 8) |
|
|
423 | write (epp->fd [1], &counter, (epp->len = 8)); |
|
|
424 | #endif |
|
|
425 | } |
|
|
426 | |
|
|
427 | static void |
|
|
428 | s_epipe_drain (s_epipe *epp) |
|
|
429 | { |
|
|
430 | dTHX; |
|
|
431 | char buf [9]; |
|
|
432 | |
|
|
433 | #ifdef _WIN32 |
|
|
434 | recv (epp->fd [0], buf, sizeof (buf), 0); |
|
|
435 | #else |
|
|
436 | read (epp->fd [0], buf, sizeof (buf)); |
|
|
437 | #endif |
|
|
438 | } |
|
|
439 | |
|
|
440 | /* like new, but dups over old */ |
|
|
441 | static int |
|
|
442 | s_epipe_renew (s_epipe *epp) |
|
|
443 | { |
|
|
444 | dTHX; |
|
|
445 | s_epipe epn; |
|
|
446 | |
|
|
447 | if (epp->fd [1] != epp->fd [0]) |
|
|
448 | close (epp->fd [1]); |
|
|
449 | |
|
|
450 | if (s_epipe_new (&epn)) |
|
|
451 | return -1; |
|
|
452 | |
|
|
453 | if (epp->len) |
|
|
454 | { |
|
|
455 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
|
|
456 | croak ("unable to dup over old event pipe"); /* should not croak */ |
|
|
457 | |
|
|
458 | close (epn.fd [0]); |
|
|
459 | |
|
|
460 | if (epn.fd [0] == epn.fd [1]) |
|
|
461 | epn.fd [1] = epp->fd [0]; |
|
|
462 | |
|
|
463 | epn.fd [0] = epp->fd [0]; |
|
|
464 | } |
|
|
465 | |
|
|
466 | *epp = epn; |
|
|
467 | |
|
|
468 | return 0; |
|
|
469 | } |
|
|
470 | |
|
|
471 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
|
|
472 | |
|
|
473 | static int |
|
|
474 | s_epipe_wait (s_epipe *epp) |
|
|
475 | { |
|
|
476 | dTHX; |
|
|
477 | #if SCHMORP_H_PREFER_SELECT |
|
|
478 | fd_set rfd; |
|
|
479 | int fd = s_epipe_fd (epp); |
|
|
480 | |
|
|
481 | FD_ZERO (&rfd); |
|
|
482 | FD_SET (fd, &rfd); |
|
|
483 | |
|
|
484 | return PerlSock_select (fd + 1, &rfd, 0, 0, 0); |
|
|
485 | #else |
|
|
486 | /* poll is preferable on posix systems */ |
|
|
487 | struct pollfd pfd; |
|
|
488 | |
|
|
489 | pfd.fd = s_epipe_fd (epp); |
|
|
490 | pfd.events = POLLIN; |
|
|
491 | |
|
|
492 | return poll (&pfd, 1, -1); |
|
|
493 | #endif |
|
|
494 | } |
|
|
495 | |
|
|
496 | #endif |
|
|
497 | |