uniqueidgen.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* uniqueidgen.c - implementation for uniqueID generator */
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/time.h>
  16. #include <sys/sysinfo.h>
  17. #include <sys/utsname.h>
  18. #include "nspr.h"
  19. #include "slap.h"
  20. #include "uuid.h"
  21. #define MODULE "uniqueid generator"
  22. /* converts from guid -> UniqueID */
  23. /* static void uuid2UniqueID (const guid_t *uuid, Slapi_UniqueID *uId); */
  24. /* converts from UniqueID -> guid */
  25. /* static void uniqueID2uuid (const Slapi_UniqueID *uId, guid_t *uuid); */
  26. /* validates directory */
  27. static int validDir (const char *configDir);
  28. /* Function: uniqueIDGenInit
  29. Description: this function initializes the generator
  30. Parameters: configDir - directory in which generators state is stored
  31. configDN - DIT entry with state information
  32. mtGen - indicates whether multiple threads will use generator
  33. Return: UID_SUCCESS if function succeeds
  34. UID_BADDATA if invalif directory is passed
  35. UID_SYSTEM_ERROR if any other failure occurs
  36. */
  37. int uniqueIDGenInit (const char *configDir, const Slapi_DN *configDN, PRBool mtGen)
  38. {
  39. int rt;
  40. if ((configDN == NULL && (configDir == NULL || !validDir(configDir))) ||
  41. (configDN && configDir))
  42. {
  43. slapi_log_error (SLAPI_LOG_FATAL, MODULE, "uniqueIDGenInit: invalid arguments\n");
  44. return UID_BADDATA;
  45. }
  46. rt = uuid_init (configDir, configDN, mtGen);
  47. if (rt == UUID_SUCCESS)
  48. return UID_SUCCESS;
  49. else
  50. {
  51. slapi_log_error (SLAPI_LOG_FATAL, MODULE, "uniqueIDGenInit: "
  52. "generator initialization failed\n");
  53. return UID_SYSTEM_ERROR;
  54. }
  55. }
  56. /* Function: uniqueIDGenCleanup
  57. Description: cleanup
  58. Parameters: none
  59. Return: none
  60. */
  61. void uniqueIDGenCleanup (){
  62. uuid_cleanup ();
  63. }
  64. /* Function: slapi_uniqueIDGenerate
  65. Description: this function generates UniqueID; exposed to the plugins.
  66. Parameters: uId - structure in which new id will be return
  67. Return: UID_SUCCESS, if operation is successful
  68. UID_BADDATA, if null pointer is passed to the function
  69. UID_SYSTEM_ERROR, if update to persistent storage failed
  70. */
  71. int slapi_uniqueIDGenerate (Slapi_UniqueID *uId){
  72. int rt;
  73. if (uId == NULL)
  74. {
  75. slapi_log_error (SLAPI_LOG_FATAL, MODULE, "uniqueIDGenerate: "
  76. "NULL parameter is passed to the function.\n");
  77. return UID_BADDATA;
  78. }
  79. rt = uuid_create(uId);
  80. if (rt != UUID_SUCCESS)
  81. {
  82. slapi_log_error (SLAPI_LOG_FATAL, MODULE, "uniqueIDGenerate: "
  83. "id generation failed.\n");
  84. return UID_SYSTEM_ERROR;
  85. }
  86. return UID_SUCCESS;
  87. }
  88. /* Function: slapi_uniqueIDGenerateString
  89. Description: this function generates uniqueid an returns it as a string
  90. This function returns the data in the format generated by
  91. slapi_uniqueIDFormat.
  92. Parameters: uId - buffer to receive the ID. Caller is responsible for
  93. freeing uId buffer.
  94. Return: UID_SUCCESS if function succeeds;
  95. UID_BADDATA if invalid pointer passed to the function;
  96. UID_MEMORY_ERROR if malloc fails;
  97. UID_SYSTEM_ERROR update to persistent storage failed.
  98. */
  99. int slapi_uniqueIDGenerateString (char **uId)
  100. {
  101. Slapi_UniqueID uIdTemp;
  102. int rc;
  103. rc = slapi_uniqueIDGenerate (&uIdTemp);
  104. if (rc != UID_SUCCESS)
  105. return rc;
  106. rc = slapi_uniqueIDFormat (&uIdTemp, uId);
  107. return rc;
  108. }
  109. /* Function: slapi_uniqueIDGenerateFromName
  110. Description: this function generates an id from name. See uuid
  111. draft for more details. This function is thread safe.
  112. Parameters: uId - generated id
  113. uIDBase - uid used for generation to distinguish different
  114. name - buffer containing name from which to generate the id
  115. namelen - length of the name buffer
  116. name spaces
  117. Return: UID_SUCCESS if function succeeds
  118. UID_BADDATA if invalid argument is passed to the
  119. function.
  120. */
  121. int slapi_uniqueIDGenerateFromName (Slapi_UniqueID *uId, const Slapi_UniqueID *uIdBase,
  122. const void *name, int namelen)
  123. {
  124. if (uId == NULL || uIdBase == NULL || name == NULL || namelen <= 0)
  125. {
  126. slapi_log_error (SLAPI_LOG_FATAL, MODULE, "uniqueIDGenerateMT: "
  127. "invalid parameter is passed to the function.\n");
  128. return UID_BADDATA;
  129. }
  130. uuid_create_from_name(uId, *uIdBase, name, namelen);
  131. return UID_SUCCESS;
  132. }
  133. /* Function: slapi_uniqueIDGenerateFromName
  134. Description: this function generates an id from a name and returns
  135. it in the string format. See uuid draft for more
  136. details. This function can be used in both a
  137. singlethreaded and a multithreaded environments.
  138. Parameters: uId - generated id in string form
  139. uIDBase - uid used for generation to distinguish among
  140. different name spaces in string form; NULL means to use
  141. empty id as the base.
  142. name - buffer containing name from which to generate the id
  143. namelen - length of the name buffer
  144. Return: UID_SUCCESS if function succeeds
  145. UID_BADDATA if invalid argument is passed to the
  146. function.
  147. */
  148. int slapi_uniqueIDGenerateFromNameString (char **uId,
  149. const char *uIdBase,
  150. const void *name, int namelen)
  151. {
  152. int rc;
  153. Slapi_UniqueID idBase;
  154. Slapi_UniqueID idGen;
  155. /* just use Id of all 0 as base id */
  156. if (uIdBase == NULL)
  157. {
  158. memset (&idBase, 0, sizeof (idBase));
  159. memset (&idGen, 0, sizeof (idGen));
  160. }
  161. else
  162. {
  163. rc = slapi_uniqueIDScan (&idBase, uIdBase);
  164. if (rc != UID_SUCCESS)
  165. {
  166. return rc;
  167. }
  168. }
  169. rc = slapi_uniqueIDGenerateFromName (&idGen, &idBase, name, namelen);
  170. if (rc != UID_SUCCESS)
  171. {
  172. return rc;
  173. }
  174. rc = slapi_uniqueIDFormat (&idGen, uId);
  175. return rc;
  176. }
  177. /* helper fumctions */
  178. static int validDir(const char *configDir){
  179. PRDir* dir;
  180. /* empty string means this directory */
  181. if (strlen(configDir) == 0)
  182. return 1;
  183. dir = PR_OpenDir(configDir);
  184. if (dir){
  185. PR_CloseDir (dir);
  186. return 1;
  187. }
  188. return 0;
  189. }