Bring back ext2 code from branch
[reactos.git] / reactos / dll / win32 / rpcrt4 / rpcrt4_ros.diff
1 Index: rpc_server.c
2 ===================================================================
3 --- rpc_server.c (working copy)
4 +++ rpc_server.c (working copy)
5 @@ -1017,22 +1017,30 @@
6 /***********************************************************************
7 * RpcMgmtServerWaitListen (RPCRT4.@)
8 */
9 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
10 {
11 - TRACE("()\n");
12 + RpcServerProtseq *cps;
13
14 EnterCriticalSection(&listen_cs);
15
16 if (!std_listen) {
17 LeaveCriticalSection(&listen_cs);
18 return RPC_S_NOT_LISTENING;
19 }
20
21 + do
22 + {
23 LeaveCriticalSection(&listen_cs);
24
25 - FIXME("not waiting for server calls to finish\n");
26 + LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
27 + WaitForSingleObject(cps->server_ready_event, INFINITE);
28 +
29 + EnterCriticalSection(&listen_cs);
30 + } while (!std_listen);
31 +
32 + LeaveCriticalSection(&listen_cs);
33
34 return RPC_S_OK;
35 }
36
37 /***********************************************************************
38 Index: rpc_transport.c
39 ===================================================================
40 --- rpc_transport.c (working copy)
41 +++ rpc_transport.c (working copy)
42 @@ -54,10 +54,13 @@
43 #endif
44 #ifdef HAVE_SYS_POLL_H
45 #include <sys/poll.h>
46 #endif
47
48 +#include <winsock2.h>
49 +#include <ws2tcpip.h>
50 +
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnls.h"
54 #include "winerror.h"
55 #include "winternl.h"
56 @@ -71,10 +74,12 @@
57 #include "rpc_binding.h"
58 #include "rpc_message.h"
59 #include "rpc_server.h"
60 #include "epm_towers.h"
61
62 +#include "unix_func.h"
63 +
64 #ifndef SOL_TCP
65 # define SOL_TCP IPPROTO_TCP
66 #endif
67
68 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
69 @@ -83,11 +88,11 @@
70
71 typedef struct _RpcConnection_np
72 {
73 RpcConnection common;
74 HANDLE pipe;
75 - OVERLAPPED ovl;
76 + OVERLAPPED ovl[2];
77 BOOL listening;
78 } RpcConnection_np;
79
80 static RpcConnection *rpcrt4_conn_np_alloc(void)
81 {
82 @@ -105,15 +110,15 @@
83 {
84 if (npc->listening)
85 return RPC_S_OK;
86
87 npc->listening = TRUE;
88 - if (ConnectNamedPipe(npc->pipe, &npc->ovl))
89 + if (ConnectNamedPipe(npc->pipe, &npc->ovl[0]))
90 return RPC_S_OK;
91
92 if (GetLastError() == ERROR_PIPE_CONNECTED) {
93 - SetEvent(npc->ovl.hEvent);
94 + SetEvent(npc->ovl[0].hEvent);
95 return RPC_S_OK;
96 }
97 if (GetLastError() == ERROR_IO_PENDING) {
98 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
99 return RPC_S_OK;
100 @@ -126,11 +131,11 @@
101 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
102 {
103 RpcConnection_np *npc = (RpcConnection_np *) Connection;
104 TRACE("listening on %s\n", pname);
105
106 - npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
107 + npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
108 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
109 PIPE_UNLIMITED_INSTANCES,
110 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
111 if (npc->pipe == INVALID_HANDLE_VALUE) {
112 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
113 @@ -139,11 +144,12 @@
114 else
115 return RPC_S_CANT_CREATE_ENDPOINT;
116 }
117
118 memset(&npc->ovl, 0, sizeof(npc->ovl));
119 - npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
120 + npc->ovl[0].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
121 + npc->ovl[1].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
122
123 /* Note: we don't call ConnectNamedPipe here because it must be done in the
124 * server thread as the thread must be alertable */
125 return RPC_S_OK;
126 }
127 @@ -202,11 +208,13 @@
128 /* success */
129 memset(&npc->ovl, 0, sizeof(npc->ovl));
130 /* pipe is connected; change to message-read mode. */
131 dwMode = PIPE_READMODE_MESSAGE;
132 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
133 - npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
134 + npc->ovl[0].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
135 + npc->ovl[1].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
136 +
137 npc->pipe = pipe;
138
139 return RPC_S_OK;
140 }
141
142 @@ -308,11 +316,12 @@
143 {
144 /* because of the way named pipes work, we'll transfer the connected pipe
145 * to the child, then reopen the server binding to continue listening */
146
147 new_npc->pipe = old_npc->pipe;
148 - new_npc->ovl = old_npc->ovl;
149 + new_npc->ovl[0] = old_npc->ovl[0];
150 + new_npc->ovl[1] = old_npc->ovl[1];
151 old_npc->pipe = 0;
152 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
153 old_npc->listening = FALSE;
154 }
155
156 @@ -359,13 +368,18 @@
157 unsigned int bytes_left = count;
158
159 while (bytes_left)
160 {
161 DWORD bytes_read;
162 - ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
163 - if (!ret || !bytes_read)
164 + ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, &npc->ovl[0]);
165 + if ((!ret || !bytes_read) && (GetLastError() != ERROR_IO_PENDING))
166 break;
167 +
168 + ret = GetOverlappedResult(npc->pipe, &npc->ovl[0], &bytes_read, TRUE);
169 + if (!ret && GetLastError() != ERROR_MORE_DATA)
170 + break;
171 +
172 bytes_left -= bytes_read;
173 buf += bytes_read;
174 }
175 return ret ? count : -1;
176 }
177 @@ -379,13 +393,18 @@
178 unsigned int bytes_left = count;
179
180 while (bytes_left)
181 {
182 DWORD bytes_written;
183 - ret = WriteFile(npc->pipe, buf, count, &bytes_written, NULL);
184 - if (!ret || !bytes_written)
185 + ret = WriteFile(npc->pipe, buf, count, &bytes_written, &npc->ovl[1]);
186 + if ((!ret || !bytes_written) && (GetLastError() != ERROR_IO_PENDING))
187 break;
188 +
189 + ret = GetOverlappedResult(npc->pipe, &npc->ovl[1], &bytes_written, TRUE);
190 + if (!ret && GetLastError() != ERROR_MORE_DATA)
191 + break;
192 +
193 bytes_left -= bytes_written;
194 buf += bytes_written;
195 }
196 return ret ? count : -1;
197 }
198 @@ -396,14 +415,19 @@
199 if (npc->pipe) {
200 FlushFileBuffers(npc->pipe);
201 CloseHandle(npc->pipe);
202 npc->pipe = 0;
203 }
204 - if (npc->ovl.hEvent) {
205 - CloseHandle(npc->ovl.hEvent);
206 - npc->ovl.hEvent = 0;
207 + if (npc->ovl[0].hEvent) {
208 + CloseHandle(npc->ovl[0].hEvent);
209 + npc->ovl[0].hEvent = 0;
210 + }
211 + if (npc->ovl[1].hEvent) {
212 + CloseHandle(npc->ovl[1].hEvent);
213 + npc->ovl[1].hEvent = 0;
214 }
215 +
216 return 0;
217 }
218
219 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
220 {
221 @@ -547,11 +571,11 @@
222 /* open and count connections */
223 *count = 1;
224 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
225 while (conn) {
226 rpcrt4_conn_listen_pipe(conn);
227 - if (conn->ovl.hEvent)
228 + if (conn->ovl[0].hEvent)
229 (*count)++;
230 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
231 }
232
233 /* make array of connections */
234 @@ -568,11 +592,11 @@
235
236 objs[0] = npps->mgr_event;
237 *count = 1;
238 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
239 while (conn) {
240 - if ((objs[*count] = conn->ovl.hEvent))
241 + if ((objs[*count] = conn->ovl[0].hEvent))
242 (*count)++;
243 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
244 }
245 LeaveCriticalSection(&protseq->cs);
246 return objs;
247 @@ -615,11 +639,11 @@
248 b_handle = objs[res - WAIT_OBJECT_0];
249 /* find which connection got a RPC */
250 EnterCriticalSection(&protseq->cs);
251 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
252 while (conn) {
253 - if (b_handle == conn->ovl.hEvent) break;
254 + if (b_handle == conn->ovl[0].hEvent) break;
255 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
256 }
257 cconn = NULL;
258 if (conn)
259 RPCRT4_SpawnConnection(&cconn, &conn->common);
260 @@ -713,16 +737,18 @@
261 RpcConnection_tcp *tcpc;
262 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
263 if (tcpc == NULL)
264 return NULL;
265 tcpc->sock = -1;
266 +#ifndef __REACTOS__
267 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
268 {
269 ERR("socketpair() failed: %s\n", strerror(errno));
270 HeapFree(GetProcessHeap(), 0, tcpc);
271 return NULL;
272 }
273 +#endif
274 return &tcpc->common;
275 }
276
277 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
278 {
279 @@ -783,12 +809,11 @@
280 continue;
281 }
282
283 /* RPC depends on having minimal latency so disable the Nagle algorithm */
284 val = 1;
285 - setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
286 - fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
287 + setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
288
289 tcpc->sock = sock;
290
291 freeaddrinfo(ai);
292 TRACE("connected\n");
293 @@ -806,10 +831,11 @@
294 int sock;
295 int ret;
296 struct addrinfo *ai;
297 struct addrinfo *ai_cur;
298 struct addrinfo hints;
299 + u_long blocking;
300 RpcConnection *first_connection = NULL;
301
302 TRACE("(%p, %s)\n", protseq, endpoint);
303
304 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
305 @@ -857,11 +883,11 @@
306 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
307 if (ret < 0)
308 {
309 WARN("bind failed: %s\n", strerror(errno));
310 close(sock);
311 - if (errno == EADDRINUSE)
312 + if (errno == WSAEADDRINUSE)
313 status = RPC_S_DUPLICATE_ENDPOINT;
314 else
315 status = RPC_S_CANT_CREATE_ENDPOINT;
316 continue;
317 }
318 @@ -886,11 +912,12 @@
319 }
320 /* need a non-blocking socket, otherwise accept() has a potential
321 * race-condition (poll() says it is readable, connection drops,
322 * and accept() blocks until the next connection comes...)
323 */
324 - ret = fcntl(sock, F_SETFL, O_NONBLOCK);
325 + blocking = 1;
326 + ret = ioctlsocket(sock, FIONBIO, &blocking);
327 if (ret < 0)
328 {
329 WARN("couldn't make socket non-blocking, error %d\n", ret);
330 RPCRT4_DestroyConnection(&tcpc->common);
331 status = RPC_S_OUT_OF_RESOURCES;
332 @@ -929,10 +956,11 @@
333 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
334 {
335 int ret;
336 struct sockaddr_in address;
337 socklen_t addrsize;
338 + u_long blocking;
339 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
340 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
341
342 addrsize = sizeof(address);
343 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
344 @@ -940,11 +968,12 @@
345 {
346 ERR("Failed to accept a TCP connection: error %d\n", ret);
347 return RPC_S_OUT_OF_RESOURCES;
348 }
349 /* reset to blocking behaviour */
350 - fcntl(ret, F_SETFL, 0);
351 + blocking = 0;
352 + ret = ioctlsocket(ret, FIONBIO, &blocking);
353 client->sock = ret;
354 TRACE("Accepted a new TCP connection\n");
355 return RPC_S_OK;
356 }
357
358 @@ -1187,14 +1216,16 @@
359 {
360 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
361 if (ps)
362 {
363 int fds[2];
364 + u_long blocking;
365 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
366 {
367 - fcntl(fds[0], F_SETFL, O_NONBLOCK);
368 - fcntl(fds[1], F_SETFL, O_NONBLOCK);
369 + blocking = 1;
370 + ioctlsocket(fds[0], FIONBIO, &blocking);
371 + ioctlsocket(fds[1], FIONBIO, &blocking);
372 ps->mgr_event_rcv = fds[0];
373 ps->mgr_event_snd = fds[1];
374 }
375 else
376 {