2 * PROJECT: ReactOS Service Host
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: base/services/svchost/rpcsrv.c
5 * PURPOSE: RPC Service Support
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES ******************************************************************/
13 /* GLOBALS *******************************************************************/
15 LONG RpcpNumInstances
;
16 CRITICAL_SECTION RpcpCriticalSection
;
18 /* FUNCTIONS *****************************************************************/
26 /* Clear the reference count and initialize the critical section */
28 return RtlInitializeCriticalSection((PVOID
)&RpcpCriticalSection
);
34 _In_ RPC_IF_HANDLE IfSpec
39 /* Unregister the interface */
40 rpcStatus
= RpcServerUnregisterIf(IfSpec
, NULL
, TRUE
);
42 /* Acquire the lock while we dereference the RPC services */
43 EnterCriticalSection(&RpcpCriticalSection
);
44 if (--RpcpNumInstances
== 0)
46 /* All RPC services stopped, rundown the server */
47 RpcMgmtStopServerListening(NULL
);
48 RpcMgmtWaitServerListen();
51 /* Release the lock and return the unregister result */
52 LeaveCriticalSection(&RpcpCriticalSection
);
53 return I_RpcMapWin32Status(rpcStatus
);
59 _In_ RPC_IF_HANDLE IfSpec
64 /* Unregister the interface */
65 rpcStatus
= RpcServerUnregisterIfEx(IfSpec
, NULL
, TRUE
);
67 /* Acquire the lock while we dereference the RPC services */
68 EnterCriticalSection(&RpcpCriticalSection
);
69 if (--RpcpNumInstances
== 0)
71 /* All RPC services stopped, rundown the server */
72 RpcMgmtStopServerListening(NULL
);
73 RpcMgmtWaitServerListen();
76 /* Release the lock and return the unregister result */
77 LeaveCriticalSection(&RpcpCriticalSection
);
78 return I_RpcMapWin32Status(rpcStatus
);
85 _In_ RPC_IF_HANDLE IfSpec
92 /* Allocate space for the interface name and the \\PIPE\\ prefix */
93 endpointName
= LocalAlloc(0, sizeof(WCHAR
) * wcslen(IfName
) + 16);
96 /* Copy the prefix, and then the interface name */
97 wcscpy(endpointName
, L
"\\PIPE\\");
98 wcscat(endpointName
, IfName
);
100 /* Create a named pipe endpoint with this name */
101 rpcStatus
= RpcServerUseProtseqEpW(L
"ncacn_np",
102 RPC_C_PROTSEQ_MAX_REQS_DEFAULT
,
104 if ((rpcStatus
!= RPC_S_OK
) && (rpcStatus
!= RPC_S_DUPLICATE_ENDPOINT
))
106 /* We couldn't create it, or it already existed... */
107 DbgPrint("RpcServerUseProtseqW failed! rpcstatus = %u\n", rpcStatus
);
111 /* It worked, register an interface on this endpoint now */
112 rpcStatus
= RpcServerRegisterIf(IfSpec
, 0, 0);
115 /* In both success and failure, free the name, and convert the status */
116 LocalFree(endpointName
);
117 ntStatus
= I_RpcMapWin32Status(rpcStatus
);
121 /* No memory, bail out */
122 ntStatus
= STATUS_NO_MEMORY
;
125 /* Return back to the caller */
133 _In_ RPC_IF_HANDLE IfSpec
138 /* Acquire the lock while we instantiate a new interface */
139 EnterCriticalSection(&RpcpCriticalSection
);
141 /* Add this interface to the service */
142 ntStatus
= RpcpAddInterface(IfName
, IfSpec
);
145 /* Increment the reference count to see if this was the first interface */
146 if (++RpcpNumInstances
== 1)
148 /* It was, so put the server into listening mode now */
149 ntStatus
= RpcServerListen(1, 12345, TRUE
);
150 if (ntStatus
== RPC_S_ALREADY_LISTENING
) ntStatus
= STATUS_SUCCESS
;
154 /* Release the lock and return back the result to the caller */
155 LeaveCriticalSection(&RpcpCriticalSection
);
156 return I_RpcMapWin32Status(ntStatus
);