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.

308 lines
8.3 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. tcreate.c
  5. Abstract:
  6. Test for cluster object creation APIs
  7. Author:
  8. John Vert (jvert) 3-May-1996
  9. Revision History:
  10. --*/
  11. #include "windows.h"
  12. #include "cluster.h"
  13. #include "stdio.h"
  14. #include "stdlib.h"
  15. LPWSTR ClusterName=NULL;
  16. BOOL DoGroup=FALSE;
  17. BOOL DoResource=FALSE;
  18. int FirstParam=0;
  19. LPWSTR Name=NULL;
  20. LPWSTR GroupName=NULL;
  21. LPWSTR ResourceType=NULL;
  22. CHAR UsageText[] =
  23. "TCREATE [-c cluster] type name [resourcegroup resourcetype]\n"
  24. " [-p paramkey:paramname \"paramvalue\"]\n"
  25. " cluster\tspecifies the name of the cluster to connect to\n"
  26. " type\t\teither \"group\" or \"resource\"\n"
  27. " name\t\tthe friendly name of the object\n"
  28. " resourcegroup\tthe name of the group the resource should be created in.\n"
  29. " resourcetype\tthe type of resource to be created\n";
  30. void
  31. Usage(
  32. void
  33. )
  34. {
  35. fprintf(stderr, UsageText);
  36. exit(1);
  37. }
  38. LPWSTR
  39. GetString(
  40. IN LPSTR String
  41. )
  42. {
  43. LPWSTR wString;
  44. DWORD Length;
  45. Length = strlen(String)+1;
  46. wString = malloc(Length*sizeof(WCHAR));
  47. if (wString == NULL) {
  48. fprintf(stderr, "GetString couldn't malloc %d bytes\n",Length*sizeof(WCHAR));
  49. }
  50. mbstowcs(wString, String, Length);
  51. return(wString);
  52. }
  53. void
  54. SetParam(
  55. IN HRESOURCE hRes,
  56. IN LPSTR KeyName,
  57. IN LPSTR ValueName,
  58. IN LPSTR ValueData
  59. )
  60. {
  61. HKEY Key;
  62. HKEY Key2;
  63. HKEY ParamKey;
  64. LONG Status;
  65. LPWSTR wKeyName, wValueName, wValueData;
  66. DWORD Disposition;
  67. DWORD IntCount;
  68. DWORD Number = FALSE;
  69. DWORD Value;
  70. CHAR TestValue[16];
  71. //
  72. // See if we got a number, instead of a string.
  73. //
  74. IntCount = sscanf( ValueData, "%d", &Value );
  75. if ( IntCount == 1 ) {
  76. sprintf( TestValue, "%d\0", Value );
  77. if ( strcmp(TestValue, ValueData) == 0 ) {
  78. Number = TRUE;
  79. }
  80. }
  81. wKeyName = GetString(KeyName);
  82. wValueName = GetString(ValueName);
  83. wValueData = GetString(ValueData);
  84. Key = GetClusterResourceKey(hRes, KEY_READ | KEY_WRITE);
  85. if (Key == NULL) {
  86. fprintf(stderr, "GetResourceKey failed %d\n", GetLastError());
  87. return;
  88. }
  89. Status = ClusterRegOpenKey(Key, L"Parameters", KEY_WRITE, &Key2);
  90. if (Status != ERROR_SUCCESS) {
  91. fprintf(stderr, "SetParam: Couldn't open Parameters key error %d\n", Status);
  92. } else {
  93. Status = ClusterRegCreateKey(Key2,
  94. wKeyName,
  95. 0,
  96. KEY_WRITE,
  97. NULL,
  98. &ParamKey,
  99. &Disposition);
  100. if (Status != ERROR_SUCCESS) {
  101. fprintf(stderr, "SetParam: Couldn't create key %ws error %d\n", wKeyName,Status);
  102. } else {
  103. if ( Number ) {
  104. Status = ClusterRegSetValue(ParamKey,
  105. wValueName,
  106. REG_DWORD,
  107. (CONST BYTE *)&Value,
  108. sizeof(Value));
  109. } else {
  110. Status = ClusterRegSetValue(ParamKey,
  111. wValueName,
  112. REG_SZ,
  113. (CONST BYTE *)wValueData,
  114. (lstrlenW(wValueData)+1)*sizeof(WCHAR));
  115. }
  116. if (Status != ERROR_SUCCESS) {
  117. fprintf(stderr, "SetParam: Couldn't set value %ws in key %ws, error %lx\n",
  118. wValueName, wKeyName, Status);
  119. }
  120. ClusterRegCloseKey(ParamKey);
  121. }
  122. ClusterRegCloseKey(Key2);
  123. }
  124. ClusterRegCloseKey(Key);
  125. free(wKeyName);
  126. free(wValueName);
  127. free(wValueData);
  128. }
  129. void
  130. ParseArgs(
  131. int argc,
  132. char *argv[]
  133. )
  134. {
  135. int i;
  136. DWORD ArgsSeen=0;
  137. for (i=1;i<argc;i++) {
  138. if ((argv[i][0] == '-') ||
  139. (argv[i][0] == '/')) {
  140. switch (argv[i][1]) {
  141. case 'c':
  142. if (++i == argc) {
  143. Usage();
  144. }
  145. ClusterName = GetString(argv[i]);
  146. break;
  147. case 'p':
  148. if ((i+2) >= argc) {
  149. Usage();
  150. }
  151. FirstParam = i+1;
  152. return;
  153. default:
  154. Usage();
  155. break;
  156. }
  157. } else {
  158. switch (ArgsSeen) {
  159. case 0:
  160. //
  161. // type
  162. //
  163. if (_stricmp(argv[i], "group")==0) {
  164. DoGroup = TRUE;
  165. } else if (_stricmp(argv[i], "resource") == 0) {
  166. DoResource = TRUE;
  167. } else {
  168. Usage();
  169. }
  170. ArgsSeen++;
  171. break;
  172. case 1:
  173. Name = GetString(argv[i]);
  174. ArgsSeen++;
  175. break;
  176. case 2:
  177. if (DoGroup) {
  178. Usage();
  179. } else {
  180. GroupName = GetString(argv[i]);
  181. }
  182. ArgsSeen++;
  183. break;
  184. case 3:
  185. ResourceType = GetString(argv[i]);
  186. ArgsSeen++;
  187. break;
  188. default:
  189. Usage();
  190. }
  191. }
  192. }
  193. if (DoGroup) {
  194. if (ArgsSeen != 2) {
  195. Usage();
  196. }
  197. } else if (DoResource) {
  198. if (ArgsSeen != 4) {
  199. Usage();
  200. }
  201. } else {
  202. Usage();
  203. }
  204. }
  205. _cdecl
  206. main (argc, argv)
  207. int argc;
  208. char *argv[];
  209. {
  210. HCLUSTER hClus;
  211. HGROUP hGroup;
  212. HRESOURCE hResource;
  213. LPSTR KeyName, ParamName;
  214. LPSTR ParamValue;
  215. ParseArgs(argc, argv);
  216. hClus = OpenCluster(ClusterName);
  217. if (hClus == NULL) {
  218. fprintf(stderr,
  219. "OpenCluster %ws failed %d\n",
  220. (ClusterName == NULL) ? L"(NULL)" : ClusterName,
  221. GetLastError());
  222. return(0);
  223. }
  224. if (DoGroup) {
  225. hGroup = CreateClusterGroup(hClus, Name);
  226. if (hGroup == NULL) {
  227. fprintf(stderr,
  228. "CreateGroup %ws failed %d\n", Name, GetLastError());
  229. return(0);
  230. }
  231. } else if (DoResource) {
  232. hGroup = OpenClusterGroup(hClus, GroupName);
  233. if (hGroup == NULL) {
  234. fprintf(stderr,
  235. "OpenGroup %ws failed %d\n", GroupName, GetLastError());
  236. return(0);
  237. }
  238. hResource = CreateClusterResource(hGroup,
  239. Name,
  240. ResourceType,
  241. 0);
  242. if (hResource == NULL) {
  243. fprintf(stderr,
  244. "CreateResource %ws failed %d\n", Name, GetLastError());
  245. return(0);
  246. }
  247. while (FirstParam != 0) {
  248. //
  249. // FirstParam is the keyname:paramname.
  250. // FirstParam+1 is the paramvalue
  251. //
  252. KeyName = argv[FirstParam];
  253. ParamValue = argv[FirstParam+1];
  254. ParamName = strchr(KeyName,':');
  255. if (ParamName == NULL) {
  256. Usage();
  257. }
  258. *ParamName++ = '\0';
  259. SetParam(hResource, KeyName, ParamName, ParamValue);
  260. FirstParam += 2;
  261. if (FirstParam == argc) {
  262. break;
  263. }
  264. if (_stricmp(argv[FirstParam], "-p") != 0) {
  265. Usage();
  266. }
  267. if (FirstParam+2 >= argc) {
  268. fprintf(stderr, "firstparam %d, argc %d\n",FirstParam,argc);
  269. Usage();
  270. }
  271. FirstParam+=1;
  272. }
  273. } else {
  274. Usage();
  275. }
  276. return(0);
  277. }