cl5_config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. /* cl5_config.c - functions to process changelog configuration
  39. */
  40. #include <string.h>
  41. #include <prio.h>
  42. #include "repl5.h"
  43. #include "cl5.h"
  44. #include "cl5_clcache.h" /* To configure the Changelog Cache */
  45. #include "intrinsics.h" /* JCMREPL - Is this bad? */
  46. #ifdef TEST_CL5
  47. #include "cl5_test.h"
  48. #endif
  49. #include "nspr.h"
  50. #include "plstr.h"
  51. #define CONFIG_BASE "cn=changelog5,cn=config" /*"cn=changelog,cn=supplier,cn=replication5.0,cn=replication,cn=config"*/
  52. #define CONFIG_FILTER "(objectclass=*)"
  53. static PRRWLock *s_configLock; /* guarantees that only on thread at a time
  54. modifies changelog configuration */
  55. /* Forward Declartions */
  56. static int changelog5_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  57. static int changelog5_config_modify (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  58. static int changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
  59. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg);
  60. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config);
  61. static changelog5Config * changelog5_dup_config(changelog5Config *config);
  62. static void replace_bslash (char *dir);
  63. static int notify_replica (Replica *r, void *arg);
  64. static int _is_absolutepath (char *dir);
  65. int changelog5_config_init()
  66. {
  67. /* The FE DSE *must* be initialised before we get here */
  68. /* create the configuration lock */
  69. s_configLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "config_lock");
  70. if (s_configLock == NULL)
  71. {
  72. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  73. "changelog5_config_init: failed to create configurationlock; "
  74. "NSPR error - %d\n",PR_GetError ());
  75. return 1;
  76. }
  77. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  78. CONFIG_FILTER, changelog5_config_add, NULL);
  79. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  80. CONFIG_FILTER, changelog5_config_modify, NULL);
  81. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  82. CONFIG_FILTER, dont_allow_that, NULL);
  83. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  84. CONFIG_FILTER, changelog5_config_delete, NULL);
  85. return 0;
  86. }
  87. void changelog5_config_cleanup()
  88. {
  89. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  90. CONFIG_FILTER, changelog5_config_add);
  91. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  92. CONFIG_FILTER, changelog5_config_modify);
  93. slapi_config_remove_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  94. CONFIG_FILTER, dont_allow_that);
  95. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, CONFIG_BASE, LDAP_SCOPE_BASE,
  96. CONFIG_FILTER, changelog5_config_delete);
  97. if (s_configLock)
  98. {
  99. PR_DestroyRWLock (s_configLock);
  100. s_configLock = NULL;
  101. }
  102. }
  103. int changelog5_read_config (changelog5Config *config)
  104. {
  105. int rc = LDAP_SUCCESS;
  106. Slapi_PBlock *pb;
  107. pb = slapi_pblock_new ();
  108. slapi_search_internal_set_pb (pb, CONFIG_BASE, LDAP_SCOPE_BASE, CONFIG_FILTER, NULL, 0, NULL,
  109. NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  110. slapi_search_internal_pb (pb);
  111. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_RESULT, &rc );
  112. if ( LDAP_SUCCESS == rc )
  113. {
  114. Slapi_Entry **entries = NULL;
  115. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
  116. if ( NULL != entries && NULL != entries[0])
  117. {
  118. /* Extract the config info from the changelog entry */
  119. changelog5_extract_config(entries[0], config);
  120. }
  121. }
  122. else
  123. {
  124. memset (config, 0, sizeof (*config));
  125. rc = LDAP_SUCCESS;
  126. }
  127. slapi_free_search_results_internal(pb);
  128. slapi_pblock_destroy(pb);
  129. return rc;
  130. }
  131. void changelog5_config_done (changelog5Config *config)
  132. {
  133. if (config) {
  134. /* slapi_ch_free_string accepts NULL pointer */
  135. slapi_ch_free_string (&config->maxAge);
  136. slapi_ch_free_string (&config->dir);
  137. }
  138. }
  139. void changelog5_config_free (changelog5Config **config)
  140. {
  141. changelog5_config_done(*config);
  142. slapi_ch_free((void **)config);
  143. }
  144. static int
  145. changelog5_config_add (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  146. int *returncode, char *returntext, void *arg)
  147. {
  148. int rc;
  149. changelog5Config config;
  150. *returncode = LDAP_SUCCESS;
  151. PR_RWLock_Wlock (s_configLock);
  152. /* we already have a configured changelog - don't need to do anything
  153. since add operation will fail */
  154. if (cl5GetState () == CL5_STATE_OPEN)
  155. {
  156. *returncode = 1;
  157. if (returntext)
  158. {
  159. strcpy (returntext, "attempt to add changelog when it already exists");
  160. }
  161. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  162. "changelog5_config_add: changelog already exist; "
  163. "request ignored\n");
  164. goto done;
  165. }
  166. changelog5_extract_config(e, &config);
  167. if (config.dir == NULL)
  168. {
  169. *returncode = 1;
  170. if (returntext)
  171. {
  172. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "NULL changelog directory");
  173. }
  174. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  175. "changelog5_config_add: NULL changelog directory\n");
  176. goto done;
  177. }
  178. /* start the changelog */
  179. rc = cl5Open (config.dir, &config.dbconfig);
  180. if (rc != CL5_SUCCESS)
  181. {
  182. *returncode = 1;
  183. if (returntext)
  184. {
  185. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to start changelog; error - %d", rc);
  186. }
  187. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  188. "changelog5_config_add: failed to start changelog\n");
  189. goto done;
  190. }
  191. /* set trimming parameters */
  192. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  193. if (rc != CL5_SUCCESS)
  194. {
  195. *returncode = 1;
  196. if (returntext)
  197. {
  198. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  199. }
  200. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  201. "changelog5_config_add: failed to configure changelog trimming\n");
  202. goto done;
  203. }
  204. /* notify all the replicas that the changelog is configured
  205. so that the can log dummy changes if necessary. */
  206. replica_enumerate_replicas (notify_replica, NULL);
  207. #ifdef TEST_CL5
  208. testChangelog (TEST_ITERATION);
  209. #endif
  210. done:;
  211. PR_RWLock_Unlock (s_configLock);
  212. changelog5_config_done (&config);
  213. if (*returncode == LDAP_SUCCESS)
  214. {
  215. if (returntext)
  216. {
  217. returntext[0] = '\0';
  218. }
  219. return SLAPI_DSE_CALLBACK_OK;
  220. }
  221. return SLAPI_DSE_CALLBACK_ERROR;
  222. }
  223. static int
  224. changelog5_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  225. int *returncode, char *returntext, void *arg)
  226. {
  227. int rc= 0;
  228. LDAPMod **mods;
  229. int i;
  230. changelog5Config config;
  231. changelog5Config * originalConfig = NULL;
  232. char *currentDir = NULL;
  233. *returncode = LDAP_SUCCESS;
  234. /* changelog must be open before its parameters can be modified */
  235. if (cl5GetState() != CL5_STATE_OPEN)
  236. {
  237. if (returntext)
  238. {
  239. strcpy (returntext, "changelog is not configured");
  240. }
  241. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  242. "changelog5_config_modify: changelog is not configured\n");
  243. return SLAPI_DSE_CALLBACK_ERROR;
  244. }
  245. PR_RWLock_Wlock (s_configLock);
  246. /* changelog must be open before its parameters can be modified */
  247. if (cl5GetState() != CL5_STATE_OPEN)
  248. {
  249. *returncode = 1;
  250. if (returntext)
  251. {
  252. strcpy (returntext, "changelog is not configured");
  253. }
  254. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  255. "changelog5_config_modify: changelog is not configured\n");
  256. goto done;
  257. }
  258. /*
  259. * Extract all the original configuration: This is needed to ensure that the configuration
  260. * is trully reloaded. This was not needed before 091401 because the changelog configuration
  261. * was always hardcoded (NULL was being passed to cl5Open). Now we need to ensure we pass to
  262. * cl5Open the proper configuration...
  263. */
  264. changelog5_extract_config(e, &config);
  265. originalConfig = changelog5_dup_config(&config);
  266. /* Reset all the attributes that have been potentially modified by the current MODIFY operation */
  267. slapi_ch_free_string(&config.dir);
  268. config.dir = NULL;
  269. config.maxEntries = CL5_NUM_IGNORE;
  270. slapi_ch_free_string(&config.maxAge);
  271. config.maxAge = slapi_ch_strdup(CL5_STR_IGNORE);
  272. config.dbconfig.maxChCacheEntries = 0;
  273. config.dbconfig.maxChCacheSize = CL5_NUM_IGNORE;
  274. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  275. for (i = 0; mods[i] != NULL; i++)
  276. {
  277. if (mods[i]->mod_op & LDAP_MOD_DELETE)
  278. {
  279. /* We don't support deleting changelog attributes */
  280. }
  281. else
  282. {
  283. int j;
  284. for (j = 0; ((mods[i]->mod_values[j]) && (LDAP_SUCCESS == rc)); j++)
  285. {
  286. char *config_attr, *config_attr_value;
  287. config_attr = (char *) mods[i]->mod_type;
  288. config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
  289. #define ATTR_MODIFIERSNAME "modifiersname"
  290. #define ATTR_MODIFYTIMESTAMP "modifytimestamp"
  291. if ( strcasecmp ( config_attr, ATTR_MODIFIERSNAME ) == 0 ) {
  292. continue;
  293. }
  294. if ( strcasecmp ( config_attr, ATTR_MODIFYTIMESTAMP ) == 0 ) {
  295. continue;
  296. }
  297. /* replace existing value */
  298. if ( strcasecmp (config_attr, CONFIG_CHANGELOG_DIR_ATTRIBUTE ) == 0 )
  299. {
  300. if (config_attr_value && config_attr_value[0] != '\0')
  301. {
  302. slapi_ch_free_string(&config.dir);
  303. config.dir = slapi_ch_strdup(config_attr_value);
  304. replace_bslash (config.dir);
  305. }
  306. else
  307. {
  308. *returncode = 1;
  309. if (returntext)
  310. {
  311. strcpy (returntext, "null changelog directory");
  312. }
  313. goto done;
  314. }
  315. }
  316. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE ) == 0 )
  317. {
  318. if (config_attr_value && config_attr_value[0] != '\0')
  319. {
  320. config.maxEntries = atoi (config_attr_value);
  321. }
  322. else
  323. {
  324. config.maxEntries = 0;
  325. }
  326. }
  327. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE ) == 0 )
  328. {
  329. slapi_ch_free_string(&config.maxAge);
  330. config.maxAge = slapi_ch_strdup(config_attr_value);
  331. }
  332. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_CACHESIZE ) == 0 )
  333. { /* The Changelog Cache Size parameters can be modified online without a need for restart */
  334. if (config_attr_value && config_attr_value[0] != '\0')
  335. {
  336. config.dbconfig.maxChCacheEntries = atoi (config_attr_value);
  337. }
  338. else
  339. {
  340. config.dbconfig.maxChCacheEntries = 0;
  341. }
  342. }
  343. else if ( strcasecmp ( config_attr, CONFIG_CHANGELOG_CACHEMEMSIZE ) == 0 )
  344. { /* The Changelog Cache Size parameters can be modified online without a need for restart */
  345. if (config_attr_value && config_attr_value[0] != '\0')
  346. {
  347. config.dbconfig.maxChCacheSize = atoi (config_attr_value);
  348. }
  349. else
  350. {
  351. config.dbconfig.maxChCacheSize = 0;
  352. }
  353. }
  354. else
  355. {
  356. *returncode = LDAP_UNWILLING_TO_PERFORM;
  357. if (returntext)
  358. {
  359. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  360. "Unwilling to apply %s mods while the server is running", config_attr);
  361. }
  362. goto done;
  363. }
  364. }
  365. }
  366. }
  367. /* Undo the reset above for all the modifiable attributes that were not modified
  368. * except config.dir */
  369. if (config.maxEntries == CL5_NUM_IGNORE)
  370. config.maxEntries = originalConfig->maxEntries;
  371. if (strcmp (config.maxAge, CL5_STR_IGNORE) == 0) {
  372. slapi_ch_free_string(&config.maxAge);
  373. if (originalConfig->maxAge)
  374. config.maxAge = slapi_ch_strdup(originalConfig->maxAge);
  375. }
  376. if (config.dbconfig.maxChCacheEntries == 0)
  377. config.dbconfig.maxChCacheEntries = originalConfig->dbconfig.maxChCacheEntries;
  378. if (config.dbconfig.maxChCacheSize == CL5_NUM_IGNORE)
  379. config.dbconfig.maxChCacheSize = originalConfig->dbconfig.maxChCacheSize;
  380. /* attempt to change chagelog dir */
  381. if (config.dir)
  382. {
  383. currentDir = cl5GetDir ();
  384. if (currentDir == NULL)
  385. {
  386. /* something is wrong: we should never be here */
  387. *returncode = 1;
  388. if (returntext)
  389. {
  390. strcpy (returntext, "internal failure");
  391. }
  392. goto done;
  393. }
  394. #ifdef _WIN32
  395. if (strcasecmp (currentDir, config.dir) != 0)
  396. #else /* On Unix, path are case sensitive */
  397. if (strcmp (currentDir, config.dir) != 0)
  398. #endif
  399. {
  400. if (!_is_absolutepath(config.dir) || (CL5_SUCCESS != cl5CreateDirIfNeeded(config.dir)))
  401. {
  402. *returncode = 1;
  403. if (returntext)
  404. {
  405. PL_strncpyz (returntext, "invalid changelog directory or insufficient access", SLAPI_DSE_RETURNTEXT_SIZE);
  406. }
  407. goto done;
  408. }
  409. /* changelog directory changed - need to remove the
  410. previous changelog and create new one */
  411. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name_cl,
  412. "changelog5_config_modify: changelog directory changed; "
  413. "old dir - %s, new dir - %s; recreating changelog.\n",
  414. currentDir, config.dir);
  415. /* this call will block until all threads using changelog
  416. release changelog by calling cl5RemoveThread () */
  417. rc = cl5Close ();
  418. if (rc != CL5_SUCCESS)
  419. {
  420. *returncode = 1;
  421. if (returntext)
  422. {
  423. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  424. }
  425. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  426. "changelog5_config_modify: failed to close changelog\n");
  427. goto done;
  428. }
  429. rc = cl5Delete (currentDir);
  430. if (rc != CL5_SUCCESS)
  431. {
  432. *returncode = 1;
  433. if (returntext)
  434. {
  435. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  436. }
  437. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  438. "changelog5_config_modify: failed to remove changelog\n");
  439. goto done;
  440. }
  441. rc = cl5Open (config.dir, &config.dbconfig);
  442. if (rc != CL5_SUCCESS)
  443. {
  444. *returncode = 1;
  445. if (returntext)
  446. {
  447. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to restart changelog; error - %d", rc);
  448. }
  449. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  450. "changelog5_config_modify: failed to restart changelog\n");
  451. /* before finishing, let's try to do some error recovery */
  452. if (CL5_SUCCESS != cl5Open(currentDir, &config.dbconfig)) {
  453. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  454. "changelog5_config_modify: failed to restore previous changelog\n");
  455. }
  456. goto done;
  457. }
  458. }
  459. }
  460. /* one of the changelog parameters is modified */
  461. if (config.maxEntries != CL5_NUM_IGNORE ||
  462. strcmp (config.maxAge, CL5_STR_IGNORE) != 0)
  463. {
  464. rc = cl5ConfigTrimming (config.maxEntries, config.maxAge);
  465. if (rc != CL5_SUCCESS)
  466. {
  467. *returncode = 1;
  468. if (returntext)
  469. {
  470. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to configure changelog trimming; error - %d", rc);
  471. }
  472. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  473. "changelog5_config_modify: failed to configure changelog trimming\n");
  474. goto done;
  475. }
  476. }
  477. if (config.dbconfig.maxChCacheEntries != 0 || config.dbconfig.maxChCacheSize != CL5_NUM_IGNORE)
  478. clcache_set_config(&config.dbconfig);
  479. done:;
  480. PR_RWLock_Unlock (s_configLock);
  481. changelog5_config_done (&config);
  482. changelog5_config_free (&originalConfig);
  483. /* slapi_ch_free accepts NULL pointer */
  484. slapi_ch_free ((void**)&currentDir);
  485. if (*returncode == LDAP_SUCCESS)
  486. {
  487. if (returntext)
  488. {
  489. returntext[0] = '\0';
  490. }
  491. return SLAPI_DSE_CALLBACK_OK;
  492. }
  493. return SLAPI_DSE_CALLBACK_ERROR;
  494. }
  495. static int
  496. changelog5_config_delete (Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter,
  497. int *returncode, char *returntext, void *arg)
  498. {
  499. int rc;
  500. char *currentDir = NULL;
  501. *returncode = LDAP_SUCCESS;
  502. /* changelog must be open before it can be deleted */
  503. if (cl5GetState () != CL5_STATE_OPEN)
  504. {
  505. *returncode = 1;
  506. if (returntext)
  507. {
  508. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  509. }
  510. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  511. "changelog5_config_delete: chagelog is not configured\n");
  512. return SLAPI_DSE_CALLBACK_ERROR;
  513. }
  514. PR_RWLock_Wlock (s_configLock);
  515. /* changelog must be open before it can be deleted */
  516. if (cl5GetState () != CL5_STATE_OPEN)
  517. {
  518. *returncode = 1;
  519. if (returntext)
  520. {
  521. PL_strncpyz(returntext, "changelog is not configured", SLAPI_DSE_RETURNTEXT_SIZE);
  522. }
  523. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  524. "changelog5_config_delete: chagelog is not configured\n");
  525. goto done;
  526. }
  527. currentDir = cl5GetDir ();
  528. if (currentDir == NULL)
  529. {
  530. /* something is wrong: we should never be here */
  531. *returncode = 1;
  532. if (returntext)
  533. {
  534. PL_strncpyz (returntext, "internal failure", SLAPI_DSE_RETURNTEXT_SIZE);
  535. }
  536. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  537. "changelog5_config_delete: NULL directory\n");
  538. goto done;
  539. }
  540. /* this call will block until all threads using changelog
  541. release changelog by calling cl5RemoveThread () */
  542. rc = cl5Close ();
  543. if (rc != CL5_SUCCESS)
  544. {
  545. *returncode = 1;
  546. if (returntext)
  547. {
  548. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to close changelog; error - %d", rc);
  549. }
  550. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  551. "changelog5_config_delete: failed to close changelog\n");
  552. goto done;
  553. }
  554. rc = cl5Delete (currentDir);
  555. if (rc != CL5_SUCCESS)
  556. {
  557. *returncode = 1;
  558. if (returntext)
  559. {
  560. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to remove changelog; error - %d", rc);
  561. }
  562. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  563. "changelog5_config_delete: failed to remove changelog\n");
  564. goto done;
  565. }
  566. done:;
  567. PR_RWLock_Unlock (s_configLock);
  568. /* slapi_ch_free accepts NULL pointer */
  569. slapi_ch_free ((void**)&currentDir);
  570. if (*returncode == LDAP_SUCCESS)
  571. {
  572. if (returntext)
  573. {
  574. returntext[0] = '\0';
  575. }
  576. return SLAPI_DSE_CALLBACK_OK;
  577. }
  578. return SLAPI_DSE_CALLBACK_ERROR;
  579. }
  580. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  581. int *returncode, char *returntext, void *arg)
  582. {
  583. *returncode = LDAP_UNWILLING_TO_PERFORM;
  584. return SLAPI_DSE_CALLBACK_ERROR;
  585. }
  586. static changelog5Config * changelog5_dup_config(changelog5Config *config)
  587. {
  588. changelog5Config *dup = (changelog5Config *) slapi_ch_calloc(1, sizeof(changelog5Config));
  589. if (config->dir)
  590. dup->dir = slapi_ch_strdup(config->dir);
  591. if (config->maxAge)
  592. dup->maxAge = slapi_ch_strdup(config->maxAge);
  593. dup->maxEntries = config->maxEntries;
  594. /*memcpy((void *) &dup->dbconfig, (const void *) &config->dbconfig, sizeof(CL5DBConfig));*/
  595. dup->dbconfig.cacheSize = config->dbconfig.cacheSize;
  596. dup->dbconfig.durableTrans = config->dbconfig.durableTrans;
  597. dup->dbconfig.checkpointInterval = config->dbconfig.checkpointInterval;
  598. dup->dbconfig.circularLogging = config->dbconfig.circularLogging;
  599. dup->dbconfig.pageSize = config->dbconfig.pageSize;
  600. dup->dbconfig.logfileSize = config->dbconfig.logfileSize;
  601. dup->dbconfig.maxTxnSize = config->dbconfig.maxTxnSize;
  602. dup->dbconfig.fileMode = config->dbconfig.fileMode;
  603. dup->dbconfig.verbose = config->dbconfig.verbose;
  604. dup->dbconfig.debug = config->dbconfig.debug;
  605. dup->dbconfig.tricklePercentage = config->dbconfig.tricklePercentage;
  606. dup->dbconfig.spinCount = config->dbconfig.spinCount;
  607. dup->dbconfig.maxChCacheEntries = config->dbconfig.maxChCacheEntries;
  608. dup->dbconfig.maxChCacheSize = config->dbconfig.maxChCacheSize;
  609. dup->dbconfig.nb_lock_config = config->dbconfig.nb_lock_config;
  610. return dup;
  611. }
  612. /*
  613. * Given the changelog configuration entry, extract the configuration directives.
  614. */
  615. static void changelog5_extract_config(Slapi_Entry* entry, changelog5Config *config)
  616. {
  617. char *arg;
  618. memset (config, 0, sizeof (*config));
  619. config->dir = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_DIR_ATTRIBUTE);
  620. replace_bslash (config->dir);
  621. arg= slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE);
  622. if (arg)
  623. {
  624. config->maxEntries = atoi (arg);
  625. slapi_ch_free_string(&arg);
  626. }
  627. config->maxAge = slapi_entry_attr_get_charptr(entry,CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  628. /*
  629. * Read the Changelog Internal Configuration Parameters for the Changelog DB
  630. * (db cache size, db settings...)
  631. */
  632. /* Set configuration default values first... */
  633. config->dbconfig.cacheSize = CL5_DEFAULT_CONFIG_DB_DBCACHESIZE;
  634. config->dbconfig.durableTrans = CL5_DEFAULT_CONFIG_DB_DURABLE_TRANSACTIONS;
  635. config->dbconfig.checkpointInterval = CL5_DEFAULT_CONFIG_DB_CHECKPOINT_INTERVAL;
  636. config->dbconfig.circularLogging = CL5_DEFAULT_CONFIG_DB_CIRCULAR_LOGGING;
  637. config->dbconfig.pageSize = CL5_DEFAULT_CONFIG_DB_PAGE_SIZE;
  638. config->dbconfig.logfileSize = CL5_DEFAULT_CONFIG_DB_LOGFILE_SIZE;
  639. config->dbconfig.maxTxnSize = CL5_DEFAULT_CONFIG_DB_TXN_MAX;
  640. config->dbconfig.verbose = CL5_DEFAULT_CONFIG_DB_VERBOSE;
  641. config->dbconfig.debug = CL5_DEFAULT_CONFIG_DB_DEBUG;
  642. config->dbconfig.tricklePercentage = CL5_DEFAULT_CONFIG_DB_TRICKLE_PERCENTAGE;
  643. config->dbconfig.spinCount = CL5_DEFAULT_CONFIG_DB_SPINCOUNT;
  644. config->dbconfig.nb_lock_config = CL5_DEFAULT_CONFIG_NB_LOCK;
  645. /* Now read from the entry to override default values if needed */
  646. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DBCACHESIZE);
  647. if (arg)
  648. {
  649. size_t theSize = atoi (arg);
  650. if (theSize > CL5_MIN_DB_DBCACHESIZE)
  651. config->dbconfig.cacheSize = theSize;
  652. else {
  653. config->dbconfig.cacheSize = CL5_MIN_DB_DBCACHESIZE;
  654. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  655. "Warning: Changelog dbcache size too small. "
  656. "Increasing the Memory Size to %d bytes\n",
  657. CL5_MIN_DB_DBCACHESIZE);
  658. }
  659. slapi_ch_free_string(&arg);
  660. }
  661. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DURABLE_TRANSACTIONS);
  662. if (arg)
  663. {
  664. config->dbconfig.durableTrans = atoi (arg);
  665. slapi_ch_free_string(&arg);
  666. }
  667. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_CHECKPOINT_INTERVAL);
  668. if (arg)
  669. {
  670. config->dbconfig.checkpointInterval = atoi (arg);
  671. slapi_ch_free_string(&arg);
  672. }
  673. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_CIRCULAR_LOGGING);
  674. if (arg)
  675. {
  676. config->dbconfig.circularLogging = atoi (arg);
  677. slapi_ch_free_string(&arg);
  678. }
  679. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_PAGE_SIZE);
  680. if (arg)
  681. {
  682. config->dbconfig.pageSize = atoi (arg);
  683. slapi_ch_free_string(&arg);
  684. }
  685. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_LOGFILE_SIZE);
  686. if (arg)
  687. {
  688. config->dbconfig.logfileSize = atoi (arg);
  689. slapi_ch_free_string(&arg);
  690. }
  691. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_MAXTXN_SIZE);
  692. if (arg)
  693. {
  694. config->dbconfig.maxTxnSize = atoi (arg);
  695. slapi_ch_free_string(&arg);
  696. }
  697. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_VERBOSE);
  698. if (arg)
  699. {
  700. config->dbconfig.verbose = atoi (arg);
  701. slapi_ch_free_string(&arg);
  702. }
  703. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_DEBUG);
  704. if (arg)
  705. {
  706. config->dbconfig.debug = atoi (arg);
  707. slapi_ch_free_string(&arg);
  708. }
  709. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_TRICKLE_PERCENTAGE);
  710. if (arg)
  711. {
  712. config->dbconfig.tricklePercentage = atoi (arg);
  713. slapi_ch_free_string(&arg);
  714. }
  715. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_DB_SPINCOUNT);
  716. if (arg)
  717. {
  718. config->dbconfig.spinCount = atoi (arg);
  719. slapi_ch_free_string(&arg);
  720. }
  721. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_MAX_CONCURRENT_WRITES);
  722. if (arg)
  723. {
  724. config->dbconfig.maxConcurrentWrites = atoi (arg);
  725. slapi_ch_free_string(&arg);
  726. }
  727. if ( config->dbconfig.maxConcurrentWrites <= 0 )
  728. {
  729. config->dbconfig.maxConcurrentWrites = CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES;
  730. }
  731. /*
  732. * Read the Changelog Internal Configuration Parameters for the Changelog Cache
  733. */
  734. /* Set configuration default values first... */
  735. config->dbconfig.maxChCacheEntries = CL5_DEFAULT_CONFIG_CACHESIZE;
  736. config->dbconfig.maxChCacheSize = CL5_DEFAULT_CONFIG_CACHEMEMSIZE;
  737. /* Now read from the entry to override default values if needed */
  738. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_CACHESIZE);
  739. if (arg)
  740. {
  741. config->dbconfig.maxChCacheEntries = atoi (arg);
  742. slapi_ch_free_string(&arg);
  743. }
  744. arg= slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_CACHEMEMSIZE);
  745. if (arg)
  746. {
  747. config->dbconfig.maxChCacheSize = atoi (arg);
  748. slapi_ch_free_string(&arg);
  749. }
  750. arg = slapi_entry_attr_get_charptr(entry, CONFIG_CHANGELOG_NB_LOCK);
  751. if (arg)
  752. {
  753. size_t theSize = atoi(arg);
  754. if (theSize < CL5_MIN_NB_LOCK)
  755. {
  756. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  757. "Warning: Changelog %s value is too low (%d). Set to minimal value instead (%d)\n",
  758. CONFIG_CHANGELOG_NB_LOCK, theSize, CL5_MIN_NB_LOCK);
  759. config->dbconfig.nb_lock_config = CL5_MIN_NB_LOCK;
  760. }
  761. else
  762. {
  763. config->dbconfig.nb_lock_config = theSize;
  764. }
  765. slapi_ch_free_string(&arg);
  766. }
  767. clcache_set_config(&config->dbconfig);
  768. }
  769. static void replace_bslash (char *dir)
  770. {
  771. char *bslash;
  772. if (dir == NULL)
  773. return;
  774. bslash = strchr (dir, '\\');
  775. while (bslash)
  776. {
  777. *bslash = '/';
  778. bslash = strchr (bslash, '\\');
  779. }
  780. }
  781. static int notify_replica (Replica *r, void *arg)
  782. {
  783. return replica_log_ruv_elements (r);
  784. }
  785. static int _is_absolutepath (char * dir)
  786. {
  787. if (dir[0] == '/')
  788. return 1;
  789. #if defined(_WIN32)
  790. if (dir[2] == '/' && dir[1] == ':')
  791. return 1;
  792. #endif
  793. return 0;
  794. }