cl4_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* cl4_init.c - implments initialization/cleanup functions for
  42. 4.0 style changelog
  43. */
  44. #include <string.h>
  45. #include "slapi-plugin.h"
  46. #include "cl4.h"
  47. #include "repl.h"
  48. /* forward declarations */
  49. static int changelog4_create_be();
  50. static int changelog4_start_be ();
  51. static int changelog4_close();
  52. static int changelog4_remove();
  53. /*
  54. * Initialise the 4.0 Changelog
  55. */
  56. int changelog4_init ()
  57. {
  58. int rc= 0; /* OK */
  59. Slapi_Backend *rbe;
  60. changeNumber first_change = 0UL, last_change = 0UL;
  61. int lderr;
  62. if (changelog4_create_be() < 0 )
  63. {
  64. rc= -1;
  65. }
  66. else
  67. {
  68. rc = changelog4_start_be ();
  69. }
  70. if(rc == 0)
  71. {
  72. rbe = get_repl_backend();
  73. if(rbe!=NULL)
  74. {
  75. /* We have a Change Log. Check it's valid. */
  76. /* changelog has to be started before its
  77. data version can be read */
  78. const char *sdv= get_server_dataversion();
  79. const char *cdv= get_changelog_dataversion();
  80. char *suffix = changelog4_get_suffix ();
  81. if(!cdv || strcmp(sdv,cdv)!=0)
  82. {
  83. /* The SDV and CDV are not the same. The Change Log is invalid.
  84. It must be removed. */
  85. /* ONREPL - currently we go through this code when the changelog
  86. is first created because we can't tell new backend from the
  87. existing one.*/
  88. rc = changelog4_close();
  89. rc = changelog4_remove();
  90. /* now restart the changelog */
  91. changelog4_start_be ();
  92. create_entity( suffix, "extensibleobject");
  93. /* Write the Server Data Version onto the changelog suffix entry */
  94. /* JCMREPL - And the changelog database version number */
  95. set_changelog_dataversion(sdv);
  96. slapi_ch_free ((void **)&suffix);
  97. }
  98. }
  99. }
  100. if(rc != 0)
  101. {
  102. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  103. "An error occurred configuring the changelog database\n" );
  104. }
  105. first_change = replog_get_firstchangenum( &lderr );
  106. last_change = replog_get_lastchangenum( &lderr );
  107. ldapi_initialize_changenumbers( first_change, last_change );
  108. return rc;
  109. }
  110. static int
  111. changelog4_close()
  112. {
  113. int rc= 0 /* OK */;
  114. Slapi_Backend *rbe= get_repl_backend();
  115. Slapi_PBlock *pb = slapi_pblock_new ();
  116. IFP closefn = NULL;
  117. rc = slapi_be_getentrypoint (rbe, SLAPI_PLUGIN_CLOSE_FN, (void**)&closefn, pb);
  118. if (rc != 0)
  119. {
  120. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  121. "Error: backend close entry point is missing. "
  122. "Replication subsystem disabled.\n");
  123. slapi_pblock_destroy (pb);
  124. set_repl_backend( NULL );
  125. return -1;
  126. }
  127. rc = closefn (pb);
  128. if (rc != 0)
  129. {
  130. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Error: the changelog database could "
  131. "not be closed. Replication subsystem disabled.\n");
  132. set_repl_backend( NULL );
  133. rc = -1;
  134. }
  135. slapi_pblock_destroy (pb);
  136. return rc;
  137. }
  138. static int
  139. changelog4_remove()
  140. {
  141. int rc= 0 /* OK */;
  142. Slapi_Backend *rbe= get_repl_backend();
  143. Slapi_PBlock *pb = slapi_pblock_new ();
  144. IFP rmdbfn = NULL;
  145. rc = slapi_be_getentrypoint (rbe, SLAPI_PLUGIN_DB_RMDB_FN, (void**)&rmdbfn, pb);
  146. if (rc != 0)
  147. {
  148. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  149. "Error: backend rmdb entry point is missing. "
  150. "Replication subsystem disabled.\n");
  151. slapi_pblock_destroy (pb);
  152. set_repl_backend( NULL );
  153. return -1;
  154. }
  155. rc = rmdbfn (pb);
  156. if (rc != 0)
  157. {
  158. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Error: the changelog database could "
  159. "not be removed. Replication subsystem disabled.\n");
  160. rc = -1;
  161. }
  162. else
  163. {
  164. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name, "New database generation computed. "
  165. "Changelog database removed.\n");
  166. }
  167. slapi_pblock_destroy (pb);
  168. return rc;
  169. }
  170. static Slapi_Backend *repl_backend = NULL;
  171. Slapi_Backend
  172. *get_repl_backend()
  173. {
  174. return repl_backend;
  175. }
  176. void
  177. set_repl_backend(Slapi_Backend *be)
  178. {
  179. repl_backend = be;
  180. }
  181. int changelog4_shutdown ()
  182. {
  183. /* ONREPL - will shutdown the backend */
  184. int rc = 1;
  185. return rc;
  186. }
  187. static void changelog4_init_trimming ()
  188. {
  189. char *cl_maxage = changelog4_get_maxage ();
  190. unsigned long cl_maxentries = changelog4_get_maxentries ();
  191. time_t ageval = age_str2time (cl_maxage);
  192. slapi_ch_free ((void **)&cl_maxage);
  193. init_changelog_trimming(cl_maxentries, ageval );
  194. }
  195. /*
  196. * Function: changelog4_create_be
  197. * Arguments: none
  198. * Returns: 0 on success, non-0 on error
  199. * Description: configures changelog backend instance.
  200. */
  201. static int
  202. changelog4_create_be()
  203. {
  204. int i, dir_count = 5;
  205. Slapi_Backend *rbe;
  206. slapi_be_config config;
  207. char *cl_dir = changelog4_get_dir ();
  208. char *cl_suffix;
  209. if ( cl_dir == NULL ) {
  210. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  211. "Error: no directory specified for changelog database.\n");
  212. return -1;
  213. }
  214. cl_suffix = changelog4_get_suffix ();
  215. if ( cl_suffix == NULL ) {
  216. slapi_ch_free ((void **)&cl_dir);
  217. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  218. "Error: no suffix specified for changelog database.\n");
  219. return -1;
  220. }
  221. /* setup configuration parameters for backend initialization */
  222. config.type = CHANGELOG_LDBM_TYPE;
  223. config.suffix = cl_suffix;
  224. config.is_private = 1; /* yes */
  225. config.log_change = 0; /* no */
  226. config.directives = (slapi_config_directive*)slapi_ch_calloc(
  227. dir_count, sizeof(slapi_config_directive));
  228. config.dir_count = dir_count;
  229. for (i = 0; i < dir_count; i++)
  230. {
  231. config.directives[i].file_name = "(internal)";
  232. config.directives[i].lineno = 0;
  233. }
  234. /* setup indexes */
  235. config.directives[0].argv = NULL;
  236. config.directives[0].argc = 3;
  237. charray_add( &(config.directives[0].argv), slapi_ch_strdup( "index" ));
  238. charray_add( &(config.directives[0].argv), slapi_ch_strdup( attr_changenumber ));
  239. charray_add( &(config.directives[0].argv), slapi_ch_strdup( "eq" ));
  240. /* Set up the database directory */
  241. config.directives[1].argv = NULL;
  242. config.directives[1].argc = 2;
  243. charray_add( &(config.directives[1].argv), slapi_ch_strdup( "directory" ));
  244. charray_add( &(config.directives[1].argv), slapi_ch_strdup( cl_dir ));
  245. /* Override the entry cache size */
  246. config.directives[2].argv = NULL;
  247. config.directives[2].argc = 2;
  248. charray_add( &(config.directives[2].argv), slapi_ch_strdup( "cachesize" ));
  249. charray_add( &(config.directives[2].argv), slapi_ch_strdup( "10" ));
  250. /* Override the database cache size */
  251. config.directives[3].argv = NULL;
  252. config.directives[3].argc = 2;
  253. charray_add( &(config.directives[3].argv), slapi_ch_strdup( "dbcachesize" ));
  254. charray_add( &(config.directives[3].argv), slapi_ch_strdup( "1000000" ));
  255. /* Override the allids threshold */
  256. config.directives[4].argv = NULL;
  257. config.directives[4].argc = 2;
  258. charray_add( &(config.directives[4].argv), slapi_ch_strdup( "allidsthreshold" ));
  259. /* assumes sizeof(int) >= 32 bits */
  260. charray_add( &(config.directives[4].argv), slapi_ch_strdup( "2147483647" ));
  261. /* rbe = slapi_be_create_instance(&config, LDBM_TYPE); */
  262. rbe= NULL;
  263. /* free memory allocated to argv */
  264. for (i = 0; i < dir_count; i++)
  265. {
  266. charray_free (config.directives[i].argv);
  267. }
  268. slapi_ch_free ((void **)&config.directives);
  269. slapi_ch_free ((void **)&cl_dir);
  270. slapi_ch_free ((void **)&cl_suffix);
  271. if (rbe == NULL)
  272. {
  273. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  274. "Error: failed to create changelog backend. "
  275. "Replication disabled.\n");
  276. return -1;
  277. }
  278. set_repl_backend (rbe);
  279. changelog4_init_trimming ();
  280. return 0;
  281. }
  282. /* Name: changelog4_start_be
  283. * Parameters: none
  284. * Return: 0 if successful, non 0 otherwise
  285. * Description: starts the changelog backend; backend must be configured
  286. * first via call to changelog4_create_be
  287. */
  288. static int
  289. changelog4_start_be ()
  290. {
  291. int rc;
  292. IFP startfn = NULL;
  293. Slapi_PBlock *pb;
  294. Slapi_Backend *rbe = get_repl_backend ();
  295. if (rbe)
  296. {
  297. pb = slapi_pblock_new();
  298. rc = slapi_be_getentrypoint(rbe, SLAPI_PLUGIN_START_FN, (void**)&startfn, pb);
  299. if (rc != 0)
  300. {
  301. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  302. "Error: backend start entry point is missing. "
  303. "Replication subsystem disabled.\n");
  304. slapi_pblock_destroy (pb);
  305. set_repl_backend( NULL );
  306. return -1;
  307. }
  308. rc = startfn (pb);
  309. slapi_pblock_destroy (pb);
  310. if (rc != 0)
  311. {
  312. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name,
  313. "Error: Failed to start changelog backend. "
  314. "Replication subsystem disabled.\n");
  315. set_repl_backend( NULL );
  316. return -1;
  317. }
  318. }
  319. return 0;
  320. }