1 |
/* GetCurrentProcessId is XP and up, which means in all supported versions */ |
2 |
/* but older SDK's might need this */ |
3 |
#define _WIN32_WINNT NTDDI_WINXP |
4 |
|
5 |
#ifdef __sun |
6 |
#define _XOPEN_SOURCE 1 |
7 |
#define _XOPEN_SOURCE_EXTENDED 1 |
8 |
#define __EXTENSIONS__ 1 |
9 |
#endif |
10 |
|
11 |
#include "EXTERN.h" |
12 |
#include "perl.h" |
13 |
#include "XSUB.h" |
14 |
|
15 |
#if WIN32 |
16 |
|
17 |
/* perl probably did this already */ |
18 |
#include <windows.h> |
19 |
|
20 |
#elif __CYGWIN__ |
21 |
|
22 |
#include <windows.h> |
23 |
#include <io.h> |
24 |
#include <sys/cygwin.h> |
25 |
|
26 |
#define ioctlsocket(a,b,c) ioctl (a, b, c) |
27 |
#define _open_osfhandle(h,m) cygwin_attach_handle_to_fd ("/dev/pipe", -1, (HANDLE)h, 1, GENERIC_READ | GENERIC_WRITE) |
28 |
typedef int SOCKET; |
29 |
|
30 |
#else |
31 |
|
32 |
#include <stddef.h> // needed by broken bsds for NULL used in sys/uio.h |
33 |
#include <stdlib.h> |
34 |
#include <errno.h> |
35 |
|
36 |
/* send_fd/recv_fd taken from libptytty */ |
37 |
#include <sys/types.h> |
38 |
#include <sys/uio.h> |
39 |
#include <sys/socket.h> |
40 |
|
41 |
#ifndef CMSG_SPACE |
42 |
# define CMSG_SPACE(len) (sizeof (cmsghdr) + len) |
43 |
#endif |
44 |
|
45 |
#ifndef CMSG_LEN |
46 |
# define CMSG_LEN(len) (sizeof (cmsghdr) + len) |
47 |
#endif |
48 |
|
49 |
#endif |
50 |
|
51 |
#if defined(WIN32) |
52 |
/* the rub is this: win32 doesn't seem to have a way to query whether a socket */ |
53 |
/* is non-blocking or not. so we assume it is blocking, make it so if it isn't */ |
54 |
/* and reset it afterwards */ |
55 |
static int |
56 |
rw (int wr, int fd, char *buf, int len) |
57 |
{ |
58 |
u_long nbio = 0; |
59 |
int got = 0; |
60 |
|
61 |
while (got != len) |
62 |
{ |
63 |
int sze = wr |
64 |
? send ((SOCKET)fd, buf, len) /* we assume send and recv are macros with arguments */ |
65 |
: recv ((SOCKET)fd, buf, len); /* to be on the safe side */ |
66 |
|
67 |
if (sze <= 0) |
68 |
{ |
69 |
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == WSAWOULDBLOCK) |
70 |
{ |
71 |
ioctlsocket (s, FIONBIO, &nbio); |
72 |
nbio = 1; |
73 |
} |
74 |
else |
75 |
break; |
76 |
} |
77 |
else |
78 |
got += sze; |
79 |
} |
80 |
|
81 |
if (nbio) |
82 |
ioctlsocket (s, FIONBIO, &nbio); |
83 |
|
84 |
return got == len; |
85 |
} |
86 |
#endif |
87 |
|
88 |
static int |
89 |
fd_send (int socket, int fd) |
90 |
{ |
91 |
#if defined(WIN32) |
92 |
DWORD pid; |
93 |
HANDLE target, h; |
94 |
|
95 |
/* seriously, there is no way to query whether a socket is non-blocking?? */ |
96 |
if (rw (0, socket, (char *)&pid, sizeof (pid), 0) != sizeof (pid)) |
97 |
return 0; |
98 |
|
99 |
target = OpenProcess (PROCESS_DUP_HANDLE, FALSE, pid); |
100 |
if (!target) |
101 |
croak ("AnyEvent::ProcessPool::fd_recv: OpenProcess failed"); |
102 |
|
103 |
if (!DuplicateHandle ((HANDLE)-1, (HANDLE)_get_osfhandle (fd), target, &h, 0, FALSE, DUPLICATE_SAME_ACCESS)) |
104 |
croak ("AnyEvent::ProcessPool::fd_recv: DuplicateHandle failed"); |
105 |
|
106 |
CloseHandle (target); |
107 |
|
108 |
if (rw (1, socket, (char *)&h, sizeof (h), 0) != sizeof (h)) |
109 |
return 0; |
110 |
|
111 |
return 1; |
112 |
|
113 |
#else |
114 |
void *buf = malloc (CMSG_SPACE (sizeof (int))); |
115 |
|
116 |
if (!buf) |
117 |
return 0; |
118 |
|
119 |
struct msghdr msg; |
120 |
struct iovec iov; |
121 |
struct cmsghdr *cmsg; |
122 |
char data = 0; |
123 |
|
124 |
iov.iov_base = &data; |
125 |
iov.iov_len = 1; |
126 |
|
127 |
msg.msg_name = 0; |
128 |
msg.msg_namelen = 0; |
129 |
msg.msg_iov = &iov; |
130 |
msg.msg_iovlen = 1; |
131 |
msg.msg_control = buf; |
132 |
msg.msg_controllen = CMSG_SPACE (sizeof (int)); |
133 |
|
134 |
cmsg = CMSG_FIRSTHDR (&msg); |
135 |
cmsg->cmsg_level = SOL_SOCKET; |
136 |
cmsg->cmsg_type = SCM_RIGHTS; |
137 |
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); |
138 |
|
139 |
*(int *)CMSG_DATA (cmsg) = fd; |
140 |
|
141 |
ssize_t result = sendmsg (socket, &msg, 0); |
142 |
|
143 |
free (buf); |
144 |
|
145 |
return result >= 0; |
146 |
#endif |
147 |
} |
148 |
|
149 |
static int |
150 |
fd_recv (int socket) |
151 |
{ |
152 |
#if defined(WIN32) |
153 |
DWORD pid = GetCurrentProcessId (); |
154 |
HANDLE h; |
155 |
|
156 |
if (rw (1, socket, (char *)&pid, sizeof (pid), 0) != sizeof (pid)) |
157 |
return -1; |
158 |
|
159 |
if (rw (0, socket, (char *)&h, sizeof (h), 0) != sizeof (h)) |
160 |
return -1; |
161 |
|
162 |
return _open_osfhandle ((intptr_t)h, 0); |
163 |
#else |
164 |
void *buf = malloc (CMSG_SPACE (sizeof (int))); |
165 |
|
166 |
if (!buf) |
167 |
return -1; |
168 |
|
169 |
struct msghdr msg; |
170 |
struct iovec iov; |
171 |
char data = 1; |
172 |
|
173 |
iov.iov_base = &data; |
174 |
iov.iov_len = 1; |
175 |
|
176 |
msg.msg_name = 0; |
177 |
msg.msg_namelen = 0; |
178 |
msg.msg_iov = &iov; |
179 |
msg.msg_iovlen = 1; |
180 |
msg.msg_control = buf; |
181 |
msg.msg_controllen = CMSG_SPACE (sizeof (int)); |
182 |
|
183 |
if (recvmsg (socket, &msg, 0) <= 0) |
184 |
return -1; |
185 |
|
186 |
int fd = -1; |
187 |
|
188 |
errno = EDOM; |
189 |
|
190 |
if ( |
191 |
data == 0 |
192 |
#if __OpenBSD__ |
193 |
&& msg.msg_controllen >= CMSG_LEN (sizeof (int)) /* work around a bug in at least openbsd 4.5 and 4.8 */ |
194 |
#else |
195 |
&& msg.msg_controllen >= CMSG_SPACE (sizeof (int)) |
196 |
#endif |
197 |
) { |
198 |
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); |
199 |
|
200 |
if (cmsg->cmsg_level == SOL_SOCKET |
201 |
&& cmsg->cmsg_type == SCM_RIGHTS |
202 |
&& cmsg->cmsg_len >= CMSG_LEN (sizeof (int))) |
203 |
fd = *(int *)CMSG_DATA (cmsg); |
204 |
} |
205 |
|
206 |
free (buf); |
207 |
|
208 |
return fd; |
209 |
#endif |
210 |
} |
211 |
|
212 |
MODULE = IO::FDPass PACKAGE = IO::FDPass PREFIX = fd_ |
213 |
|
214 |
PROTOTYPES: DISABLE |
215 |
|
216 |
int |
217 |
fd_send (int socket, int fd) |
218 |
|
219 |
int |
220 |
fd_recv (int socket) |
221 |
|