retrocl_cn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include "retrocl.h"
  39. static changeNumber retrocl_internal_cn = 0;
  40. static changeNumber retrocl_first_cn = 0;
  41. PRLock *retrocl_internal_lock = NULL;
  42. /*
  43. * Function: a2changeNumber
  44. *
  45. * Returns: changeNumber (long)
  46. *
  47. * Arguments: string
  48. *
  49. * Description: parses the string to a changenumber. changenumbers are
  50. * positive integers.
  51. *
  52. */
  53. static changeNumber a2changeNumber (const char *p)
  54. {
  55. changeNumber c;
  56. c = strntoul((char *)p,strlen(p),10);
  57. return c;
  58. }
  59. /*
  60. * Function: handle_cnum_entry
  61. * Arguments: op - pointer to Operation struct for this operation
  62. * e - pointer to returned entry.
  63. * Returns: nothing
  64. * Description: Search result handler for retrocl_getchangenum(). Sets the
  65. * op->o_handler_data to point to a structure which contains
  66. * the changenumber retrieved and an error code.
  67. */
  68. static int
  69. handle_cnum_entry( Slapi_Entry *e, void *callback_data )
  70. {
  71. cnumRet *cr = (cnumRet *)callback_data;
  72. Slapi_Value *sval=NULL;
  73. const struct berval *value;
  74. cr->cr_cnum = 0UL;
  75. cr->cr_time = NULL;
  76. if ( NULL != e ) {
  77. Slapi_Attr *chattr = NULL;
  78. sval = NULL;
  79. value = NULL;
  80. if ( slapi_entry_attr_find( e, attr_changenumber, &chattr ) == 0 ) {
  81. slapi_attr_first_value( chattr,&sval );
  82. if ( NULL != sval ) {
  83. value = slapi_value_get_berval ( sval );
  84. if( NULL != value && NULL != value->bv_val &&
  85. '\0' != value->bv_val[0]) {
  86. cr->cr_cnum = a2changeNumber( value->bv_val );
  87. }
  88. }
  89. }
  90. chattr = NULL;
  91. sval = NULL;
  92. value = NULL;
  93. chattr = NULL;
  94. sval = NULL;
  95. value = NULL;
  96. if ( slapi_entry_attr_find( e, attr_changetime, &chattr ) == 0 ) {
  97. slapi_attr_first_value( chattr,&sval );
  98. if ( NULL != sval) {
  99. value = slapi_value_get_berval ( sval );
  100. if (NULL != value && NULL != value->bv_val &&
  101. '\0' != value->bv_val[0]) {
  102. cr->cr_time = slapi_ch_strdup( value->bv_val );
  103. }
  104. }
  105. }
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Function: handle_cnum_result
  111. * Arguments: err - error code returned from search
  112. * callback_data - private data for callback
  113. * Returns: nothing
  114. * Description: result handler for retrocl_getchangenum(). Sets the cr_lderr
  115. * field of the cnumRet struct to the error returned
  116. * from the backend.
  117. */
  118. static void
  119. handle_cnum_result( int err, void *callback_data )
  120. {
  121. cnumRet *cr = (cnumRet *)callback_data;
  122. cr->cr_lderr = err;
  123. }
  124. /*
  125. * Function: retrocl_get_changenumbers
  126. *
  127. * Returns: 0/-1
  128. *
  129. * Arguments: none
  130. *
  131. * Description: reads the first and last entry in the changelog to obtain
  132. * the starting and ending change numbers.
  133. *
  134. */
  135. int retrocl_get_changenumbers(void)
  136. {
  137. cnumRet cr;
  138. if (retrocl_internal_lock == NULL) {
  139. retrocl_internal_lock = PR_NewLock();
  140. if (retrocl_internal_lock == NULL) return -1;
  141. }
  142. if (retrocl_be_changelog == NULL) return -1;
  143. cr.cr_cnum = 0;
  144. cr.cr_time = 0;
  145. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_FIRST,
  146. (char *)attr_changenumber, /* cast away const */
  147. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  148. handle_cnum_entry, NULL);
  149. retrocl_first_cn = cr.cr_cnum;
  150. slapi_ch_free(( void **) &cr.cr_time );
  151. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  152. (char *)attr_changenumber, /* cast away const */
  153. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  154. handle_cnum_entry, NULL);
  155. retrocl_internal_cn = cr.cr_cnum;
  156. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %d and %d\n",
  157. retrocl_first_cn,
  158. retrocl_internal_cn);
  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. if (retrocl_internal_lock == NULL) return;
  213. PR_Lock(retrocl_internal_lock);
  214. retrocl_first_cn = 0;
  215. retrocl_internal_cn = 0;
  216. PR_Unlock(retrocl_internal_lock);
  217. }
  218. /*
  219. * Function: retrocl_get_first_changenumber
  220. *
  221. * Returns: changeNumber
  222. *
  223. * Arguments: none
  224. *
  225. * Description: used in root DSE
  226. *
  227. */
  228. changeNumber retrocl_get_first_changenumber(void)
  229. {
  230. changeNumber cn;
  231. PR_Lock(retrocl_internal_lock);
  232. cn = retrocl_first_cn;
  233. PR_Unlock(retrocl_internal_lock);
  234. return cn;
  235. }
  236. /*
  237. * Function: retrocl_set_first_changenumber
  238. *
  239. * Returns: none
  240. *
  241. * Arguments: changenumber
  242. *
  243. * Description: used in changelog trimming
  244. *
  245. */
  246. void retrocl_set_first_changenumber(changeNumber cn)
  247. {
  248. PR_Lock(retrocl_internal_lock);
  249. retrocl_first_cn = cn;
  250. PR_Unlock(retrocl_internal_lock);
  251. }
  252. /*
  253. * Function: retrocl_get_last_changenumber
  254. *
  255. * Returns:
  256. *
  257. * Arguments:
  258. *
  259. * Description: used in root DSE
  260. *
  261. */
  262. changeNumber retrocl_get_last_changenumber(void)
  263. {
  264. changeNumber cn;
  265. PR_Lock(retrocl_internal_lock);
  266. cn = retrocl_internal_cn;
  267. PR_Unlock(retrocl_internal_lock);
  268. return cn;
  269. }
  270. /*
  271. * Function: retrocl_commit_changenumber
  272. *
  273. * Returns: none
  274. *
  275. * Arguments: none, lock must be held
  276. *
  277. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  278. *
  279. */
  280. void retrocl_commit_changenumber(void)
  281. {
  282. if ( retrocl_first_cn == 0) {
  283. retrocl_first_cn = retrocl_internal_cn;
  284. }
  285. }
  286. /*
  287. * Function: retrocl_release_changenumber
  288. *
  289. * Returns: none
  290. *
  291. * Arguments: none, lock must be held
  292. *
  293. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  294. *
  295. */
  296. void retrocl_release_changenumber(void)
  297. {
  298. retrocl_internal_cn--;
  299. }
  300. /*
  301. * Function: retrocl_update_lastchangenumber
  302. *
  303. * Returns: 0/-1
  304. *
  305. * Arguments: none
  306. *
  307. * Description: reads the last entry in the changelog to obtain
  308. * the last change number.
  309. *
  310. */
  311. int retrocl_update_lastchangenumber(void)
  312. {
  313. cnumRet cr;
  314. if (retrocl_internal_lock == NULL) {
  315. retrocl_internal_lock = PR_NewLock();
  316. if (retrocl_internal_lock == NULL) return -1;
  317. }
  318. if (retrocl_be_changelog == NULL) return -1;
  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. retrocl_internal_cn = cr.cr_cnum;
  326. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %d \n",
  327. retrocl_internal_cn);
  328. slapi_ch_free(( void **) &cr.cr_time );
  329. return 0;
  330. }
  331. /*
  332. * Function: retrocl_assign_changenumber
  333. *
  334. * Returns: change number, 0 on error
  335. *
  336. * Arguments: none. Lock must be held.
  337. *
  338. * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
  339. * retrocl_release_changenumber
  340. *
  341. */
  342. changeNumber retrocl_assign_changenumber(void)
  343. {
  344. changeNumber cn;
  345. if (retrocl_internal_lock == NULL) return 0;
  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. if(retrocl_internal_cn <= retrocl_first_cn){
  350. /* the numbers have become out of sync - retrocl_get_changenumbers
  351. * gets called only once during startup and it may have had a problem
  352. * getting the last changenumber.
  353. * If there was any problem then update the lastchangenumber from the changelog db.
  354. * This function is being called by only the thread that is actually writing
  355. * to the changelog.
  356. */
  357. retrocl_update_lastchangenumber();
  358. }
  359. retrocl_internal_cn++;
  360. cn = retrocl_internal_cn;
  361. return cn;
  362. }