retrocl_cn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #include "retrocl.h"
  42. static changeNumber retrocl_internal_cn = 0;
  43. static changeNumber retrocl_first_cn = 0;
  44. /*
  45. * Function: a2changeNumber
  46. *
  47. * Returns: changeNumber (long)
  48. *
  49. * Arguments: string
  50. *
  51. * Description: parses the string to a changenumber. changenumbers are
  52. * positive integers.
  53. *
  54. */
  55. static changeNumber a2changeNumber (const char *p)
  56. {
  57. changeNumber c;
  58. c = strntoul((char *)p,strlen(p),10);
  59. return c;
  60. }
  61. /*
  62. * Function: handle_cnum_entry
  63. * Arguments: op - pointer to Operation struct for this operation
  64. * e - pointer to returned entry.
  65. * Returns: nothing
  66. * Description: Search result handler for retrocl_getchangenum(). Sets the
  67. * op->o_handler_data to point to a structure which contains
  68. * the changenumber retrieved and an error code.
  69. */
  70. static int
  71. handle_cnum_entry( Slapi_Entry *e, void *callback_data )
  72. {
  73. cnumRet *cr = (cnumRet *)callback_data;
  74. Slapi_Value *sval=NULL;
  75. const struct berval *value;
  76. cr->cr_cnum = 0UL;
  77. cr->cr_time = NULL;
  78. if ( NULL != e ) {
  79. Slapi_Attr *chattr = NULL;
  80. sval = NULL;
  81. value = NULL;
  82. if ( slapi_entry_attr_find( e, attr_changenumber, &chattr ) == 0 ) {
  83. slapi_attr_first_value( chattr,&sval );
  84. if ( NULL != sval ) {
  85. value = slapi_value_get_berval ( sval );
  86. if( NULL != value && NULL != value->bv_val &&
  87. '\0' != value->bv_val[0]) {
  88. cr->cr_cnum = a2changeNumber( value->bv_val );
  89. }
  90. }
  91. }
  92. chattr = NULL;
  93. sval = NULL;
  94. value = NULL;
  95. chattr = NULL;
  96. sval = NULL;
  97. value = NULL;
  98. if ( slapi_entry_attr_find( e, attr_changetime, &chattr ) == 0 ) {
  99. slapi_attr_first_value( chattr,&sval );
  100. if ( NULL != sval) {
  101. value = slapi_value_get_berval ( sval );
  102. if (NULL != value && NULL != value->bv_val &&
  103. '\0' != value->bv_val[0]) {
  104. cr->cr_time = slapi_ch_strdup( value->bv_val );
  105. }
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Function: handle_cnum_result
  113. * Arguments: err - error code returned from search
  114. * callback_data - private data for callback
  115. * Returns: nothing
  116. * Description: result handler for retrocl_getchangenum(). Sets the cr_lderr
  117. * field of the cnumRet struct to the error returned
  118. * from the backend.
  119. */
  120. static void
  121. handle_cnum_result( int err, void *callback_data )
  122. {
  123. cnumRet *cr = (cnumRet *)callback_data;
  124. cr->cr_lderr = err;
  125. }
  126. /*
  127. * Function: retrocl_get_changenumbers
  128. *
  129. * Returns: 0/-1
  130. *
  131. * Arguments: none
  132. *
  133. * Description: reads the first and last entry in the changelog to obtain
  134. * the starting and ending change numbers.
  135. *
  136. */
  137. int retrocl_get_changenumbers(void)
  138. {
  139. cnumRet cr;
  140. if (retrocl_be_changelog == NULL) return -1;
  141. cr.cr_cnum = 0;
  142. cr.cr_time = 0;
  143. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_FIRST,
  144. (char *)attr_changenumber, /* cast away const */
  145. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  146. handle_cnum_entry, NULL);
  147. slapi_rwlock_wrlock(retrocl_cn_lock);
  148. retrocl_first_cn = cr.cr_cnum;
  149. slapi_ch_free(( void **) &cr.cr_time );
  150. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  151. (char *)attr_changenumber, /* cast away const */
  152. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  153. handle_cnum_entry, NULL);
  154. retrocl_internal_cn = cr.cr_cnum;
  155. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %lu and %lu\n",
  156. retrocl_first_cn,
  157. retrocl_internal_cn);
  158. slapi_rwlock_unlock(retrocl_cn_lock);
  159. slapi_ch_free(( void **) &cr.cr_time );
  160. return 0;
  161. }
  162. /*
  163. * Function: retrocl_getchangetime
  164. * Arguments: type - one of SLAPI_SEQ_FIRST, SLAPI_SEQ_LAST
  165. * Returns: The time of the requested change record. If the return value is
  166. * NO_TIME, the changelog could not be read.
  167. * If err is non-NULL, the memory it points to is set the the
  168. * error code returned from the backend. If "type" is not valid,
  169. * *err is set to -1.
  170. * Description: Get the first or last changenumber stored in the changelog,
  171. * depending on the value of argument "type".
  172. */
  173. time_t retrocl_getchangetime( int type, int *err )
  174. {
  175. cnumRet cr;
  176. time_t ret;
  177. if ( type != SLAPI_SEQ_FIRST && type != SLAPI_SEQ_LAST ) {
  178. if ( err != NULL ) {
  179. *err = -1;
  180. }
  181. return NO_TIME;
  182. }
  183. memset( &cr, '\0', sizeof( cnumRet ));
  184. slapi_seq_callback( RETROCL_CHANGELOG_DN, type,
  185. (char *)attr_changenumber, /* cast away const */
  186. NULL,
  187. NULL, 0, &cr, NULL,
  188. handle_cnum_result, handle_cnum_entry, NULL );
  189. if ( err != NULL ) {
  190. *err = cr.cr_lderr;
  191. }
  192. if ( NULL == cr.cr_time ) {
  193. ret = NO_TIME;
  194. } else {
  195. ret = parse_localTime( cr.cr_time );
  196. }
  197. slapi_ch_free(( void **) &cr.cr_time );
  198. return ret;
  199. }
  200. /*
  201. * Function: retrocl_forget_changenumbers
  202. *
  203. * Returns: none
  204. *
  205. * Arguments: none
  206. *
  207. * Description: used only when the server is shutting down
  208. *
  209. */
  210. void retrocl_forget_changenumbers(void)
  211. {
  212. slapi_rwlock_wrlock(retrocl_cn_lock);
  213. retrocl_first_cn = 0;
  214. retrocl_internal_cn = 0;
  215. slapi_rwlock_unlock(retrocl_cn_lock);
  216. }
  217. /*
  218. * Function: retrocl_get_first_changenumber
  219. *
  220. * Returns: changeNumber
  221. *
  222. * Arguments: none
  223. *
  224. * Description: used in root DSE
  225. *
  226. */
  227. changeNumber retrocl_get_first_changenumber(void)
  228. {
  229. changeNumber cn;
  230. slapi_rwlock_rdlock(retrocl_cn_lock);
  231. cn = retrocl_first_cn;
  232. slapi_rwlock_unlock(retrocl_cn_lock);
  233. return cn;
  234. }
  235. /*
  236. * Function: retrocl_set_first_changenumber
  237. *
  238. * Returns: none
  239. *
  240. * Arguments: changenumber
  241. *
  242. * Description: used in changelog trimming
  243. *
  244. */
  245. void retrocl_set_first_changenumber(changeNumber cn)
  246. {
  247. slapi_rwlock_wrlock(retrocl_cn_lock);
  248. retrocl_first_cn = cn;
  249. slapi_rwlock_unlock(retrocl_cn_lock);
  250. }
  251. /*
  252. * Function: retrocl_get_last_changenumber
  253. *
  254. * Returns:
  255. *
  256. * Arguments:
  257. *
  258. * Description: used in root DSE
  259. *
  260. */
  261. changeNumber retrocl_get_last_changenumber(void)
  262. {
  263. changeNumber cn;
  264. slapi_rwlock_rdlock(retrocl_cn_lock);
  265. cn = retrocl_internal_cn;
  266. slapi_rwlock_unlock(retrocl_cn_lock);
  267. return cn;
  268. }
  269. /*
  270. * Function: retrocl_commit_changenumber
  271. *
  272. * Returns: none
  273. *
  274. * Arguments: none, lock must be held
  275. *
  276. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  277. *
  278. */
  279. void retrocl_commit_changenumber(void)
  280. {
  281. slapi_rwlock_wrlock(retrocl_cn_lock);
  282. if ( retrocl_first_cn == 0) {
  283. retrocl_first_cn = retrocl_internal_cn;
  284. }
  285. slapi_rwlock_unlock(retrocl_cn_lock);
  286. }
  287. /*
  288. * Function: retrocl_release_changenumber
  289. *
  290. * Returns: none
  291. *
  292. * Arguments: none, lock must be held
  293. *
  294. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  295. *
  296. */
  297. void retrocl_release_changenumber(void)
  298. {
  299. slapi_rwlock_wrlock(retrocl_cn_lock);
  300. retrocl_internal_cn--;
  301. slapi_rwlock_unlock(retrocl_cn_lock);
  302. }
  303. /*
  304. * Function: retrocl_update_lastchangenumber
  305. *
  306. * Returns: 0/-1
  307. *
  308. * Arguments: none. The caller should have taken write lock for the change numbers
  309. *
  310. * Description: reads the last entry in the changelog to obtain
  311. * the last change number.
  312. *
  313. */
  314. int retrocl_update_lastchangenumber(void)
  315. {
  316. cnumRet cr;
  317. if (retrocl_be_changelog == NULL) return -1;
  318. slapi_rwlock_unlock(retrocl_cn_lock);
  319. cr.cr_cnum = 0;
  320. cr.cr_time = 0;
  321. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  322. (char *)attr_changenumber, /* cast away const */
  323. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  324. handle_cnum_entry, NULL);
  325. slapi_rwlock_wrlock(retrocl_cn_lock);
  326. retrocl_internal_cn = cr.cr_cnum;
  327. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %lu \n",
  328. retrocl_internal_cn);
  329. slapi_ch_free(( void **) &cr.cr_time );
  330. return 0;
  331. }
  332. /*
  333. * Function: retrocl_assign_changenumber
  334. *
  335. * Returns: change number, 0 on error
  336. *
  337. * Arguments: none. Lock must be held.
  338. *
  339. * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
  340. * retrocl_release_changenumber
  341. *
  342. */
  343. changeNumber retrocl_assign_changenumber(void)
  344. {
  345. changeNumber cn;
  346. /* Before we assign the changenumber; we should check for the
  347. * validity of the internal assignment of retrocl_internal_cn
  348. * we had from the startup */
  349. slapi_rwlock_wrlock(retrocl_cn_lock);
  350. if(retrocl_internal_cn <= retrocl_first_cn){
  351. /* the numbers have become out of sync - retrocl_get_changenumbers
  352. * gets called only once during startup and it may have had a problem
  353. * getting the last changenumber.
  354. * If there was any problem then update the lastchangenumber from the changelog db.
  355. * This function is being called by only the thread that is actually writing
  356. * to the changelog.
  357. */
  358. retrocl_update_lastchangenumber();
  359. }
  360. retrocl_internal_cn++;
  361. cn = retrocl_internal_cn;
  362. slapi_rwlock_unlock(retrocl_cn_lock);
  363. return cn;
  364. }