1 | /* $Id: localipc-win.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Local IPC, Windows Implementation Using Named Pipes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_LOCALIPC
|
---|
32 | /*
|
---|
33 | * We have to force NT 5.0 here because of
|
---|
34 | * ConvertStringSecurityDescriptorToSecurityDescriptor. Note that because of
|
---|
35 | * FILE_FLAG_FIRST_PIPE_INSTANCE this code actually requires W2K SP2+.
|
---|
36 | */
|
---|
37 | #ifndef _WIN32_WINNT
|
---|
38 | # define _WIN32_WINNT 0x0500 /* for ConvertStringSecurityDescriptorToSecurityDescriptor */
|
---|
39 | #elif _WIN32_WINNT < 0x0500
|
---|
40 | # undef _WIN32_WINNT
|
---|
41 | # define _WIN32_WINNT 0x0500
|
---|
42 | #endif
|
---|
43 | #define UNICODE /* For the SDDL_ strings. */
|
---|
44 | #include <Windows.h>
|
---|
45 | #include <sddl.h>
|
---|
46 |
|
---|
47 | #include "internal/iprt.h"
|
---|
48 | #include <iprt/localipc.h>
|
---|
49 |
|
---|
50 | #include <iprt/asm.h>
|
---|
51 | #include <iprt/assert.h>
|
---|
52 | #include <iprt/critsect.h>
|
---|
53 | #include <iprt/ctype.h>
|
---|
54 | #include <iprt/err.h>
|
---|
55 | #include <iprt/ldr.h>
|
---|
56 | #include <iprt/log.h>
|
---|
57 | #include <iprt/mem.h>
|
---|
58 | #include <iprt/param.h>
|
---|
59 | #include <iprt/string.h>
|
---|
60 | #include <iprt/thread.h>
|
---|
61 | #include <iprt/time.h>
|
---|
62 |
|
---|
63 | #include "internal/magics.h"
|
---|
64 | #include "internal-r3-win.h"
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | /*********************************************************************************************************************************
|
---|
69 | * Defined Constants And Macros *
|
---|
70 | *********************************************************************************************************************************/
|
---|
71 | /** Pipe prefix string. */
|
---|
72 | #define RTLOCALIPC_WIN_PREFIX L"\\\\.\\pipe\\IPRT-"
|
---|
73 |
|
---|
74 | /** DACL for block all network access and local users other than the creator/owner.
|
---|
75 | *
|
---|
76 | * ACE format: (ace_type;ace_flags;rights;object_guid;inherit_object_guid;account_sid)
|
---|
77 | *
|
---|
78 | * Note! FILE_GENERIC_WRITE (SDDL_FILE_WRITE) is evil here because it includes
|
---|
79 | * the FILE_CREATE_PIPE_INSTANCE(=FILE_APPEND_DATA) flag. Thus the hardcoded
|
---|
80 | * value 0x0012019b in the client ACE. The server-side still needs
|
---|
81 | * setting FILE_CREATE_PIPE_INSTANCE although.
|
---|
82 | * It expands to:
|
---|
83 | * 0x00000001 - FILE_READ_DATA
|
---|
84 | * 0x00000008 - FILE_READ_EA
|
---|
85 | * 0x00000080 - FILE_READ_ATTRIBUTES
|
---|
86 | * 0x00020000 - READ_CONTROL
|
---|
87 | * 0x00100000 - SYNCHRONIZE
|
---|
88 | * 0x00000002 - FILE_WRITE_DATA
|
---|
89 | * 0x00000010 - FILE_WRITE_EA
|
---|
90 | * 0x00000100 - FILE_WRITE_ATTRIBUTES
|
---|
91 | * = 0x0012019b (client)
|
---|
92 | * + (only for server):
|
---|
93 | * 0x00000004 - FILE_CREATE_PIPE_INSTANCE
|
---|
94 | * = 0x0012019f
|
---|
95 | *
|
---|
96 | * @todo Triple check this!
|
---|
97 | * @todo EVERYONE -> AUTHENTICATED USERS or something more appropriate?
|
---|
98 | * @todo Have trouble allowing the owner FILE_CREATE_PIPE_INSTANCE access, so for now I'm hacking
|
---|
99 | * it just to get progress - the service runs as local system.
|
---|
100 | * The CREATOR OWNER and PERSONAL SELF works (the former is only involved in inheriting
|
---|
101 | * it seems, which is why it won't work. The latter I've no idea about. Perhaps the solution
|
---|
102 | * is to go the annoying route of OpenProcessToken, QueryTokenInformation,
|
---|
103 | * ConvertSidToStringSid and then use the result... Suggestions are very welcome
|
---|
104 | */
|
---|
105 | #define RTLOCALIPC_WIN_SDDL_BASE \
|
---|
106 | SDDL_DACL SDDL_DELIMINATOR \
|
---|
107 | SDDL_ACE_BEGIN SDDL_ACCESS_DENIED L";;" SDDL_GENERIC_ALL L";;;" SDDL_NETWORK SDDL_ACE_END \
|
---|
108 | SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" SDDL_FILE_ALL L";;;" SDDL_LOCAL_SYSTEM SDDL_ACE_END
|
---|
109 |
|
---|
110 | #define RTLOCALIPC_WIN_SDDL_SERVER \
|
---|
111 | RTLOCALIPC_WIN_SDDL_BASE \
|
---|
112 | SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" L"0x0012019f" L";;;" SDDL_EVERYONE SDDL_ACE_END
|
---|
113 |
|
---|
114 | #define RTLOCALIPC_WIN_SDDL_CLIENT \
|
---|
115 | RTLOCALIPC_WIN_SDDL_BASE \
|
---|
116 | SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" L"0x0012019b" L";;;" SDDL_EVERYONE SDDL_ACE_END
|
---|
117 |
|
---|
118 | // SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" SDDL_GENERIC_ALL L";;;" SDDL_PERSONAL_SELF SDDL_ACE_END \
|
---|
119 | // SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";CIOI;" SDDL_GENERIC_ALL L";;;" SDDL_CREATOR_OWNER SDDL_ACE_END
|
---|
120 | // SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" L"0x0012019b" L";;;" SDDL_EVERYONE SDDL_ACE_END
|
---|
121 | // SDDL_ACE_BEGIN SDDL_ACCESS_ALLOWED L";;" SDDL_FILE_ALL L";;;" SDDL_LOCAL_SYSTEM SDDL_ACE_END
|
---|
122 |
|
---|
123 |
|
---|
124 | /*********************************************************************************************************************************
|
---|
125 | * Structures and Typedefs *
|
---|
126 | *********************************************************************************************************************************/
|
---|
127 | /**
|
---|
128 | * Local IPC service instance, Windows.
|
---|
129 | */
|
---|
130 | typedef struct RTLOCALIPCSERVERINT
|
---|
131 | {
|
---|
132 | /** The magic (RTLOCALIPCSERVER_MAGIC). */
|
---|
133 | uint32_t u32Magic;
|
---|
134 | /** The creation flags. */
|
---|
135 | uint32_t fFlags;
|
---|
136 | /** Critical section protecting the structure. */
|
---|
137 | RTCRITSECT CritSect;
|
---|
138 | /** The number of references to the instance.
|
---|
139 | * @remarks The reference counting isn't race proof. */
|
---|
140 | uint32_t volatile cRefs;
|
---|
141 | /** Indicates that there is a pending cancel request. */
|
---|
142 | bool volatile fCancelled;
|
---|
143 | /** The named pipe handle. */
|
---|
144 | HANDLE hNmPipe;
|
---|
145 | /** The handle to the event object we're using for overlapped I/O. */
|
---|
146 | HANDLE hEvent;
|
---|
147 | /** The overlapped I/O structure. */
|
---|
148 | OVERLAPPED OverlappedIO;
|
---|
149 | /** The full pipe name (variable length). */
|
---|
150 | RTUTF16 wszName[1];
|
---|
151 | } RTLOCALIPCSERVERINT;
|
---|
152 | /** Pointer to a local IPC server instance (Windows). */
|
---|
153 | typedef RTLOCALIPCSERVERINT *PRTLOCALIPCSERVERINT;
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Local IPC session instance, Windows.
|
---|
158 | *
|
---|
159 | * This is a named pipe and we should probably merge the pipe code with this to
|
---|
160 | * save work and code duplication.
|
---|
161 | */
|
---|
162 | typedef struct RTLOCALIPCSESSIONINT
|
---|
163 | {
|
---|
164 | /** The magic (RTLOCALIPCSESSION_MAGIC). */
|
---|
165 | uint32_t u32Magic;
|
---|
166 | /** Critical section protecting the structure. */
|
---|
167 | RTCRITSECT CritSect;
|
---|
168 | /** The number of references to the instance.
|
---|
169 | * @remarks The reference counting isn't race proof. */
|
---|
170 | uint32_t volatile cRefs;
|
---|
171 | /** Set if the zero byte read that the poll code using is pending. */
|
---|
172 | bool fZeroByteRead;
|
---|
173 | /** Indicates that there is a pending cancel request. */
|
---|
174 | bool volatile fCancelled;
|
---|
175 | /** Set if this is the server side, clear if the client. */
|
---|
176 | bool fServerSide;
|
---|
177 | /** The named pipe handle. */
|
---|
178 | HANDLE hNmPipe;
|
---|
179 | struct
|
---|
180 | {
|
---|
181 | RTTHREAD hActiveThread;
|
---|
182 | /** The handle to the event object we're using for overlapped I/O. */
|
---|
183 | HANDLE hEvent;
|
---|
184 | /** The overlapped I/O structure. */
|
---|
185 | OVERLAPPED OverlappedIO;
|
---|
186 | }
|
---|
187 | /** Overlapped reads. */
|
---|
188 | Read,
|
---|
189 | /** Overlapped writes. */
|
---|
190 | Write;
|
---|
191 | #if 0 /* Non-blocking writes are not yet supported. */
|
---|
192 | /** Bounce buffer for writes. */
|
---|
193 | uint8_t *pbBounceBuf;
|
---|
194 | /** Amount of used buffer space. */
|
---|
195 | size_t cbBounceBufUsed;
|
---|
196 | /** Amount of allocated buffer space. */
|
---|
197 | size_t cbBounceBufAlloc;
|
---|
198 | #endif
|
---|
199 | /** Buffer for the zero byte read.
|
---|
200 | * Used in RTLocalIpcSessionWaitForData(). */
|
---|
201 | uint8_t abBuf[8];
|
---|
202 | } RTLOCALIPCSESSIONINT;
|
---|
203 | /** Pointer to a local IPC session instance (Windows). */
|
---|
204 | typedef RTLOCALIPCSESSIONINT *PRTLOCALIPCSESSIONINT;
|
---|
205 |
|
---|
206 |
|
---|
207 | /*********************************************************************************************************************************
|
---|
208 | * Internal Functions *
|
---|
209 | *********************************************************************************************************************************/
|
---|
210 | static int rtLocalIpcWinCreateSession(PRTLOCALIPCSESSIONINT *ppSession, HANDLE hNmPipeSession);
|
---|
211 |
|
---|
212 |
|
---|
213 | /*********************************************************************************************************************************
|
---|
214 | * Global Variables *
|
---|
215 | *********************************************************************************************************************************/
|
---|
216 | static bool volatile g_fResolvedApis = false;
|
---|
217 | /** advapi32.dll API ConvertStringSecurityDescriptorToSecurityDescriptorW. */
|
---|
218 | static decltype(ConvertStringSecurityDescriptorToSecurityDescriptorW) *g_pfnSSDLToSecDescW = NULL;
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Builds and allocates the security descriptor required for securing the local pipe.
|
---|
223 | *
|
---|
224 | * @return IPRT status code.
|
---|
225 | * @param ppDesc Where to store the allocated security descriptor on success.
|
---|
226 | * Must be free'd using LocalFree().
|
---|
227 | * @param fServer Whether it's for a server or client instance.
|
---|
228 | */
|
---|
229 | static int rtLocalIpcServerWinAllocSecurityDescriptior(PSECURITY_DESCRIPTOR *ppDesc, bool fServer)
|
---|
230 | {
|
---|
231 | /*
|
---|
232 | * Resolve the API the first time around.
|
---|
233 | */
|
---|
234 | if (!g_fResolvedApis)
|
---|
235 | {
|
---|
236 | g_pfnSSDLToSecDescW = (decltype(g_pfnSSDLToSecDescW))RTLdrGetSystemSymbol("advapi32.dll", "ConvertStringSecurityDescriptorToSecurityDescriptorW");
|
---|
237 | ASMCompilerBarrier();
|
---|
238 | g_fResolvedApis = true;
|
---|
239 | }
|
---|
240 |
|
---|
241 | int rc;
|
---|
242 | PSECURITY_DESCRIPTOR pSecDesc = NULL;
|
---|
243 | if (g_pfnSSDLToSecDescW)
|
---|
244 | {
|
---|
245 | /*
|
---|
246 | * We'll create a security descriptor from a SDDL that denies
|
---|
247 | * access to network clients (this is local IPC after all), it
|
---|
248 | * makes some further restrictions to prevent non-authenticated
|
---|
249 | * users from screwing around.
|
---|
250 | */
|
---|
251 | PCRTUTF16 pwszSDDL = fServer ? RTLOCALIPC_WIN_SDDL_SERVER : RTLOCALIPC_WIN_SDDL_CLIENT;
|
---|
252 | if (g_pfnSSDLToSecDescW(pwszSDDL, SDDL_REVISION_1, &pSecDesc, NULL))
|
---|
253 | {
|
---|
254 | AssertPtr(pSecDesc);
|
---|
255 | *ppDesc = pSecDesc;
|
---|
256 | return VINF_SUCCESS;
|
---|
257 | }
|
---|
258 |
|
---|
259 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
260 | }
|
---|
261 | else
|
---|
262 | {
|
---|
263 | /* Windows OSes < W2K SP2 not supported for now, bail out. */
|
---|
264 | /** @todo Implement me! */
|
---|
265 | rc = VERR_NOT_SUPPORTED;
|
---|
266 | }
|
---|
267 | return rc;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Creates a named pipe instance.
|
---|
273 | *
|
---|
274 | * This is used by both RTLocalIpcServerCreate and RTLocalIpcServerListen.
|
---|
275 | *
|
---|
276 | * @return IPRT status code.
|
---|
277 | * @param phNmPipe Where to store the named pipe handle on success.
|
---|
278 | * This will be set to INVALID_HANDLE_VALUE on failure.
|
---|
279 | * @param pwszPipeName The named pipe name, full, UTF-16 encoded.
|
---|
280 | * @param fFirst Set on the first call (from RTLocalIpcServerCreate),
|
---|
281 | * otherwise clear. Governs the
|
---|
282 | * FILE_FLAG_FIRST_PIPE_INSTANCE flag.
|
---|
283 | */
|
---|
284 | static int rtLocalIpcServerWinCreatePipeInstance(PHANDLE phNmPipe, PCRTUTF16 pwszPipeName, bool fFirst)
|
---|
285 | {
|
---|
286 | *phNmPipe = INVALID_HANDLE_VALUE;
|
---|
287 |
|
---|
288 | PSECURITY_DESCRIPTOR pSecDesc;
|
---|
289 | int rc = rtLocalIpcServerWinAllocSecurityDescriptior(&pSecDesc, fFirst /* Server? */);
|
---|
290 | if (RT_SUCCESS(rc))
|
---|
291 | {
|
---|
292 | SECURITY_ATTRIBUTES SecAttrs;
|
---|
293 | SecAttrs.nLength = sizeof(SECURITY_ATTRIBUTES);
|
---|
294 | SecAttrs.lpSecurityDescriptor = pSecDesc;
|
---|
295 | SecAttrs.bInheritHandle = FALSE;
|
---|
296 |
|
---|
297 | DWORD fOpenMode = PIPE_ACCESS_DUPLEX
|
---|
298 | | PIPE_WAIT
|
---|
299 | | FILE_FLAG_OVERLAPPED;
|
---|
300 | if ( fFirst
|
---|
301 | && ( g_enmWinVer >= kRTWinOSType_XP
|
---|
302 | || ( g_enmWinVer == kRTWinOSType_2K
|
---|
303 | && g_WinOsInfoEx.wServicePackMajor >= 2) ) )
|
---|
304 | fOpenMode |= FILE_FLAG_FIRST_PIPE_INSTANCE; /* Introduced with W2K SP2 */
|
---|
305 |
|
---|
306 | HANDLE hNmPipe = CreateNamedPipeW(pwszPipeName, /* lpName */
|
---|
307 | fOpenMode, /* dwOpenMode */
|
---|
308 | PIPE_TYPE_BYTE, /* dwPipeMode */
|
---|
309 | PIPE_UNLIMITED_INSTANCES, /* nMaxInstances */
|
---|
310 | PAGE_SIZE, /* nOutBufferSize (advisory) */
|
---|
311 | PAGE_SIZE, /* nInBufferSize (ditto) */
|
---|
312 | 30*1000, /* nDefaultTimeOut = 30 sec */
|
---|
313 | &SecAttrs); /* lpSecurityAttributes */
|
---|
314 | LocalFree(pSecDesc);
|
---|
315 | if (hNmPipe != INVALID_HANDLE_VALUE)
|
---|
316 | *phNmPipe = hNmPipe;
|
---|
317 | else
|
---|
318 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
319 | }
|
---|
320 |
|
---|
321 | return rc;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Validates the user specified name.
|
---|
327 | *
|
---|
328 | * @returns IPRT status code.
|
---|
329 | * @param pszName The name to validate.
|
---|
330 | * @param pcwcFullName Where to return the UTF-16 length of the full name.
|
---|
331 | * @param fNative Whether it's a native name or a portable name.
|
---|
332 | */
|
---|
333 | static int rtLocalIpcWinValidateName(const char *pszName, size_t *pcwcFullName, bool fNative)
|
---|
334 | {
|
---|
335 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
336 | AssertReturn(*pszName, VERR_INVALID_NAME);
|
---|
337 |
|
---|
338 | if (!fNative)
|
---|
339 | {
|
---|
340 | size_t cwcName = RT_ELEMENTS(RTLOCALIPC_WIN_PREFIX) - 1;
|
---|
341 | for (;;)
|
---|
342 | {
|
---|
343 | char ch = *pszName++;
|
---|
344 | if (!ch)
|
---|
345 | break;
|
---|
346 | AssertReturn(!RT_C_IS_CNTRL(ch), VERR_INVALID_NAME);
|
---|
347 | AssertReturn((unsigned)ch < 0x80, VERR_INVALID_NAME);
|
---|
348 | AssertReturn(ch != '\\', VERR_INVALID_NAME);
|
---|
349 | AssertReturn(ch != '/', VERR_INVALID_NAME);
|
---|
350 | cwcName++;
|
---|
351 | }
|
---|
352 | *pcwcFullName = cwcName;
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | int rc = RTStrCalcUtf16LenEx(pszName, RTSTR_MAX, pcwcFullName);
|
---|
357 | AssertRCReturn(rc, rc);
|
---|
358 | }
|
---|
359 |
|
---|
360 | return VINF_SUCCESS;
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Constructs the full pipe name as UTF-16.
|
---|
366 | *
|
---|
367 | * @returns IPRT status code.
|
---|
368 | * @param pszName The user supplied name.
|
---|
369 | * @param pwszFullName The output buffer.
|
---|
370 | * @param cwcFullName The output buffer size excluding the terminator.
|
---|
371 | * @param fNative Whether the user supplied name is a native or
|
---|
372 | * portable one.
|
---|
373 | */
|
---|
374 | static int rtLocalIpcWinConstructName(const char *pszName, PRTUTF16 pwszFullName, size_t cwcFullName, bool fNative)
|
---|
375 | {
|
---|
376 | if (!fNative)
|
---|
377 | {
|
---|
378 | static RTUTF16 const s_wszPrefix[] = RTLOCALIPC_WIN_PREFIX;
|
---|
379 | Assert(cwcFullName * sizeof(RTUTF16) > sizeof(s_wszPrefix));
|
---|
380 | memcpy(pwszFullName, s_wszPrefix, sizeof(s_wszPrefix));
|
---|
381 | cwcFullName -= RT_ELEMENTS(s_wszPrefix) - 1;
|
---|
382 | pwszFullName += RT_ELEMENTS(s_wszPrefix) - 1;
|
---|
383 | }
|
---|
384 | return RTStrToUtf16Ex(pszName, RTSTR_MAX, &pwszFullName, cwcFullName + 1, NULL);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | RTDECL(int) RTLocalIpcServerCreate(PRTLOCALIPCSERVER phServer, const char *pszName, uint32_t fFlags)
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * Validate parameters.
|
---|
392 | */
|
---|
393 | AssertPtrReturn(phServer, VERR_INVALID_POINTER);
|
---|
394 | *phServer = NIL_RTLOCALIPCSERVER;
|
---|
395 | AssertReturn(!(fFlags & ~RTLOCALIPC_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
396 | size_t cwcFullName;
|
---|
397 | int rc = rtLocalIpcWinValidateName(pszName, &cwcFullName, RT_BOOL(fFlags & RTLOCALIPC_FLAGS_NATIVE_NAME));
|
---|
398 | if (RT_SUCCESS(rc))
|
---|
399 | {
|
---|
400 | /*
|
---|
401 | * Allocate and initialize the instance data.
|
---|
402 | */
|
---|
403 | size_t cbThis = RT_OFFSETOF(RTLOCALIPCSERVERINT, wszName[cwcFullName + 1]);
|
---|
404 | PRTLOCALIPCSERVERINT pThis = (PRTLOCALIPCSERVERINT)RTMemAllocVar(cbThis);
|
---|
405 | AssertReturn(pThis, VERR_NO_MEMORY);
|
---|
406 |
|
---|
407 | pThis->u32Magic = RTLOCALIPCSERVER_MAGIC;
|
---|
408 | pThis->cRefs = 1; /* the one we return */
|
---|
409 | pThis->fCancelled = false;
|
---|
410 |
|
---|
411 | rc = rtLocalIpcWinConstructName(pszName, pThis->wszName, cwcFullName, RT_BOOL(fFlags & RTLOCALIPC_FLAGS_NATIVE_NAME));
|
---|
412 | if (RT_SUCCESS(rc))
|
---|
413 | {
|
---|
414 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | {
|
---|
417 | pThis->hEvent = CreateEvent(NULL /*lpEventAttributes*/, TRUE /*bManualReset*/,
|
---|
418 | FALSE /*bInitialState*/, NULL /*lpName*/);
|
---|
419 | if (pThis->hEvent != NULL)
|
---|
420 | {
|
---|
421 | RT_ZERO(pThis->OverlappedIO);
|
---|
422 | pThis->OverlappedIO.Internal = STATUS_PENDING;
|
---|
423 | pThis->OverlappedIO.hEvent = pThis->hEvent;
|
---|
424 |
|
---|
425 | rc = rtLocalIpcServerWinCreatePipeInstance(&pThis->hNmPipe, pThis->wszName, true /* fFirst */);
|
---|
426 | if (RT_SUCCESS(rc))
|
---|
427 | {
|
---|
428 | *phServer = pThis;
|
---|
429 | return VINF_SUCCESS;
|
---|
430 | }
|
---|
431 |
|
---|
432 | BOOL fRc = CloseHandle(pThis->hEvent);
|
---|
433 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
434 | }
|
---|
435 | else
|
---|
436 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
437 |
|
---|
438 | int rc2 = RTCritSectDelete(&pThis->CritSect);
|
---|
439 | AssertRC(rc2);
|
---|
440 | }
|
---|
441 | }
|
---|
442 | RTMemFree(pThis);
|
---|
443 | }
|
---|
444 | return rc;
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Retains a reference to the server instance.
|
---|
450 | *
|
---|
451 | * @returns
|
---|
452 | * @param pThis The server instance.
|
---|
453 | */
|
---|
454 | DECLINLINE(void) rtLocalIpcServerRetain(PRTLOCALIPCSERVERINT pThis)
|
---|
455 | {
|
---|
456 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
457 | Assert(cRefs < UINT32_MAX / 2 && cRefs); NOREF(cRefs);
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Call when the reference count reaches 0.
|
---|
463 | *
|
---|
464 | * Caller owns the critsect.
|
---|
465 | *
|
---|
466 | * @returns VINF_OBJECT_DESTROYED
|
---|
467 | * @param pThis The instance to destroy.
|
---|
468 | */
|
---|
469 | DECL_NO_INLINE(static, int) rtLocalIpcServerWinDestroy(PRTLOCALIPCSERVERINT pThis)
|
---|
470 | {
|
---|
471 | Assert(pThis->u32Magic == ~RTLOCALIPCSERVER_MAGIC);
|
---|
472 | pThis->u32Magic = ~RTLOCALIPCSERVER_MAGIC;
|
---|
473 |
|
---|
474 | BOOL fRc = CloseHandle(pThis->hNmPipe);
|
---|
475 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
476 | pThis->hNmPipe = INVALID_HANDLE_VALUE;
|
---|
477 |
|
---|
478 | fRc = CloseHandle(pThis->hEvent);
|
---|
479 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
480 | pThis->hEvent = NULL;
|
---|
481 |
|
---|
482 | RTCritSectLeave(&pThis->CritSect);
|
---|
483 | RTCritSectDelete(&pThis->CritSect);
|
---|
484 |
|
---|
485 | RTMemFree(pThis);
|
---|
486 | return VINF_OBJECT_DESTROYED;
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Server instance destructor.
|
---|
492 | *
|
---|
493 | * @returns VINF_OBJECT_DESTROYED
|
---|
494 | * @param pThis The server instance.
|
---|
495 | */
|
---|
496 | DECL_NO_INLINE(static, int) rtLocalIpcServerDtor(PRTLOCALIPCSERVERINT pThis)
|
---|
497 | {
|
---|
498 | RTCritSectEnter(&pThis->CritSect);
|
---|
499 | return rtLocalIpcServerWinDestroy(pThis);
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Releases a reference to the server instance.
|
---|
505 | *
|
---|
506 | * @returns VINF_SUCCESS if only release, VINF_OBJECT_DESTROYED if destroyed.
|
---|
507 | * @param pThis The server instance.
|
---|
508 | */
|
---|
509 | DECLINLINE(int) rtLocalIpcServerRelease(PRTLOCALIPCSERVERINT pThis)
|
---|
510 | {
|
---|
511 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
512 | Assert(cRefs < UINT32_MAX / 2);
|
---|
513 | if (!cRefs)
|
---|
514 | return rtLocalIpcServerDtor(pThis);
|
---|
515 | return VINF_SUCCESS;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Releases a reference to the server instance and leaves the critsect.
|
---|
521 | *
|
---|
522 | * @returns VINF_SUCCESS if only release, VINF_OBJECT_DESTROYED if destroyed.
|
---|
523 | * @param pThis The server instance.
|
---|
524 | */
|
---|
525 | DECLINLINE(int) rtLocalIpcServerReleaseAndUnlock(PRTLOCALIPCSERVERINT pThis)
|
---|
526 | {
|
---|
527 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
528 | Assert(cRefs < UINT32_MAX / 2);
|
---|
529 | if (!cRefs)
|
---|
530 | return rtLocalIpcServerWinDestroy(pThis);
|
---|
531 | return RTCritSectLeave(&pThis->CritSect);
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 |
|
---|
536 | RTDECL(int) RTLocalIpcServerDestroy(RTLOCALIPCSERVER hServer)
|
---|
537 | {
|
---|
538 | /*
|
---|
539 | * Validate input.
|
---|
540 | */
|
---|
541 | if (hServer == NIL_RTLOCALIPCSERVER)
|
---|
542 | return VINF_SUCCESS;
|
---|
543 | PRTLOCALIPCSERVERINT pThis = (PRTLOCALIPCSERVERINT)hServer;
|
---|
544 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
545 | AssertReturn(pThis->u32Magic == RTLOCALIPCSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * Cancel any thread currently busy using the server,
|
---|
549 | * leaving the cleanup to it.
|
---|
550 | */
|
---|
551 | AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTLOCALIPCSERVER_MAGIC, RTLOCALIPCSERVER_MAGIC), VERR_WRONG_ORDER);
|
---|
552 |
|
---|
553 | RTCritSectEnter(&pThis->CritSect);
|
---|
554 |
|
---|
555 | /* Cancel everything. */
|
---|
556 | ASMAtomicUoWriteBool(&pThis->fCancelled, true);
|
---|
557 | if (pThis->cRefs > 1)
|
---|
558 | {
|
---|
559 | BOOL fRc = SetEvent(pThis->hEvent);
|
---|
560 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
561 | }
|
---|
562 |
|
---|
563 | return rtLocalIpcServerReleaseAndUnlock(pThis);
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | RTDECL(int) RTLocalIpcServerListen(RTLOCALIPCSERVER hServer, PRTLOCALIPCSESSION phClientSession)
|
---|
568 | {
|
---|
569 | /*
|
---|
570 | * Validate input.
|
---|
571 | */
|
---|
572 | PRTLOCALIPCSERVERINT pThis = (PRTLOCALIPCSERVERINT)hServer;
|
---|
573 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
574 | AssertReturn(pThis->u32Magic == RTLOCALIPCSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
575 | AssertPtrReturn(phClientSession, VERR_INVALID_POINTER);
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Enter the critsect before inspecting the object further.
|
---|
579 | */
|
---|
580 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
581 | AssertRCReturn(rc, rc);
|
---|
582 |
|
---|
583 | rtLocalIpcServerRetain(pThis);
|
---|
584 | if (!pThis->fCancelled)
|
---|
585 | {
|
---|
586 | ResetEvent(pThis->hEvent);
|
---|
587 |
|
---|
588 | RTCritSectLeave(&pThis->CritSect);
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Try connect a client. We need to use overlapped I/O here because
|
---|
592 | * of the cancellation done by RTLocalIpcServerCancel and RTLocalIpcServerDestroy.
|
---|
593 | */
|
---|
594 | SetLastError(NO_ERROR);
|
---|
595 | BOOL fRc = ConnectNamedPipe(pThis->hNmPipe, &pThis->OverlappedIO);
|
---|
596 | DWORD dwErr = fRc ? NO_ERROR : GetLastError();
|
---|
597 | if ( !fRc
|
---|
598 | && dwErr == ERROR_IO_PENDING)
|
---|
599 | {
|
---|
600 | WaitForSingleObject(pThis->hEvent, INFINITE);
|
---|
601 | DWORD dwIgnored;
|
---|
602 | fRc = GetOverlappedResult(pThis->hNmPipe, &pThis->OverlappedIO, &dwIgnored, FALSE /* bWait*/);
|
---|
603 | dwErr = fRc ? NO_ERROR : GetLastError();
|
---|
604 | }
|
---|
605 |
|
---|
606 | RTCritSectEnter(&pThis->CritSect);
|
---|
607 | if ( !pThis->fCancelled /* Event signalled but not cancelled? */
|
---|
608 | && pThis->u32Magic == RTLOCALIPCSERVER_MAGIC)
|
---|
609 | {
|
---|
610 | /*
|
---|
611 | * Still alive, some error or an actual client.
|
---|
612 | *
|
---|
613 | * If it's the latter we'll have to create a new pipe instance that
|
---|
614 | * replaces the current one for the server. The current pipe instance
|
---|
615 | * will be assigned to the client session.
|
---|
616 | */
|
---|
617 | if ( fRc
|
---|
618 | || dwErr == ERROR_PIPE_CONNECTED)
|
---|
619 | {
|
---|
620 | HANDLE hNmPipe;
|
---|
621 | rc = rtLocalIpcServerWinCreatePipeInstance(&hNmPipe, pThis->wszName, false /* fFirst */);
|
---|
622 | if (RT_SUCCESS(rc))
|
---|
623 | {
|
---|
624 | HANDLE hNmPipeSession = pThis->hNmPipe; /* consumed */
|
---|
625 | pThis->hNmPipe = hNmPipe;
|
---|
626 | rc = rtLocalIpcWinCreateSession(phClientSession, hNmPipeSession);
|
---|
627 | }
|
---|
628 | else
|
---|
629 | {
|
---|
630 | /*
|
---|
631 | * We failed to create a new instance for the server, disconnect
|
---|
632 | * the client and fail. Don't try service the client here.
|
---|
633 | */
|
---|
634 | fRc = DisconnectNamedPipe(pThis->hNmPipe);
|
---|
635 | AssertMsg(fRc, ("%d\n", GetLastError()));
|
---|
636 | }
|
---|
637 | }
|
---|
638 | else
|
---|
639 | rc = RTErrConvertFromWin32(dwErr);
|
---|
640 | }
|
---|
641 | else
|
---|
642 | {
|
---|
643 | /*
|
---|
644 | * Cancelled.
|
---|
645 | *
|
---|
646 | * Cancel the overlapped io if it didn't complete (must be done
|
---|
647 | * in the this thread) or disconnect the client.
|
---|
648 | */
|
---|
649 | Assert(pThis->fCancelled);
|
---|
650 | if ( fRc
|
---|
651 | || dwErr == ERROR_PIPE_CONNECTED)
|
---|
652 | fRc = DisconnectNamedPipe(pThis->hNmPipe);
|
---|
653 | else if (dwErr == ERROR_IO_PENDING)
|
---|
654 | fRc = CancelIo(pThis->hNmPipe);
|
---|
655 | else
|
---|
656 | fRc = TRUE;
|
---|
657 | AssertMsg(fRc, ("%d\n", GetLastError()));
|
---|
658 | rc = VERR_CANCELLED;
|
---|
659 | }
|
---|
660 | }
|
---|
661 | else
|
---|
662 | {
|
---|
663 | /*pThis->fCancelled = false; - Terrible interface idea. Add API to clear fCancelled if ever required. */
|
---|
664 | rc = VERR_CANCELLED;
|
---|
665 | }
|
---|
666 | rtLocalIpcServerReleaseAndUnlock(pThis);
|
---|
667 | return rc;
|
---|
668 | }
|
---|
669 |
|
---|
670 |
|
---|
671 | RTDECL(int) RTLocalIpcServerCancel(RTLOCALIPCSERVER hServer)
|
---|
672 | {
|
---|
673 | /*
|
---|
674 | * Validate input.
|
---|
675 | */
|
---|
676 | PRTLOCALIPCSERVERINT pThis = (PRTLOCALIPCSERVERINT)hServer;
|
---|
677 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
678 | AssertReturn(pThis->u32Magic == RTLOCALIPCSERVER_MAGIC, VERR_INVALID_HANDLE);
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * Enter the critical section, then set the cancellation flag
|
---|
682 | * and signal the event (to wake up anyone in/at WaitForSingleObject).
|
---|
683 | */
|
---|
684 | rtLocalIpcServerRetain(pThis);
|
---|
685 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
686 | if (RT_SUCCESS(rc))
|
---|
687 | {
|
---|
688 | ASMAtomicUoWriteBool(&pThis->fCancelled, true);
|
---|
689 |
|
---|
690 | BOOL fRc = SetEvent(pThis->hEvent);
|
---|
691 | if (fRc)
|
---|
692 | rc = VINF_SUCCESS;
|
---|
693 | else
|
---|
694 | {
|
---|
695 | DWORD dwErr = GetLastError();
|
---|
696 | AssertMsgFailed(("dwErr=%u\n", dwErr));
|
---|
697 | rc = RTErrConvertFromWin32(dwErr);
|
---|
698 | }
|
---|
699 |
|
---|
700 | rtLocalIpcServerReleaseAndUnlock(pThis);
|
---|
701 | }
|
---|
702 | else
|
---|
703 | rtLocalIpcServerRelease(pThis);
|
---|
704 | return rc;
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Create a session instance for a new server client or a client connect.
|
---|
710 | *
|
---|
711 | * @returns IPRT status code.
|
---|
712 | *
|
---|
713 | * @param ppSession Where to store the session handle on success.
|
---|
714 | * @param hNmPipeSession The named pipe handle if server calling,
|
---|
715 | * INVALID_HANDLE_VALUE if client connect. This will
|
---|
716 | * be consumed by this session, meaning on failure to
|
---|
717 | * create the session it will be closed.
|
---|
718 | */
|
---|
719 | static int rtLocalIpcWinCreateSession(PRTLOCALIPCSESSIONINT *ppSession, HANDLE hNmPipeSession)
|
---|
720 | {
|
---|
721 | AssertPtr(ppSession);
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Allocate and initialize the session instance data.
|
---|
725 | */
|
---|
726 | int rc;
|
---|
727 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)RTMemAllocZ(sizeof(*pThis));
|
---|
728 | if (pThis)
|
---|
729 | {
|
---|
730 | pThis->u32Magic = RTLOCALIPCSESSION_MAGIC;
|
---|
731 | pThis->cRefs = 1; /* our ref */
|
---|
732 | pThis->fCancelled = false;
|
---|
733 | pThis->fZeroByteRead = false;
|
---|
734 | pThis->fServerSide = hNmPipeSession != INVALID_HANDLE_VALUE;
|
---|
735 | pThis->hNmPipe = hNmPipeSession;
|
---|
736 | #if 0 /* Non-blocking writes are not yet supported. */
|
---|
737 | pThis->pbBounceBuf = NULL;
|
---|
738 | pThis->cbBounceBufAlloc = 0;
|
---|
739 | pThis->cbBounceBufUsed = 0;
|
---|
740 | #endif
|
---|
741 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
742 | if (RT_SUCCESS(rc))
|
---|
743 | {
|
---|
744 | pThis->Read.hEvent = CreateEvent(NULL /*lpEventAttributes*/, TRUE /*bManualReset*/,
|
---|
745 | FALSE /*bInitialState*/, NULL /*lpName*/);
|
---|
746 | if (pThis->Read.hEvent != NULL)
|
---|
747 | {
|
---|
748 | pThis->Read.OverlappedIO.Internal = STATUS_PENDING;
|
---|
749 | pThis->Read.OverlappedIO.hEvent = pThis->Read.hEvent;
|
---|
750 | pThis->Read.hActiveThread = NIL_RTTHREAD;
|
---|
751 |
|
---|
752 | pThis->Write.hEvent = CreateEvent(NULL /*lpEventAttributes*/, TRUE /*bManualReset*/,
|
---|
753 | FALSE /*bInitialState*/, NULL /*lpName*/);
|
---|
754 | if (pThis->Write.hEvent != NULL)
|
---|
755 | {
|
---|
756 | pThis->Write.OverlappedIO.Internal = STATUS_PENDING;
|
---|
757 | pThis->Write.OverlappedIO.hEvent = pThis->Write.hEvent;
|
---|
758 | pThis->Write.hActiveThread = NIL_RTTHREAD;
|
---|
759 |
|
---|
760 | *ppSession = pThis;
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | }
|
---|
763 |
|
---|
764 | CloseHandle(pThis->Read.hEvent);
|
---|
765 | }
|
---|
766 |
|
---|
767 | /* bail out */
|
---|
768 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
769 | RTCritSectDelete(&pThis->CritSect);
|
---|
770 | }
|
---|
771 | RTMemFree(pThis);
|
---|
772 | }
|
---|
773 | else
|
---|
774 | rc = VERR_NO_MEMORY;
|
---|
775 |
|
---|
776 | if (hNmPipeSession != INVALID_HANDLE_VALUE)
|
---|
777 | {
|
---|
778 | BOOL fRc = CloseHandle(hNmPipeSession);
|
---|
779 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
780 | }
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 |
|
---|
785 | RTDECL(int) RTLocalIpcSessionConnect(PRTLOCALIPCSESSION phSession, const char *pszName, uint32_t fFlags)
|
---|
786 | {
|
---|
787 | /*
|
---|
788 | * Validate input.
|
---|
789 | */
|
---|
790 | AssertPtrReturn(phSession, VERR_INVALID_POINTER);
|
---|
791 | AssertReturn(!(fFlags & ~RTLOCALIPC_C_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
792 |
|
---|
793 | size_t cwcFullName;
|
---|
794 | int rc = rtLocalIpcWinValidateName(pszName, &cwcFullName, RT_BOOL(fFlags & RTLOCALIPC_C_FLAGS_NATIVE_NAME));
|
---|
795 | if (RT_SUCCESS(rc))
|
---|
796 | {
|
---|
797 | /*
|
---|
798 | * Create a session (shared with server client session creation).
|
---|
799 | */
|
---|
800 | PRTLOCALIPCSESSIONINT pThis;
|
---|
801 | rc = rtLocalIpcWinCreateSession(&pThis, INVALID_HANDLE_VALUE);
|
---|
802 | if (RT_SUCCESS(rc))
|
---|
803 | {
|
---|
804 | /*
|
---|
805 | * Try open the pipe.
|
---|
806 | */
|
---|
807 | PSECURITY_DESCRIPTOR pSecDesc;
|
---|
808 | rc = rtLocalIpcServerWinAllocSecurityDescriptior(&pSecDesc, false /*fServer*/);
|
---|
809 | if (RT_SUCCESS(rc))
|
---|
810 | {
|
---|
811 | PRTUTF16 pwszFullName = RTUtf16Alloc((cwcFullName + 1) * sizeof(RTUTF16));
|
---|
812 | if (pwszFullName)
|
---|
813 | rc = rtLocalIpcWinConstructName(pszName, pwszFullName, cwcFullName,
|
---|
814 | RT_BOOL(fFlags & RTLOCALIPC_C_FLAGS_NATIVE_NAME));
|
---|
815 | else
|
---|
816 | rc = VERR_NO_UTF16_MEMORY;
|
---|
817 | if (RT_SUCCESS(rc))
|
---|
818 | {
|
---|
819 | SECURITY_ATTRIBUTES SecAttrs;
|
---|
820 | SecAttrs.nLength = sizeof(SECURITY_ATTRIBUTES);
|
---|
821 | SecAttrs.lpSecurityDescriptor = pSecDesc;
|
---|
822 | SecAttrs.bInheritHandle = FALSE;
|
---|
823 |
|
---|
824 | HANDLE hPipe = CreateFileW(pwszFullName,
|
---|
825 | GENERIC_READ | GENERIC_WRITE,
|
---|
826 | 0 /*no sharing*/,
|
---|
827 | &SecAttrs,
|
---|
828 | OPEN_EXISTING,
|
---|
829 | FILE_FLAG_OVERLAPPED,
|
---|
830 | NULL /*no template hanlde*/);
|
---|
831 | if (hPipe != INVALID_HANDLE_VALUE)
|
---|
832 | {
|
---|
833 | pThis->hNmPipe = hPipe;
|
---|
834 |
|
---|
835 | LocalFree(pSecDesc);
|
---|
836 | RTUtf16Free(pwszFullName);
|
---|
837 |
|
---|
838 | /*
|
---|
839 | * We're done!
|
---|
840 | */
|
---|
841 | *phSession = pThis;
|
---|
842 | return VINF_SUCCESS;
|
---|
843 | }
|
---|
844 |
|
---|
845 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
846 | }
|
---|
847 |
|
---|
848 | RTUtf16Free(pwszFullName);
|
---|
849 | LocalFree(pSecDesc);
|
---|
850 | }
|
---|
851 |
|
---|
852 | /* destroy the session handle. */
|
---|
853 | CloseHandle(pThis->Read.hEvent);
|
---|
854 | CloseHandle(pThis->Write.hEvent);
|
---|
855 | RTCritSectDelete(&pThis->CritSect);
|
---|
856 |
|
---|
857 | RTMemFree(pThis);
|
---|
858 | }
|
---|
859 | }
|
---|
860 | return rc;
|
---|
861 | }
|
---|
862 |
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * Cancells all pending I/O operations, forcing the methods to return with
|
---|
866 | * VERR_CANCELLED (unless they've got actual data to return).
|
---|
867 | *
|
---|
868 | * Used by RTLocalIpcSessionCancel and RTLocalIpcSessionClose.
|
---|
869 | *
|
---|
870 | * @returns IPRT status code.
|
---|
871 | * @param pThis The client session instance.
|
---|
872 | */
|
---|
873 | static int rtLocalIpcWinCancel(PRTLOCALIPCSESSIONINT pThis)
|
---|
874 | {
|
---|
875 | ASMAtomicUoWriteBool(&pThis->fCancelled, true);
|
---|
876 |
|
---|
877 | /*
|
---|
878 | * Call CancelIo since this call cancels both read and write oriented operations.
|
---|
879 | */
|
---|
880 | if ( pThis->fZeroByteRead
|
---|
881 | || pThis->Read.hActiveThread != NIL_RTTHREAD
|
---|
882 | || pThis->Write.hActiveThread != NIL_RTTHREAD)
|
---|
883 | CancelIo(pThis->hNmPipe);
|
---|
884 |
|
---|
885 | /*
|
---|
886 | * Set both event semaphores.
|
---|
887 | */
|
---|
888 | BOOL fRc = SetEvent(pThis->Read.hEvent);
|
---|
889 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
890 | fRc = SetEvent(pThis->Write.hEvent);
|
---|
891 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
892 |
|
---|
893 | return VINF_SUCCESS;
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | /**
|
---|
898 | * Retains a reference to the session instance.
|
---|
899 | *
|
---|
900 | * @param pThis The client session instance.
|
---|
901 | */
|
---|
902 | DECLINLINE(void) rtLocalIpcSessionRetain(PRTLOCALIPCSESSIONINT pThis)
|
---|
903 | {
|
---|
904 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
905 | Assert(cRefs < UINT32_MAX / 2 && cRefs); NOREF(cRefs);
|
---|
906 | }
|
---|
907 |
|
---|
908 |
|
---|
909 | RTDECL(uint32_t) RTLocalIpcSessionRetain(RTLOCALIPCSESSION hSession)
|
---|
910 | {
|
---|
911 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
912 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
913 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, UINT32_MAX);
|
---|
914 |
|
---|
915 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
916 | Assert(cRefs < UINT32_MAX / 2 && cRefs);
|
---|
917 | return cRefs;
|
---|
918 | }
|
---|
919 |
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Call when the reference count reaches 0.
|
---|
923 | *
|
---|
924 | * Caller owns the critsect.
|
---|
925 | *
|
---|
926 | * @returns VINF_OBJECT_DESTROYED
|
---|
927 | * @param pThis The instance to destroy.
|
---|
928 | */
|
---|
929 | DECL_NO_INLINE(static, int) rtLocalIpcSessionWinDestroy(PRTLOCALIPCSESSIONINT pThis)
|
---|
930 | {
|
---|
931 | BOOL fRc = CloseHandle(pThis->hNmPipe);
|
---|
932 | AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
|
---|
933 | pThis->hNmPipe = INVALID_HANDLE_VALUE;
|
---|
934 |
|
---|
935 | fRc = CloseHandle(pThis->Write.hEvent);
|
---|
936 | AssertMsg(fRc, ("%d\n", GetLastError()));
|
---|
937 | pThis->Write.hEvent = NULL;
|
---|
938 |
|
---|
939 | fRc = CloseHandle(pThis->Read.hEvent);
|
---|
940 | AssertMsg(fRc, ("%d\n", GetLastError()));
|
---|
941 | pThis->Read.hEvent = NULL;
|
---|
942 |
|
---|
943 | int rc2 = RTCritSectLeave(&pThis->CritSect); AssertRC(rc2);
|
---|
944 | RTCritSectDelete(&pThis->CritSect);
|
---|
945 |
|
---|
946 | RTMemFree(pThis);
|
---|
947 | return VINF_OBJECT_DESTROYED;
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * Releases a reference to the session instance and unlock it.
|
---|
953 | *
|
---|
954 | * @returns VINF_SUCCESS or VINF_OBJECT_DESTROYED as appropriate.
|
---|
955 | * @param pThis The session instance.
|
---|
956 | */
|
---|
957 | DECLINLINE(int) rtLocalIpcSessionReleaseAndUnlock(PRTLOCALIPCSESSIONINT pThis)
|
---|
958 | {
|
---|
959 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
960 | Assert(cRefs < UINT32_MAX / 2);
|
---|
961 | if (!cRefs)
|
---|
962 | return rtLocalIpcSessionWinDestroy(pThis);
|
---|
963 |
|
---|
964 | int rc2 = RTCritSectLeave(&pThis->CritSect); AssertRC(rc2);
|
---|
965 | Log(("rtLocalIpcSessionReleaseAndUnlock: %u refs left\n", cRefs));
|
---|
966 | return VINF_SUCCESS;
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | RTDECL(uint32_t) RTLocalIpcSessionRelease(RTLOCALIPCSESSION hSession)
|
---|
971 | {
|
---|
972 | if (hSession == NIL_RTLOCALIPCSESSION)
|
---|
973 | return 0;
|
---|
974 |
|
---|
975 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
976 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
977 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, UINT32_MAX);
|
---|
978 |
|
---|
979 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
980 | Assert(cRefs < UINT32_MAX / 2);
|
---|
981 | if (cRefs)
|
---|
982 | Log(("RTLocalIpcSessionRelease: %u refs left\n", cRefs));
|
---|
983 | else
|
---|
984 | {
|
---|
985 | RTCritSectEnter(&pThis->CritSect);
|
---|
986 | rtLocalIpcSessionWinDestroy(pThis);
|
---|
987 | }
|
---|
988 | return cRefs;
|
---|
989 | }
|
---|
990 |
|
---|
991 |
|
---|
992 | RTDECL(int) RTLocalIpcSessionClose(RTLOCALIPCSESSION hSession)
|
---|
993 | {
|
---|
994 | /*
|
---|
995 | * Validate input.
|
---|
996 | */
|
---|
997 | if (hSession == NIL_RTLOCALIPCSESSION)
|
---|
998 | return VINF_SUCCESS;
|
---|
999 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1000 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1001 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1002 |
|
---|
1003 | /*
|
---|
1004 | * Invalidate the instance, cancel all outstanding I/O and drop our reference.
|
---|
1005 | */
|
---|
1006 | RTCritSectEnter(&pThis->CritSect);
|
---|
1007 | rtLocalIpcWinCancel(pThis);
|
---|
1008 | return rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 |
|
---|
1012 | /**
|
---|
1013 | * Handles WaitForSingleObject return value when waiting for a zero byte read.
|
---|
1014 | *
|
---|
1015 | * The zero byte read is started by the RTLocalIpcSessionWaitForData method and
|
---|
1016 | * left pending when the function times out. This saves us the problem of
|
---|
1017 | * CancelIo messing with all active I/O operations and the trouble of restarting
|
---|
1018 | * the zero byte read the next time the method is called. However should
|
---|
1019 | * RTLocalIpcSessionRead be called after a failed RTLocalIpcSessionWaitForData
|
---|
1020 | * call, the zero byte read will still be pending and it must wait for it to
|
---|
1021 | * complete before the OVERLAPPEDIO structure can be reused.
|
---|
1022 | *
|
---|
1023 | * Thus, both functions will do WaitForSingleObject and share this routine to
|
---|
1024 | * handle the outcome.
|
---|
1025 | *
|
---|
1026 | * @returns IPRT status code.
|
---|
1027 | * @param pThis The session instance.
|
---|
1028 | * @param rcWait The WaitForSingleObject return code.
|
---|
1029 | */
|
---|
1030 | static int rtLocalIpcWinGetZeroReadResult(PRTLOCALIPCSESSIONINT pThis, DWORD rcWait)
|
---|
1031 | {
|
---|
1032 | int rc;
|
---|
1033 | DWORD cbRead = 42;
|
---|
1034 | if (rcWait == WAIT_OBJECT_0)
|
---|
1035 | {
|
---|
1036 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->Read.OverlappedIO, &cbRead, !pThis->fCancelled /*fWait*/))
|
---|
1037 | {
|
---|
1038 | Assert(cbRead == 0);
|
---|
1039 | rc = VINF_SUCCESS;
|
---|
1040 | pThis->fZeroByteRead = false;
|
---|
1041 | }
|
---|
1042 | else if (pThis->fCancelled)
|
---|
1043 | rc = VERR_CANCELLED;
|
---|
1044 | else
|
---|
1045 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1046 | }
|
---|
1047 | else
|
---|
1048 | {
|
---|
1049 | /* We try get the result here too, just in case we're lucky, but no waiting. */
|
---|
1050 | DWORD dwErr = GetLastError();
|
---|
1051 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->Read.OverlappedIO, &cbRead, FALSE /*fWait*/))
|
---|
1052 | {
|
---|
1053 | Assert(cbRead == 0);
|
---|
1054 | rc = VINF_SUCCESS;
|
---|
1055 | pThis->fZeroByteRead = false;
|
---|
1056 | }
|
---|
1057 | else if (rcWait == WAIT_TIMEOUT)
|
---|
1058 | rc = VERR_TIMEOUT;
|
---|
1059 | else if (rcWait == WAIT_ABANDONED)
|
---|
1060 | rc = VERR_INVALID_HANDLE;
|
---|
1061 | else
|
---|
1062 | rc = RTErrConvertFromWin32(dwErr);
|
---|
1063 | }
|
---|
1064 | return rc;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 |
|
---|
1068 | RTDECL(int) RTLocalIpcSessionRead(RTLOCALIPCSESSION hSession, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
1069 | {
|
---|
1070 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1071 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1072 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1073 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
1074 | /* pcbRead is optional. */
|
---|
1075 |
|
---|
1076 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1077 | if (RT_SUCCESS(rc))
|
---|
1078 | {
|
---|
1079 | rtLocalIpcSessionRetain(pThis);
|
---|
1080 | if (pThis->Read.hActiveThread == NIL_RTTHREAD)
|
---|
1081 | {
|
---|
1082 | pThis->Read.hActiveThread = RTThreadSelf();
|
---|
1083 |
|
---|
1084 | size_t cbTotalRead = 0;
|
---|
1085 | while (cbToRead > 0)
|
---|
1086 | {
|
---|
1087 | DWORD cbRead = 0;
|
---|
1088 | if (!pThis->fCancelled)
|
---|
1089 | {
|
---|
1090 | /*
|
---|
1091 | * Wait for pending zero byte read, if necessary.
|
---|
1092 | * Note! It cannot easily be cancelled due to concurrent current writes.
|
---|
1093 | */
|
---|
1094 | if (!pThis->fZeroByteRead)
|
---|
1095 | { /* likely */ }
|
---|
1096 | else
|
---|
1097 | {
|
---|
1098 | RTCritSectLeave(&pThis->CritSect);
|
---|
1099 | DWORD rcWait = WaitForSingleObject(pThis->Read.OverlappedIO.hEvent, RT_MS_1MIN);
|
---|
1100 | RTCritSectEnter(&pThis->CritSect);
|
---|
1101 |
|
---|
1102 | rc = rtLocalIpcWinGetZeroReadResult(pThis, rcWait);
|
---|
1103 | if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT)
|
---|
1104 | continue;
|
---|
1105 | break;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * Kick of a an overlapped read. It should return immediately if
|
---|
1110 | * there is bytes in the buffer. If not, we'll cancel it and see
|
---|
1111 | * what we get back.
|
---|
1112 | */
|
---|
1113 | rc = ResetEvent(pThis->Read.OverlappedIO.hEvent); Assert(rc == TRUE);
|
---|
1114 | RTCritSectLeave(&pThis->CritSect);
|
---|
1115 |
|
---|
1116 | if (ReadFile(pThis->hNmPipe, pvBuf,
|
---|
1117 | cbToRead <= ~(DWORD)0 ? (DWORD)cbToRead : ~(DWORD)0,
|
---|
1118 | &cbRead, &pThis->Read.OverlappedIO))
|
---|
1119 | {
|
---|
1120 | RTCritSectEnter(&pThis->CritSect);
|
---|
1121 | rc = VINF_SUCCESS;
|
---|
1122 | }
|
---|
1123 | else if (GetLastError() == ERROR_IO_PENDING)
|
---|
1124 | {
|
---|
1125 | WaitForSingleObject(pThis->Read.OverlappedIO.hEvent, INFINITE);
|
---|
1126 |
|
---|
1127 | RTCritSectEnter(&pThis->CritSect);
|
---|
1128 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->Read.OverlappedIO, &cbRead, TRUE /*fWait*/))
|
---|
1129 | rc = VINF_SUCCESS;
|
---|
1130 | else
|
---|
1131 | {
|
---|
1132 | if (pThis->fCancelled)
|
---|
1133 | rc = VERR_CANCELLED;
|
---|
1134 | else
|
---|
1135 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1136 | break;
|
---|
1137 | }
|
---|
1138 | }
|
---|
1139 | else
|
---|
1140 | {
|
---|
1141 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1142 | AssertMsgFailedBreak(("%Rrc\n", rc));
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 | else
|
---|
1146 | {
|
---|
1147 | rc = VERR_CANCELLED;
|
---|
1148 | break;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | /* Advance. */
|
---|
1152 | cbToRead -= cbRead;
|
---|
1153 | cbTotalRead += cbRead;
|
---|
1154 | pvBuf = (uint8_t *)pvBuf + cbRead;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | if (pcbRead)
|
---|
1158 | {
|
---|
1159 | *pcbRead = cbTotalRead;
|
---|
1160 | if ( RT_FAILURE(rc)
|
---|
1161 | && cbTotalRead
|
---|
1162 | && rc != VERR_INVALID_POINTER)
|
---|
1163 | rc = VINF_SUCCESS;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | pThis->Read.hActiveThread = NIL_RTTHREAD;
|
---|
1167 | }
|
---|
1168 | else
|
---|
1169 | rc = VERR_WRONG_ORDER;
|
---|
1170 | rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | return rc;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 |
|
---|
1177 | RTDECL(int) RTLocalIpcSessionReadNB(RTLOCALIPCSESSION hSession, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
1178 | {
|
---|
1179 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1180 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1181 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1182 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
1183 | AssertPtrReturn(pcbRead, VERR_INVALID_POINTER);
|
---|
1184 | *pcbRead = 0;
|
---|
1185 |
|
---|
1186 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1187 | if (RT_SUCCESS(rc))
|
---|
1188 | {
|
---|
1189 | rtLocalIpcSessionRetain(pThis);
|
---|
1190 | if (pThis->Read.hActiveThread == NIL_RTTHREAD)
|
---|
1191 | {
|
---|
1192 | pThis->Read.hActiveThread = RTThreadSelf();
|
---|
1193 |
|
---|
1194 | for (;;)
|
---|
1195 | {
|
---|
1196 | DWORD cbRead = 0;
|
---|
1197 | if (!pThis->fCancelled)
|
---|
1198 | {
|
---|
1199 | /*
|
---|
1200 | * Wait for pending zero byte read, if necessary.
|
---|
1201 | * Note! It cannot easily be cancelled due to concurrent current writes.
|
---|
1202 | */
|
---|
1203 | if (!pThis->fZeroByteRead)
|
---|
1204 | { /* likely */ }
|
---|
1205 | else
|
---|
1206 | {
|
---|
1207 | RTCritSectLeave(&pThis->CritSect);
|
---|
1208 | DWORD rcWait = WaitForSingleObject(pThis->Read.OverlappedIO.hEvent, 0);
|
---|
1209 | RTCritSectEnter(&pThis->CritSect);
|
---|
1210 |
|
---|
1211 | rc = rtLocalIpcWinGetZeroReadResult(pThis, rcWait);
|
---|
1212 | if (RT_SUCCESS(rc))
|
---|
1213 | continue;
|
---|
1214 |
|
---|
1215 | if (rc == VERR_TIMEOUT)
|
---|
1216 | rc = VINF_TRY_AGAIN;
|
---|
1217 | break;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | /*
|
---|
1221 | * Figure out how much we can read (cannot try and cancel here
|
---|
1222 | * like in the anonymous pipe code).
|
---|
1223 | */
|
---|
1224 | DWORD cbAvailable;
|
---|
1225 | if (PeekNamedPipe(pThis->hNmPipe, NULL, 0, NULL, &cbAvailable, NULL))
|
---|
1226 | {
|
---|
1227 | if (cbAvailable == 0 || cbToRead == 0)
|
---|
1228 | {
|
---|
1229 | *pcbRead = 0;
|
---|
1230 | rc = VINF_TRY_AGAIN;
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 | else
|
---|
1235 | {
|
---|
1236 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1237 | break;
|
---|
1238 | }
|
---|
1239 | if (cbAvailable > cbToRead)
|
---|
1240 | cbAvailable = (DWORD)cbToRead;
|
---|
1241 |
|
---|
1242 | /*
|
---|
1243 | * Kick of a an overlapped read. It should return immediately, so we
|
---|
1244 | * don't really need to leave the critsect here.
|
---|
1245 | */
|
---|
1246 | rc = ResetEvent(pThis->Read.OverlappedIO.hEvent); Assert(rc == TRUE);
|
---|
1247 | if (ReadFile(pThis->hNmPipe, pvBuf, cbAvailable, &cbRead, &pThis->Read.OverlappedIO))
|
---|
1248 | {
|
---|
1249 | *pcbRead = cbRead;
|
---|
1250 | rc = VINF_SUCCESS;
|
---|
1251 | }
|
---|
1252 | else if (GetLastError() == ERROR_IO_PENDING)
|
---|
1253 | {
|
---|
1254 | DWORD rcWait = WaitForSingleObject(pThis->Read.OverlappedIO.hEvent, 0);
|
---|
1255 | if (rcWait == WAIT_TIMEOUT)
|
---|
1256 | {
|
---|
1257 | RTCritSectLeave(&pThis->CritSect);
|
---|
1258 | rcWait = WaitForSingleObject(pThis->Read.OverlappedIO.hEvent, INFINITE);
|
---|
1259 | RTCritSectEnter(&pThis->CritSect);
|
---|
1260 | }
|
---|
1261 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->Read.OverlappedIO, &cbRead, TRUE /*fWait*/))
|
---|
1262 | {
|
---|
1263 | *pcbRead = cbRead;
|
---|
1264 | rc = VINF_SUCCESS;
|
---|
1265 | }
|
---|
1266 | else
|
---|
1267 | {
|
---|
1268 | if (pThis->fCancelled)
|
---|
1269 | rc = VERR_CANCELLED;
|
---|
1270 | else
|
---|
1271 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1272 | }
|
---|
1273 | }
|
---|
1274 | else
|
---|
1275 | {
|
---|
1276 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1277 | AssertMsgFailedBreak(("%Rrc\n", rc));
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 | else
|
---|
1281 | rc = VERR_CANCELLED;
|
---|
1282 | break;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | pThis->Read.hActiveThread = NIL_RTTHREAD;
|
---|
1286 | }
|
---|
1287 | else
|
---|
1288 | rc = VERR_WRONG_ORDER;
|
---|
1289 | rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | return rc;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 |
|
---|
1296 | #if 0 /* Non-blocking writes are not yet supported. */
|
---|
1297 | /**
|
---|
1298 | * Common worker for handling I/O completion.
|
---|
1299 | *
|
---|
1300 | * This is used by RTLocalIpcSessionClose and RTLocalIpcSessionWrite.
|
---|
1301 | *
|
---|
1302 | * @returns IPRT status code.
|
---|
1303 | * @param pThis The pipe instance handle.
|
---|
1304 | */
|
---|
1305 | static int rtLocalIpcSessionWriteCheckCompletion(PRTLOCALIPCSESSIONINT pThis)
|
---|
1306 | {
|
---|
1307 | int rc;
|
---|
1308 | DWORD rcWait = WaitForSingleObject(pThis->OverlappedIO.hEvent, 0);
|
---|
1309 | if (rcWait == WAIT_OBJECT_0)
|
---|
1310 | {
|
---|
1311 | DWORD cbWritten = 0;
|
---|
1312 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->OverlappedIO, &cbWritten, TRUE))
|
---|
1313 | {
|
---|
1314 | for (;;)
|
---|
1315 | {
|
---|
1316 | if (cbWritten >= pThis->cbBounceBufUsed)
|
---|
1317 | {
|
---|
1318 | pThis->fIOPending = false;
|
---|
1319 | rc = VINF_SUCCESS;
|
---|
1320 | break;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | /* resubmit the remainder of the buffer - can this actually happen? */
|
---|
1324 | memmove(&pThis->pbBounceBuf[0], &pThis->pbBounceBuf[cbWritten], pThis->cbBounceBufUsed - cbWritten);
|
---|
1325 | rc = ResetEvent(pThis->OverlappedIO.hEvent); Assert(rc == TRUE);
|
---|
1326 | if (!WriteFile(pThis->hNmPipe, pThis->pbBounceBuf, (DWORD)pThis->cbBounceBufUsed,
|
---|
1327 | &cbWritten, &pThis->OverlappedIO))
|
---|
1328 | {
|
---|
1329 | DWORD dwErr = GetLastError();
|
---|
1330 | if (dwErr == ERROR_IO_PENDING)
|
---|
1331 | rc = VINF_TRY_AGAIN;
|
---|
1332 | else
|
---|
1333 | {
|
---|
1334 | pThis->fIOPending = false;
|
---|
1335 | if (dwErr == ERROR_NO_DATA)
|
---|
1336 | rc = VERR_BROKEN_PIPE;
|
---|
1337 | else
|
---|
1338 | rc = RTErrConvertFromWin32(dwErr);
|
---|
1339 | }
|
---|
1340 | break;
|
---|
1341 | }
|
---|
1342 | Assert(cbWritten > 0);
|
---|
1343 | }
|
---|
1344 | }
|
---|
1345 | else
|
---|
1346 | {
|
---|
1347 | pThis->fIOPending = false;
|
---|
1348 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1349 | }
|
---|
1350 | }
|
---|
1351 | else if (rcWait == WAIT_TIMEOUT)
|
---|
1352 | rc = VINF_TRY_AGAIN;
|
---|
1353 | else
|
---|
1354 | {
|
---|
1355 | pThis->fIOPending = false;
|
---|
1356 | if (rcWait == WAIT_ABANDONED)
|
---|
1357 | rc = VERR_INVALID_HANDLE;
|
---|
1358 | else
|
---|
1359 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1360 | }
|
---|
1361 | return rc;
|
---|
1362 | }
|
---|
1363 | #endif
|
---|
1364 |
|
---|
1365 |
|
---|
1366 | RTDECL(int) RTLocalIpcSessionWrite(RTLOCALIPCSESSION hSession, const void *pvBuf, size_t cbToWrite)
|
---|
1367 | {
|
---|
1368 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1369 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1370 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1371 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
1372 | AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
|
---|
1373 |
|
---|
1374 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1375 | if (RT_SUCCESS(rc))
|
---|
1376 | {
|
---|
1377 | rtLocalIpcSessionRetain(pThis);
|
---|
1378 | if (pThis->Write.hActiveThread == NIL_RTTHREAD)
|
---|
1379 | {
|
---|
1380 | pThis->Write.hActiveThread = RTThreadSelf();
|
---|
1381 |
|
---|
1382 | /*
|
---|
1383 | * Try write everything. No bounce buffering necessary.
|
---|
1384 | */
|
---|
1385 | size_t cbTotalWritten = 0;
|
---|
1386 | while (cbToWrite > 0)
|
---|
1387 | {
|
---|
1388 | DWORD cbWritten = 0;
|
---|
1389 | if (!pThis->fCancelled)
|
---|
1390 | {
|
---|
1391 | BOOL fRc = ResetEvent(pThis->Write.OverlappedIO.hEvent); Assert(fRc == TRUE);
|
---|
1392 | RTCritSectLeave(&pThis->CritSect);
|
---|
1393 |
|
---|
1394 | fRc = WriteFile(pThis->hNmPipe, pvBuf,
|
---|
1395 | cbToWrite <= ~(DWORD)0 ? (DWORD)cbToWrite : ~(DWORD)0,
|
---|
1396 | &cbWritten, &pThis->Write.OverlappedIO);
|
---|
1397 | if (fRc)
|
---|
1398 | rc = VINF_SUCCESS;
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | DWORD dwErr = GetLastError();
|
---|
1402 | if (dwErr == ERROR_IO_PENDING)
|
---|
1403 | {
|
---|
1404 | DWORD rcWait = WaitForSingleObject(pThis->Write.OverlappedIO.hEvent, INFINITE);
|
---|
1405 | if (rcWait == WAIT_OBJECT_0)
|
---|
1406 | {
|
---|
1407 | if (GetOverlappedResult(pThis->hNmPipe, &pThis->Write.OverlappedIO, &cbWritten, TRUE /*fWait*/))
|
---|
1408 | rc = VINF_SUCCESS;
|
---|
1409 | else
|
---|
1410 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1411 | }
|
---|
1412 | else if (rcWait == WAIT_TIMEOUT)
|
---|
1413 | rc = VERR_TIMEOUT;
|
---|
1414 | else if (rcWait == WAIT_ABANDONED)
|
---|
1415 | rc = VERR_INVALID_HANDLE;
|
---|
1416 | else
|
---|
1417 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1418 | }
|
---|
1419 | else if (dwErr == ERROR_NO_DATA)
|
---|
1420 | rc = VERR_BROKEN_PIPE;
|
---|
1421 | else
|
---|
1422 | rc = RTErrConvertFromWin32(dwErr);
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | RTCritSectEnter(&pThis->CritSect);
|
---|
1426 | if (RT_FAILURE(rc))
|
---|
1427 | break;
|
---|
1428 | }
|
---|
1429 | else
|
---|
1430 | {
|
---|
1431 | rc = VERR_CANCELLED;
|
---|
1432 | break;
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | /* Advance. */
|
---|
1436 | pvBuf = (char const *)pvBuf + cbWritten;
|
---|
1437 | cbTotalWritten += cbWritten;
|
---|
1438 | cbToWrite -= cbWritten;
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | pThis->Write.hActiveThread = NIL_RTTHREAD;
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | rc = VERR_WRONG_ORDER;
|
---|
1445 | rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | return rc;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 |
|
---|
1452 | RTDECL(int) RTLocalIpcSessionFlush(RTLOCALIPCSESSION hSession)
|
---|
1453 | {
|
---|
1454 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1455 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1456 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1457 |
|
---|
1458 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1459 | if (RT_SUCCESS(rc))
|
---|
1460 | {
|
---|
1461 | if (pThis->Write.hActiveThread == NIL_RTTHREAD)
|
---|
1462 | {
|
---|
1463 | /* No flushing on Windows needed since RTLocalIpcSessionWrite will block until
|
---|
1464 | * all data was written (or an error occurred). */
|
---|
1465 | /** @todo r=bird: above comment is misinformed.
|
---|
1466 | /* Implement this as soon as we want an explicit asynchronous version of
|
---|
1467 | * RTLocalIpcSessionWrite on Windows. */
|
---|
1468 | rc = VINF_SUCCESS;
|
---|
1469 | }
|
---|
1470 | else
|
---|
1471 | rc = VERR_WRONG_ORDER;
|
---|
1472 | RTCritSectLeave(&pThis->CritSect);
|
---|
1473 | }
|
---|
1474 | return rc;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 |
|
---|
1478 | RTDECL(int) RTLocalIpcSessionWaitForData(RTLOCALIPCSESSION hSession, uint32_t cMillies)
|
---|
1479 | {
|
---|
1480 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1481 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1482 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1483 |
|
---|
1484 | uint64_t const msStart = RTTimeMilliTS();
|
---|
1485 |
|
---|
1486 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1487 | if (RT_SUCCESS(rc))
|
---|
1488 | {
|
---|
1489 | rtLocalIpcSessionRetain(pThis);
|
---|
1490 | if (pThis->Read.hActiveThread == NIL_RTTHREAD)
|
---|
1491 | {
|
---|
1492 | pThis->Read.hActiveThread = RTThreadSelf();
|
---|
1493 |
|
---|
1494 | /*
|
---|
1495 | * Wait loop.
|
---|
1496 | */
|
---|
1497 | for (unsigned iLoop = 0;; iLoop++)
|
---|
1498 | {
|
---|
1499 | /*
|
---|
1500 | * Check for cancellation before we continue.
|
---|
1501 | */
|
---|
1502 | if (!pThis->fCancelled)
|
---|
1503 | { /* likely */ }
|
---|
1504 | else
|
---|
1505 | {
|
---|
1506 | rc = VERR_CANCELLED;
|
---|
1507 | break;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /*
|
---|
1511 | * Prep something we can wait on.
|
---|
1512 | */
|
---|
1513 | HANDLE hWait = INVALID_HANDLE_VALUE;
|
---|
1514 | if (pThis->fZeroByteRead)
|
---|
1515 | hWait = pThis->Read.OverlappedIO.hEvent;
|
---|
1516 | else
|
---|
1517 | {
|
---|
1518 | /* Peek at the pipe buffer and see how many bytes it contains. */
|
---|
1519 | DWORD cbAvailable;
|
---|
1520 | if ( PeekNamedPipe(pThis->hNmPipe, NULL, 0, NULL, &cbAvailable, NULL)
|
---|
1521 | && cbAvailable)
|
---|
1522 | {
|
---|
1523 | rc = VINF_SUCCESS;
|
---|
1524 | break;
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | /* Start a zero byte read operation that we can wait on. */
|
---|
1528 | if (cMillies == 0)
|
---|
1529 | {
|
---|
1530 | rc = VERR_TIMEOUT;
|
---|
1531 | break;
|
---|
1532 | }
|
---|
1533 | BOOL fRc = ResetEvent(pThis->Read.OverlappedIO.hEvent); Assert(fRc == TRUE); NOREF(fRc);
|
---|
1534 | DWORD cbRead = 0;
|
---|
1535 | if (ReadFile(pThis->hNmPipe, pThis->abBuf, 0 /*cbToRead*/, &cbRead, &pThis->Read.OverlappedIO))
|
---|
1536 | {
|
---|
1537 | rc = VINF_SUCCESS;
|
---|
1538 | if (iLoop > 10)
|
---|
1539 | RTThreadYield();
|
---|
1540 | }
|
---|
1541 | else if (GetLastError() == ERROR_IO_PENDING)
|
---|
1542 | {
|
---|
1543 | pThis->fZeroByteRead = true;
|
---|
1544 | hWait = pThis->Read.OverlappedIO.hEvent;
|
---|
1545 | }
|
---|
1546 | else
|
---|
1547 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1548 | if (RT_FAILURE(rc))
|
---|
1549 | break;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | /*
|
---|
1553 | * Check for timeout.
|
---|
1554 | */
|
---|
1555 | DWORD cMsMaxWait = INFINITE; /* (MSC maybe used uninitialized) */
|
---|
1556 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
1557 | cMsMaxWait = INFINITE;
|
---|
1558 | else if ( hWait != INVALID_HANDLE_VALUE
|
---|
1559 | || iLoop > 10)
|
---|
1560 | {
|
---|
1561 | uint64_t cMsElapsed = RTTimeMilliTS() - msStart;
|
---|
1562 | if (cMsElapsed <= cMillies)
|
---|
1563 | cMsMaxWait = cMillies - (uint32_t)cMsElapsed;
|
---|
1564 | else if (iLoop == 0)
|
---|
1565 | cMsMaxWait = cMillies ? 1 : 0;
|
---|
1566 | else
|
---|
1567 | {
|
---|
1568 | rc = VERR_TIMEOUT;
|
---|
1569 | break;
|
---|
1570 | }
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | /*
|
---|
1574 | * Wait and collect the result.
|
---|
1575 | */
|
---|
1576 | if (hWait != INVALID_HANDLE_VALUE)
|
---|
1577 | {
|
---|
1578 | RTCritSectLeave(&pThis->CritSect);
|
---|
1579 |
|
---|
1580 | DWORD rcWait = WaitForSingleObject(hWait, cMsMaxWait);
|
---|
1581 |
|
---|
1582 | int rc2 = RTCritSectEnter(&pThis->CritSect);
|
---|
1583 | AssertRC(rc2);
|
---|
1584 |
|
---|
1585 | rc = rtLocalIpcWinGetZeroReadResult(pThis, rcWait);
|
---|
1586 | break;
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | pThis->Read.hActiveThread = NIL_RTTHREAD;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | return rc;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 |
|
---|
1600 | RTDECL(int) RTLocalIpcSessionCancel(RTLOCALIPCSESSION hSession)
|
---|
1601 | {
|
---|
1602 | PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
|
---|
1603 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1604 | AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
|
---|
1605 |
|
---|
1606 | /*
|
---|
1607 | * Enter the critical section, then set the cancellation flag
|
---|
1608 | * and signal the event (to wake up anyone in/at WaitForSingleObject).
|
---|
1609 | */
|
---|
1610 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
1611 | if (RT_SUCCESS(rc))
|
---|
1612 | {
|
---|
1613 | rtLocalIpcSessionRetain(pThis);
|
---|
1614 | rc = rtLocalIpcWinCancel(pThis);
|
---|
1615 | rtLocalIpcSessionReleaseAndUnlock(pThis);
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | return rc;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 |
|
---|
1622 | RTDECL(int) RTLocalIpcSessionQueryProcess(RTLOCALIPCSESSION hSession, PRTPROCESS pProcess)
|
---|
1623 | {
|
---|
1624 | return VERR_NOT_SUPPORTED;
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 |
|
---|
1628 | RTDECL(int) RTLocalIpcSessionQueryUserId(RTLOCALIPCSESSION hSession, PRTUID pUid)
|
---|
1629 | {
|
---|
1630 | return VERR_NOT_SUPPORTED;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 |
|
---|
1634 | RTDECL(int) RTLocalIpcSessionQueryGroupId(RTLOCALIPCSESSION hSession, PRTUID pUid)
|
---|
1635 | {
|
---|
1636 | return VERR_NOT_SUPPORTED;
|
---|
1637 | }
|
---|
1638 |
|
---|