rpcss_main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright 2001, Ove Kåven, TransGaming Technologies Inc.
  3. * Copyright 2002 Greg Turner
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <limits.h>
  22. #include <assert.h>
  23. #include "windef.h"
  24. #include "winbase.h"
  25. #include "winnt.h"
  26. #include "winsvc.h"
  27. #include "irot.h"
  28. #include "epm.h"
  29. #include "irpcss.h"
  30. #include "wine/debug.h"
  31. #include "wine/heap.h"
  32. #include "wine/list.h"
  33. WINE_DEFAULT_DEBUG_CHANNEL(ole);
  34. static WCHAR rpcssW[] = L"RpcSs";
  35. static HANDLE exit_event;
  36. static SERVICE_STATUS_HANDLE service_handle;
  37. struct registered_class
  38. {
  39. struct list entry;
  40. GUID clsid;
  41. unsigned int cookie;
  42. PMInterfacePointer object;
  43. unsigned int single_use : 1;
  44. };
  45. static CRITICAL_SECTION registered_classes_cs = { NULL, -1, 0, 0, 0, 0 };
  46. static struct list registered_classes = LIST_INIT(registered_classes);
  47. HRESULT __cdecl irpcss_server_register(handle_t h, const GUID *clsid, unsigned int flags,
  48. PMInterfacePointer object, unsigned int *cookie)
  49. {
  50. struct registered_class *entry;
  51. static LONG next_cookie;
  52. if (!(entry = heap_alloc_zero(sizeof(*entry))))
  53. return E_OUTOFMEMORY;
  54. entry->clsid = *clsid;
  55. entry->single_use = !(flags & (REGCLS_MULTIPLEUSE | REGCLS_MULTI_SEPARATE));
  56. if (!(entry->object = heap_alloc(FIELD_OFFSET(MInterfacePointer, abData[object->ulCntData]))))
  57. {
  58. heap_free(entry);
  59. return E_OUTOFMEMORY;
  60. }
  61. entry->object->ulCntData = object->ulCntData;
  62. memcpy(&entry->object->abData, object->abData, object->ulCntData);
  63. *cookie = entry->cookie = InterlockedIncrement(&next_cookie);
  64. EnterCriticalSection(&registered_classes_cs);
  65. list_add_tail(&registered_classes, &entry->entry);
  66. LeaveCriticalSection(&registered_classes_cs);
  67. return S_OK;
  68. }
  69. static void scm_revoke_class(struct registered_class *_class)
  70. {
  71. list_remove(&_class->entry);
  72. heap_free(_class->object);
  73. heap_free(_class);
  74. }
  75. HRESULT __cdecl irpcss_server_revoke(handle_t h, unsigned int cookie)
  76. {
  77. struct registered_class *cur;
  78. EnterCriticalSection(&registered_classes_cs);
  79. LIST_FOR_EACH_ENTRY(cur, &registered_classes, struct registered_class, entry)
  80. {
  81. if (cur->cookie == cookie)
  82. {
  83. scm_revoke_class(cur);
  84. break;
  85. }
  86. }
  87. LeaveCriticalSection(&registered_classes_cs);
  88. return S_OK;
  89. }
  90. HRESULT __cdecl irpcss_get_class_object(handle_t h, const GUID *clsid,
  91. PMInterfacePointer *object)
  92. {
  93. struct registered_class *cur;
  94. *object = NULL;
  95. EnterCriticalSection(&registered_classes_cs);
  96. LIST_FOR_EACH_ENTRY(cur, &registered_classes, struct registered_class, entry)
  97. {
  98. if (!memcmp(clsid, &cur->clsid, sizeof(*clsid)))
  99. {
  100. *object = MIDL_user_allocate(FIELD_OFFSET(MInterfacePointer, abData[cur->object->ulCntData]));
  101. if (*object)
  102. {
  103. (*object)->ulCntData = cur->object->ulCntData;
  104. memcpy((*object)->abData, cur->object->abData, cur->object->ulCntData);
  105. }
  106. if (cur->single_use)
  107. scm_revoke_class(cur);
  108. break;
  109. }
  110. }
  111. LeaveCriticalSection(&registered_classes_cs);
  112. return *object ? S_OK : E_NOINTERFACE;
  113. }
  114. HRESULT __cdecl irpcss_get_thread_seq_id(handle_t h, DWORD *id)
  115. {
  116. static LONG thread_seq_id;
  117. *id = InterlockedIncrement(&thread_seq_id);
  118. return S_OK;
  119. }
  120. static RPC_STATUS RPCSS_Initialize(void)
  121. {
  122. static unsigned short irot_protseq[] = IROT_PROTSEQ;
  123. static unsigned short irot_endpoint[] = IROT_ENDPOINT;
  124. static unsigned short epm_protseq[] = L"ncacn_np";
  125. static unsigned short epm_endpoint[] = L"\\pipe\\epmapper";
  126. static unsigned short epm_protseq_lrpc[] = L"ncalrpc";
  127. static unsigned short epm_endpoint_lrpc[] = L"epmapper";
  128. static unsigned short irpcss_protseq[] = IRPCSS_PROTSEQ;
  129. static unsigned short irpcss_endpoint[] = IRPCSS_ENDPOINT;
  130. static const struct protseq_map
  131. {
  132. unsigned short *protseq;
  133. unsigned short *endpoint;
  134. } protseqs[] =
  135. {
  136. { epm_protseq, epm_endpoint },
  137. { epm_protseq_lrpc, epm_endpoint_lrpc },
  138. { irot_protseq, irot_endpoint },
  139. { irpcss_protseq, irpcss_endpoint },
  140. };
  141. RPC_IF_HANDLE ifspecs[] =
  142. {
  143. epm_v3_0_s_ifspec,
  144. Irot_v0_2_s_ifspec,
  145. Irpcss_v0_0_s_ifspec,
  146. };
  147. RPC_STATUS status;
  148. int i, j;
  149. WINE_TRACE("\n");
  150. for (i = 0, j = 0; i < ARRAY_SIZE(ifspecs); ++i, j = i)
  151. {
  152. status = RpcServerRegisterIf(ifspecs[i], NULL, NULL);
  153. if (status != RPC_S_OK)
  154. goto fail;
  155. }
  156. for (i = 0; i < ARRAY_SIZE(protseqs); ++i)
  157. {
  158. status = RpcServerUseProtseqEpW(protseqs[i].protseq, RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
  159. protseqs[i].endpoint, NULL);
  160. if (status != RPC_S_OK)
  161. goto fail;
  162. }
  163. status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
  164. if (status != RPC_S_OK)
  165. goto fail;
  166. return RPC_S_OK;
  167. fail:
  168. for (i = 0; i < j; ++i)
  169. RpcServerUnregisterIf(ifspecs[i], NULL, FALSE);
  170. return status;
  171. }
  172. static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
  173. {
  174. SERVICE_STATUS status;
  175. status.dwServiceType = SERVICE_WIN32;
  176. status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  177. status.dwWin32ExitCode = 0;
  178. status.dwServiceSpecificExitCode = 0;
  179. status.dwCheckPoint = 0;
  180. status.dwWaitHint = 0;
  181. switch (ctrl)
  182. {
  183. case SERVICE_CONTROL_STOP:
  184. case SERVICE_CONTROL_SHUTDOWN:
  185. TRACE( "shutting down\n" );
  186. RpcMgmtStopServerListening( NULL );
  187. RpcServerUnregisterIf( epm_v3_0_s_ifspec, NULL, TRUE );
  188. RpcServerUnregisterIf( Irot_v0_2_s_ifspec, NULL, TRUE );
  189. status.dwCurrentState = SERVICE_STOP_PENDING;
  190. status.dwControlsAccepted = 0;
  191. SetServiceStatus( service_handle, &status );
  192. SetEvent( exit_event );
  193. return NO_ERROR;
  194. default:
  195. FIXME( "got service ctrl %lx\n", ctrl );
  196. status.dwCurrentState = SERVICE_RUNNING;
  197. SetServiceStatus( service_handle, &status );
  198. return NO_ERROR;
  199. }
  200. }
  201. static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
  202. {
  203. SERVICE_STATUS status;
  204. RPC_STATUS ret;
  205. TRACE( "starting service\n" );
  206. if ((ret = RPCSS_Initialize()))
  207. {
  208. WARN("Failed to initialize rpc interfaces, status %ld.\n", ret);
  209. return;
  210. }
  211. exit_event = CreateEventW( NULL, TRUE, FALSE, NULL );
  212. service_handle = RegisterServiceCtrlHandlerExW( rpcssW, service_handler, NULL );
  213. if (!service_handle) return;
  214. status.dwServiceType = SERVICE_WIN32;
  215. status.dwCurrentState = SERVICE_RUNNING;
  216. status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  217. status.dwWin32ExitCode = 0;
  218. status.dwServiceSpecificExitCode = 0;
  219. status.dwCheckPoint = 0;
  220. status.dwWaitHint = 10000;
  221. SetServiceStatus( service_handle, &status );
  222. WaitForSingleObject( exit_event, INFINITE );
  223. status.dwCurrentState = SERVICE_STOPPED;
  224. status.dwControlsAccepted = 0;
  225. SetServiceStatus( service_handle, &status );
  226. TRACE( "service stopped\n" );
  227. }
  228. int __cdecl wmain( int argc, WCHAR *argv[] )
  229. {
  230. static const SERVICE_TABLE_ENTRYW service_table[] =
  231. {
  232. { rpcssW, ServiceMain },
  233. { NULL, NULL }
  234. };
  235. StartServiceCtrlDispatcherW( service_table );
  236. return 0;
  237. }