retrocl_cn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. retrocl_first_cn = cr.cr_cnum;
  148. slapi_ch_free(( void **) &cr.cr_time );
  149. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  150. (char *)attr_changenumber, /* cast away const */
  151. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  152. handle_cnum_entry, NULL);
  153. retrocl_internal_cn = cr.cr_cnum;
  154. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %lu and %lu\n",
  155. retrocl_first_cn,
  156. retrocl_internal_cn);
  157. slapi_ch_free(( void **) &cr.cr_time );
  158. return 0;
  159. }
  160. /*
  161. * Function: retrocl_getchangetime
  162. * Arguments: type - one of SLAPI_SEQ_FIRST, SLAPI_SEQ_LAST
  163. * Returns: The time of the requested change record. If the return value is
  164. * NO_TIME, the changelog could not be read.
  165. * If err is non-NULL, the memory it points to is set the the
  166. * error code returned from the backend. If "type" is not valid,
  167. * *err is set to -1.
  168. * Description: Get the first or last changenumber stored in the changelog,
  169. * depending on the value of argument "type".
  170. */
  171. time_t retrocl_getchangetime( int type, int *err )
  172. {
  173. cnumRet cr;
  174. time_t ret;
  175. if ( type != SLAPI_SEQ_FIRST && type != SLAPI_SEQ_LAST ) {
  176. if ( err != NULL ) {
  177. *err = -1;
  178. }
  179. return NO_TIME;
  180. }
  181. memset( &cr, '\0', sizeof( cnumRet ));
  182. slapi_seq_callback( RETROCL_CHANGELOG_DN, type,
  183. (char *)attr_changenumber, /* cast away const */
  184. NULL,
  185. NULL, 0, &cr, NULL,
  186. handle_cnum_result, handle_cnum_entry, NULL );
  187. if ( err != NULL ) {
  188. *err = cr.cr_lderr;
  189. }
  190. if ( NULL == cr.cr_time ) {
  191. ret = NO_TIME;
  192. } else {
  193. ret = parse_localTime( cr.cr_time );
  194. }
  195. slapi_ch_free(( void **) &cr.cr_time );
  196. return ret;
  197. }
  198. /*
  199. * Function: retrocl_forget_changenumbers
  200. *
  201. * Returns: none
  202. *
  203. * Arguments: none
  204. *
  205. * Description: used only when the server is shutting down
  206. *
  207. */
  208. void retrocl_forget_changenumbers(void)
  209. {
  210. PR_Lock(retrocl_internal_lock);
  211. retrocl_first_cn = 0;
  212. retrocl_internal_cn = 0;
  213. PR_Unlock(retrocl_internal_lock);
  214. }
  215. /*
  216. * Function: retrocl_get_first_changenumber
  217. *
  218. * Returns: changeNumber
  219. *
  220. * Arguments: none
  221. *
  222. * Description: used in root DSE
  223. *
  224. */
  225. changeNumber retrocl_get_first_changenumber(void)
  226. {
  227. changeNumber cn;
  228. PR_Lock(retrocl_internal_lock);
  229. cn = retrocl_first_cn;
  230. PR_Unlock(retrocl_internal_lock);
  231. return cn;
  232. }
  233. /*
  234. * Function: retrocl_set_first_changenumber
  235. *
  236. * Returns: none
  237. *
  238. * Arguments: changenumber
  239. *
  240. * Description: used in changelog trimming
  241. *
  242. */
  243. void retrocl_set_first_changenumber(changeNumber cn)
  244. {
  245. PR_Lock(retrocl_internal_lock);
  246. retrocl_first_cn = cn;
  247. PR_Unlock(retrocl_internal_lock);
  248. }
  249. /*
  250. * Function: retrocl_get_last_changenumber
  251. *
  252. * Returns:
  253. *
  254. * Arguments:
  255. *
  256. * Description: used in root DSE
  257. *
  258. */
  259. changeNumber retrocl_get_last_changenumber(void)
  260. {
  261. changeNumber cn;
  262. PR_Lock(retrocl_internal_lock);
  263. cn = retrocl_internal_cn;
  264. PR_Unlock(retrocl_internal_lock);
  265. return cn;
  266. }
  267. /*
  268. * Function: retrocl_commit_changenumber
  269. *
  270. * Returns: none
  271. *
  272. * Arguments: none, lock must be held
  273. *
  274. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  275. *
  276. */
  277. void retrocl_commit_changenumber(void)
  278. {
  279. if ( retrocl_first_cn == 0) {
  280. retrocl_first_cn = retrocl_internal_cn;
  281. }
  282. }
  283. /*
  284. * Function: retrocl_release_changenumber
  285. *
  286. * Returns: none
  287. *
  288. * Arguments: none, lock must be held
  289. *
  290. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  291. *
  292. */
  293. void retrocl_release_changenumber(void)
  294. {
  295. retrocl_internal_cn--;
  296. }
  297. /*
  298. * Function: retrocl_update_lastchangenumber
  299. *
  300. * Returns: 0/-1
  301. *
  302. * Arguments: none
  303. *
  304. * Description: reads the last entry in the changelog to obtain
  305. * the last change number.
  306. *
  307. */
  308. int retrocl_update_lastchangenumber(void)
  309. {
  310. cnumRet cr;
  311. if (retrocl_be_changelog == NULL) return -1;
  312. cr.cr_cnum = 0;
  313. cr.cr_time = 0;
  314. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  315. (char *)attr_changenumber, /* cast away const */
  316. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  317. handle_cnum_entry, NULL);
  318. retrocl_internal_cn = cr.cr_cnum;
  319. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %lu \n",
  320. retrocl_internal_cn);
  321. slapi_ch_free(( void **) &cr.cr_time );
  322. return 0;
  323. }
  324. /*
  325. * Function: retrocl_assign_changenumber
  326. *
  327. * Returns: change number, 0 on error
  328. *
  329. * Arguments: none. Lock must be held.
  330. *
  331. * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
  332. * retrocl_release_changenumber
  333. *
  334. */
  335. changeNumber retrocl_assign_changenumber(void)
  336. {
  337. changeNumber cn;
  338. /* Before we assign the changenumber; we should check for the
  339. * validity of the internal assignment of retrocl_internal_cn
  340. * we had from the startup */
  341. if(retrocl_internal_cn <= retrocl_first_cn){
  342. /* the numbers have become out of sync - retrocl_get_changenumbers
  343. * gets called only once during startup and it may have had a problem
  344. * getting the last changenumber.
  345. * If there was any problem then update the lastchangenumber from the changelog db.
  346. * This function is being called by only the thread that is actually writing
  347. * to the changelog.
  348. */
  349. retrocl_update_lastchangenumber();
  350. }
  351. retrocl_internal_cn++;
  352. cn = retrocl_internal_cn;
  353. return cn;
  354. }