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.

285 lines
5.9 KiB

  1. #define UNICODE
  2. #include <nt.h>
  3. #include <ntdef.h>
  4. #include <ntrtl.h>
  5. #include <nturtl.h>
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <ntddvol.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include "fs.h"
  12. #include "fsp.h"
  13. #include "fsutil.h"
  14. #include "pipe.h"
  15. //#define ENABLE_SMB
  16. extern
  17. DWORD
  18. FindTransport(LPWSTR TransportId, LPWSTR *Transport);
  19. extern
  20. DWORD
  21. SetupTree(
  22. IN LPTSTR TreeName,
  23. IN LPTSTR DlBuf,
  24. IN OUT DWORD *DlBufSz,
  25. IN LPTSTR TransportName OPTIONAL,
  26. IN LPVOID SecurityDescriptor OPTIONAL
  27. );
  28. HANDLE event;
  29. int Done = FALSE;
  30. BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
  31. {
  32. switch(dwCtrlType) {
  33. case CTRL_C_EVENT:
  34. case CTRL_BREAK_EVENT:
  35. case CTRL_CLOSE_EVENT:
  36. case CTRL_LOGOFF_EVENT:
  37. case CTRL_SHUTDOWN_EVENT:
  38. Done = TRUE;
  39. SetEvent(event);
  40. return TRUE;
  41. default:
  42. return FALSE;
  43. }
  44. }
  45. _cdecl
  46. main(int argc, char *argv[])
  47. {
  48. int cnt;
  49. char cmd[80];
  50. DWORD err;
  51. WCHAR *share = L"root";
  52. HANDLE hdl;
  53. PVOID srvhdl, fshdl, pipehdl;
  54. PVOID fid;
  55. extern BOOLEAN FsReadOnly;
  56. WCHAR *wargv[FsMaxNodes], *tmp[FsMaxNodes];
  57. int i, mask;
  58. LPWSTR tid;
  59. WCHAR localname[MAX_COMPUTERNAME_LENGTH + 20];
  60. DWORD now;
  61. now = GetTickCount();
  62. wcscpy(localname,L"\\\\");
  63. err = sizeof(localname);
  64. if (GetComputerName(&localname[wcslen(localname)], &err) == FALSE) {
  65. return GetLastError();
  66. }
  67. wcscat(localname,L"$QFS\\");
  68. wcscat(localname,share);
  69. if (argc < 2) {
  70. printf("Usage %s: disk1 disk2 ... diskN\n", argv[0]);
  71. return 0;
  72. }
  73. tid = NULL;
  74. err = FindTransport(L"NetBT_Tcpip", &tid);
  75. if (err != ERROR_SUCCESS) {
  76. printf("Unable to find transport to bind1 %d\n", err);
  77. return 0;
  78. }
  79. memset(wargv, 0, sizeof(wargv));
  80. wargv[0] = NULL;
  81. for (i = 1; i < argc; i++) {
  82. int j;
  83. wargv[i] = (WCHAR *) malloc((strlen(argv[i])+1) * sizeof(WCHAR));
  84. j = mbstowcs(wargv[i], argv[i], strlen(argv[i]));
  85. wargv[i][j] = L'\0';
  86. }
  87. FsReadOnly = TRUE;
  88. err = FsInit(NULL, &fshdl);
  89. if (err != STATUS_SUCCESS) {
  90. printf("Unable to init filesystem %d\n", err);
  91. return 0;
  92. }
  93. err = PipeInit(0,fshdl,&pipehdl);
  94. if (err != STATUS_SUCCESS) {
  95. printf("Unable to init server %d\n", err);
  96. return 0;
  97. }
  98. err = SrvInit(NULL, fshdl, &srvhdl);
  99. if (err != STATUS_SUCCESS) {
  100. printf("Unable to init server %d\n", err);
  101. return 0;
  102. }
  103. err = FsRegister(fshdl, share, wargv[1], wargv, argc-1, &fid);
  104. if (err != STATUS_SUCCESS) {
  105. printf("Unable to register share %d\n", err);
  106. return 0;
  107. }
  108. regain:
  109. // arb
  110. event = CreateEvent(NULL, FALSE, FALSE, NULL);
  111. err = FsArbitrate(fid, event, &hdl);
  112. if (err == ERROR_IO_PENDING || err == ERROR_PATH_BUSY) {
  113. HANDLE a[2];
  114. a[0] = hdl;
  115. a[1] = event;
  116. err = WaitForMultipleObjects(2, a, FALSE, 45 * 1000);
  117. if (err != WAIT_TIMEOUT) {
  118. // check if we got it or not
  119. err = FsIsQuorum(fid);
  120. } else {
  121. // our time ran out, cancel it now
  122. printf("Arb timedout\n");
  123. FsCancelArbitration(fid);
  124. err = ERROR_CANCELLED;
  125. }
  126. }
  127. if (err != ERROR_SUCCESS) {
  128. printf("Arbitration failed %d\n", err); Sleep(5*1000);
  129. goto regain;
  130. return 1;
  131. }
  132. printf("Arb in %d msec\n", GetTickCount() - now);
  133. {
  134. HANDLE fd;
  135. sprintf(cmd, "\\\\?\\%s\\foo.txt", argv[1]);
  136. fd = CreateFileA(cmd, GENERIC_READ|GENERIC_WRITE,
  137. FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
  138. if (fd == INVALID_HANDLE_VALUE) {
  139. printf("Open failed %d\n", GetLastError());
  140. }
  141. }
  142. // online filesystem before onlining its network name
  143. while (FsIsOnline(fid) == ERROR_IO_PENDING)
  144. Sleep(1*1000);
  145. err = PipeOnline(pipehdl, share);
  146. if (err != STATUS_SUCCESS) {
  147. printf("Unable to online pipeserver %d\n", err);
  148. return 0;
  149. }
  150. #ifdef ENABLE_SMB
  151. // online
  152. err = SrvOnline(srvhdl, NULL, 0);
  153. if (err != ERROR_SUCCESS) {
  154. printf("Srv Online failed %d\n", err);
  155. return 1;
  156. }
  157. #endif
  158. #ifdef ENABLE_SMB
  159. // Now we need to connect tree
  160. retry:
  161. err = SetupTree(localname, NULL, NULL, tid, NULL);
  162. if (err != ERROR_SUCCESS) {
  163. printf("Unable to setup tree '%S' %d\n", localname, err);
  164. if (err == ERROR_PATH_NOT_FOUND) {
  165. Sleep(1000 * 2);
  166. goto retry;
  167. }
  168. return err;
  169. }
  170. #endif
  171. SetConsoleCtrlHandler(HandlerRoutine, TRUE);
  172. printf("Server is up.\n");
  173. mask = 0x2;
  174. while (Done == FALSE) {
  175. int cnt;
  176. if (FsReserve(fid) != ERROR_SUCCESS) {
  177. printf("Lost reservation!\n");
  178. break;
  179. }
  180. err = FsIsOnline(fid);
  181. if ( err != ERROR_SUCCESS && err != ERROR_IO_PENDING) {
  182. printf("Fs offlined %d!\n", err);
  183. }
  184. // Every 5 seconds change replica set
  185. WaitForSingleObject(event, 5 * 1000);
  186. #if 0
  187. memset(tmp, 0, sizeof(tmp));
  188. cnt = 0;
  189. for (i = 1; i < argc; i++) {
  190. if (mask & (1 << i)) {
  191. cnt++;
  192. printf("New replica %d '%S'\n", i, wargv[i]);
  193. tmp[i] = wargv[i];
  194. }
  195. }
  196. printf("Changing set %x size %d\n", mask, cnt);
  197. FsUpdateReplicaSet(fid, tmp, cnt);
  198. mask += 2;
  199. if (cnt >= (argc - 1)) {
  200. mask = 2;
  201. }
  202. #endif
  203. }
  204. printf("Exiting...\n");
  205. // offline
  206. #ifdef ENABLE_SMB
  207. SrvOffline(srvhdl);
  208. #endif
  209. PipeOffline(pipehdl);
  210. // release
  211. FsRelease(fid);
  212. SrvExit(srvhdl);
  213. PipeExit(pipehdl);
  214. FsExit(fshdl);
  215. return 0;
  216. }
  217. #include <stdlib.h>
  218. #include <stdarg.h>
  219. void
  220. debug_log(char *format, ...)
  221. {
  222. va_list marker;
  223. va_start(marker, format);
  224. printf("%d:%x:",GetTickCount(), GetCurrentThreadId());
  225. vprintf(format, marker);
  226. va_end(marker);
  227. }
  228. void
  229. error_log(char *format, ...)
  230. {
  231. va_list marker;
  232. va_start(marker, format);
  233. printf("*E %d:%x:",GetTickCount(), GetCurrentThreadId());
  234. vprintf(format, marker);
  235. va_end(marker);
  236. }