Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

396 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Function: main
  4. //
  5. // Arguments:
  6. // argc, argv: the passed in argument list.
  7. //
  8. // Description: This routine initializes the dfs server, and creates
  9. // a worker thread that will be responsible for periodic
  10. // work (such as scavenging and refreshing). It then calls
  11. // into the RPC code to start processing client requests.
  12. //
  13. //--------------------------------------------------------------------------
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21. #include <shellapi.h>
  22. #include "ReferralTest.hxx"
  23. #include "DfsReferralData.h"
  24. #include <DfsServerLibrary.hxx>
  25. #include "dfswmi.h"
  26. #define WPP_BIT_CLI_DRV 0x01
  27. #define WPP_LEVEL_FLAGS_LOGGER(lvl,flags) WPP_LEVEL_LOGGER(flags)
  28. #define WPP_LEVEL_FLAGS_ENABLED(lvl, flags) (WPP_LEVEL_ENABLED(flags) && WPP_CONTROL(WPP_BIT_CLI_DRV ).Level >= lvl)
  29. #define WPP_LEVEL_ERROR_FLAGS_LOGGER(lvl, error, flags) WPP_LEVEL_LOGGER(flags)
  30. #define WPP_LEVEL_ERROR_FLAGS_ENABLED(lvl, error, flags) \
  31. ((!NT_SUCCESS(error) || WPP_LEVEL_ENABLED(flags)) && WPP_CONTROL(WPP_BIT_CLI_DRV ).Level >= lvl)
  32. #include "referraltest.tmh"
  33. extern
  34. void
  35. SetReferralControl(WPP_CB_TYPE * Control);
  36. DWORD
  37. RpcInit();
  38. DFSSTATUS
  39. ProcessCommandLineArg( LPWSTR Arg );
  40. VOID
  41. ReferralServerUsage();
  42. VOID
  43. TestLinks();
  44. DWORD
  45. DfsTestThread(LPVOID TData );
  46. #define UNICODE_PATH_SEP L'\\'
  47. ULONG MaxLinks = sizeof(DfsTestLinks) /sizeof(struct _DFS_TEST_LINK);
  48. INT64 TotalRef = 0;
  49. #define MAKEARG(x) \
  50. WCHAR Arg##x[] = L"/" L#x L":"; \
  51. LONG ArgLen##x = (sizeof(Arg##x) / sizeof(WCHAR)) - 1; \
  52. BOOLEAN fArg##x;
  53. #define SWITCH(x) \
  54. WCHAR Switch##x[] = L"/" L#x ; \
  55. BOOLEAN fSwitch##x;
  56. //
  57. // The arguments we accept at commandline.
  58. //
  59. MAKEARG(Name);
  60. SWITCH(L);
  61. SWITCH(D);
  62. SWITCH(M);
  63. SWITCH(Trace);
  64. ULONG Flags;
  65. _cdecl
  66. main(int argc, char *argv[])
  67. {
  68. UNREFERENCED_PARAMETER(argv);
  69. UNREFERENCED_PARAMETER(argc);
  70. LPWSTR CommandLine;
  71. LPWSTR *argvw;
  72. DFSSTATUS Status = ERROR_SUCCESS;
  73. int argcw,i;
  74. WPP_CB_TYPE *pLogger = NULL;
  75. pLogger = &WPP_CB[WPP_CTRL_NO(WPP_BIT_CLI_DRV)];
  76. WPP_INIT_TRACING(L"referralTest");
  77. SetReferralControl(pLogger);
  78. //
  79. // Get the command line in Unicode
  80. //
  81. CommandLine = GetCommandLine();
  82. argvw = CommandLineToArgvW(CommandLine, &argcw);
  83. //
  84. // Process each argument on the command line.
  85. //
  86. for (i = 1; i < argcw; i++) {
  87. Status = ProcessCommandLineArg(argvw[i]);
  88. if (fArgName == TRUE && fSwitchL == TRUE)
  89. {
  90. printf("/L and /Name: are mutually exclusive");
  91. Status = ERROR_INVALID_PARAMETER;
  92. }
  93. if (Status != ERROR_SUCCESS)
  94. {
  95. ReferralServerUsage();
  96. break;
  97. }
  98. }
  99. if (Status == ERROR_SUCCESS)
  100. {
  101. //
  102. // Initialize the server.
  103. //
  104. Status = DfsServerInitialize( Flags );
  105. if (Status != ERROR_SUCCESS) {
  106. printf("Cannot continue: dfs server init error %d\n", Status);
  107. exit(-1);
  108. }
  109. }
  110. Sleep(200);
  111. for (i = 0; i < 10; i++) {
  112. if (Status == ERROR_SUCCESS) {
  113. HANDLE THandle;
  114. DWORD Tid;
  115. THandle = CreateThread (
  116. NULL,
  117. 0,
  118. DfsTestThread,
  119. 0,
  120. 0,
  121. &Tid);
  122. if (THandle != NULL) {
  123. CloseHandle(THandle);
  124. printf("Created Test thread tid %d\n", Tid);
  125. }
  126. else {
  127. printf("Unable to create test thread\n");
  128. Status = GetLastError();
  129. }
  130. }
  131. }
  132. while (1) {
  133. Sleep(300000000);
  134. }
  135. WPP_CLEANUP();
  136. }
  137. //+-------------------------------------------------------------------------
  138. //
  139. // Function: ProcessCommandLineArg - process the command line
  140. //
  141. // Arguments: Arg - the argument to process
  142. //
  143. // Returns: Status
  144. // ERROR_SUCCESS on success
  145. // ERROR status code otherwise
  146. //
  147. //
  148. // Description: This routine inteprets the passed in argument and
  149. // sets appropriate flags with which the server should
  150. // be initialized.
  151. //
  152. //--------------------------------------------------------------------------
  153. DFSSTATUS
  154. ProcessCommandLineArg( LPWSTR Arg )
  155. {
  156. LONG ArgLen;
  157. DFSSTATUS Status = ERROR_SUCCESS;
  158. LPWSTR NameSpace;
  159. if (Arg == NULL) {
  160. Status = ERROR_INVALID_PARAMETER;
  161. }
  162. if (Status == ERROR_SUCCESS)
  163. {
  164. ArgLen = wcslen(Arg);
  165. if (_wcsnicmp(Arg, ArgName, ArgLenName) == 0)
  166. {
  167. fArgName = TRUE;
  168. NameSpace = &Arg[ArgLenName];
  169. if (wcslen(NameSpace) == 0)
  170. {
  171. Status = ERROR_INVALID_PARAMETER;
  172. }
  173. else {
  174. Status = DfsAddHandledNamespace( NameSpace, TRUE );
  175. }
  176. }
  177. else if (_wcsicmp(Arg, SwitchTrace) == 0)
  178. {
  179. fSwitchTrace = TRUE;
  180. }
  181. else if (_wcsicmp(Arg, SwitchL) == 0)
  182. {
  183. fSwitchL = TRUE;
  184. Flags |= DFS_LOCAL_NAMESPACE;
  185. }
  186. else if (_wcsicmp(Arg, SwitchD) == 0)
  187. {
  188. Flags |= DFS_CREATE_DIRECTORIES;
  189. }
  190. else if (_wcsicmp(Arg, SwitchM) == 0)
  191. {
  192. Flags |= DFS_MIGRATE;
  193. }
  194. else {
  195. Status = STATUS_INVALID_PARAMETER;
  196. }
  197. }
  198. return Status;
  199. }
  200. //
  201. // Function: ReferralServerUsage. Usage printout for the referral server.
  202. //
  203. VOID
  204. ReferralServerUsage()
  205. {
  206. printf("Usage:\n");
  207. printf("/D - Create directories\n");
  208. printf("/L - Run on the local root server\n");
  209. printf("/M - Migrate existing DFS to allow multiple roots\n");
  210. printf("/Name:<Namespace> - Handle referrals to the specified namespace\n");
  211. return;
  212. }
  213. DWORD
  214. DfsTestThread(LPVOID TData )
  215. {
  216. srand( (unsigned)GetCurrentThreadId());
  217. INT64 TotalThreadRef = 0;
  218. INT64 y;
  219. UNREFERENCED_PARAMETER(TData);
  220. while (1) {
  221. TestLinks();
  222. Sleep(20);
  223. TotalThreadRef += 5;
  224. TotalRef += 5;
  225. y = TotalRef;
  226. if ((TotalThreadRef % 1000) == 0) {
  227. printf("Thread %d, Ref %I64d\n", GetCurrentThreadId(), TotalThreadRef);
  228. }
  229. if ((y % 10000) == 0) {
  230. printf("Total Referals %I64d\n", TotalRef);
  231. }
  232. }
  233. return 0;
  234. }
  235. DFSSTATUS
  236. DfsGenerateReferralBySitenameForTesting(
  237. LPWSTR LinkName,
  238. LPWSTR SiteName,
  239. DWORD NumReplicasToReturn,
  240. ULONG CostLimit,
  241. REFERRAL_HEADER ** ppReferralHeader);
  242. VOID
  243. DfsReleaseReferral(
  244. REFERRAL_HEADER *pReferralHeader);
  245. LPWSTR MySite=L"Red-Bldg40";
  246. VOID
  247. TestLinks()
  248. {
  249. UNICODE_STRING Name, Remaining;
  250. UNICODE_STRING LinkName, LinkRemains;
  251. REFERRAL_HEADER *pReferral;
  252. ULONG i,j;
  253. DFSSTATUS Status;
  254. BOOLEAN LinkMatches, RemainsMatches, ReplicaMatches;
  255. for (j = 0; j < 5; j++) {
  256. i = (unsigned)(GetCurrentThreadId());
  257. i += rand();
  258. i = i % MaxLinks;
  259. Status = DfsGenerateReferralBySitenameForTesting( DfsTestLinks[i].Name,
  260. MySite,
  261. 1000,
  262. 1000,
  263. &pReferral );
  264. if (Status == ERROR_SUCCESS) {
  265. if (pReferral == NULL)
  266. {
  267. DbgBreakPoint();
  268. }
  269. RtlInitUnicodeString(&Name, &DfsTestLinks[i].Name[0]);
  270. RtlInitUnicodeString(&Remaining, &DfsTestLinks[i].RemainingName[0]);
  271. RtlInitUnicodeString(&LinkRemains, NULL);
  272. LinkName.Buffer = pReferral->LinkName;
  273. LinkName.Length = LinkName.MaximumLength = (USHORT)pReferral->LinkNameLength;
  274. LinkMatches = FALSE;
  275. RemainsMatches = FALSE;
  276. ReplicaMatches = FALSE;
  277. if (RtlPrefixUnicodeString(&LinkName, &Name, TRUE) == TRUE)
  278. {
  279. LinkMatches = TRUE;
  280. LinkRemains.Buffer = &Name.Buffer[(LinkName.Length / sizeof(WCHAR))];
  281. LinkRemains.MaximumLength = LinkRemains.Length = Name.Length - LinkName.Length;
  282. if (LinkRemains.Length && LinkRemains.Buffer[0] == UNICODE_PATH_SEP)
  283. {
  284. LinkRemains.Buffer++;
  285. LinkRemains.Length -= sizeof(WCHAR);
  286. }
  287. if (RtlCompareUnicodeString(&Remaining, &LinkRemains, TRUE) == 0)
  288. {
  289. RemainsMatches = TRUE;
  290. }
  291. if (DfsTestLinks[i].NumberReplicas == pReferral->ReplicaCount)
  292. {
  293. ReplicaMatches = TRUE;
  294. }
  295. }
  296. if (!LinkMatches || !RemainsMatches || !ReplicaMatches)
  297. {
  298. printf("Link Matches %x Rem %x Rep %x\n",
  299. LinkMatches, RemainsMatches, ReplicaMatches );
  300. printf("Number of replicas %d, number got %d\n",
  301. DfsTestLinks[i].NumberReplicas, pReferral->ReplicaCount);
  302. printf("LinkRem buffer %p. Name buf %p Linkn len %x name len %x Linkrem len %x remain len %x\n",
  303. LinkRemains.Buffer, Name.Buffer, LinkName.Length, Name.Length, LinkRemains.Length, Remaining.Length);
  304. printf("Name %wZ Link %wZ Remains %wZ LinkRemains %wZ\n",
  305. &Name, &LinkName, &Remaining, &LinkRemains);
  306. }
  307. DfsReleaseReferral(pReferral);
  308. }
  309. else{
  310. printf("Load for %wS Failde with %x\n", DfsTestLinks[i].Name, Status);
  311. }
  312. }
  313. }