retrocl_cn.c 11 KB

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