sync_persist.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /** BEGIN COPYRIGHT BLOCK
  2. ent sync_srch_refresh_pre_op(Slapi_PBlock *pb);
  3. * This Program is free software; you can redistribute it and/or modify it under
  4. * the terms of the GNU General Public License as published by the Free Software
  5. * Foundation; version 2 of the License.
  6. *
  7. * This Program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with
  12. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  13. * Place, Suite 330, Boston, MA 02111-1307 USA.
  14. *
  15. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  16. * right to link the code of this Program with code not covered under the GNU
  17. * General Public License ("Non-GPL Code") and to distribute linked combinations
  18. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  19. * permitted under this exception must only link to the code of this Program
  20. * through those well defined interfaces identified in the file named EXCEPTION
  21. * found in the source code files (the "Approved Interfaces"). The files of
  22. * Non-GPL Code may instantiate templates or use macros or inline functions from
  23. * the Approved Interfaces without causing the resulting work to be covered by
  24. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  25. * additions to the list of Approved Interfaces. You must obey the GNU General
  26. * Public License in all respects for all of the Program code and other code used
  27. * in conjunction with the Program except the Non-GPL Code covered by this
  28. * exception. If you modify this file, you may extend this exception to your
  29. * version of the file, but you are not obligated to do so. If you do not wish to
  30. * provide this exception without modification, you must delete this exception
  31. * statement from your version and license this file solely under the GPL without
  32. * exception.
  33. *
  34. *
  35. * Copyright (C) 2013 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include "sync.h"
  39. /* Main list of established persistent synchronizaton searches */
  40. static SyncRequestList *sync_request_list = NULL;
  41. /*
  42. * Convenience macros for locking the list of persistent searches
  43. */
  44. #define SYNC_LOCK_READ() slapi_rwlock_rdlock(sync_request_list->sync_req_rwlock)
  45. #define SYNC_UNLOCK_READ() slapi_rwlock_unlock(sync_request_list->sync_req_rwlock)
  46. #define SYNC_LOCK_WRITE() slapi_rwlock_wrlock(sync_request_list->sync_req_rwlock)
  47. #define SYNC_UNLOCK_WRITE() slapi_rwlock_unlock(sync_request_list->sync_req_rwlock)
  48. /*
  49. * Convenience macro for checking if the Content Synchronization subsystem has
  50. * been initialized.
  51. */
  52. #define SYNC_IS_INITIALIZED() (sync_request_list != NULL)
  53. static int sync_add_request( SyncRequest *req );
  54. static void sync_remove_request( SyncRequest *req );
  55. static SyncRequest *sync_request_alloc();
  56. void sync_queue_change( Slapi_Entry *e, Slapi_Entry *eprev, ber_int_t chgtype );
  57. static void sync_send_results( void *arg );
  58. static void sync_request_wakeup_all();
  59. static void sync_node_free( SyncQueueNode **node );
  60. static int sync_acquire_connection (Slapi_Connection *conn);
  61. static int sync_release_connection (Slapi_PBlock *pb, Slapi_Connection *conn, Slapi_Operation *op, int release);
  62. int sync_add_persist_post_op(Slapi_PBlock *pb)
  63. {
  64. Slapi_Entry *e;
  65. if ( !SYNC_IS_INITIALIZED()) {
  66. return(0);
  67. }
  68. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  69. sync_queue_change(e, NULL, LDAP_REQ_ADD);
  70. return( 0 );
  71. }
  72. int sync_del_persist_post_op(Slapi_PBlock *pb)
  73. {
  74. Slapi_Entry *e;
  75. if ( !SYNC_IS_INITIALIZED()) {
  76. return(0);
  77. }
  78. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e);
  79. sync_queue_change(e, NULL, LDAP_REQ_DELETE);
  80. return( 0 );
  81. }
  82. int sync_mod_persist_post_op(Slapi_PBlock *pb)
  83. {
  84. Slapi_Entry *e;
  85. if ( !SYNC_IS_INITIALIZED()) {
  86. return(0);
  87. }
  88. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  89. sync_queue_change(e, NULL, LDAP_REQ_MODIFY);
  90. return( 0 );
  91. }
  92. int sync_modrdn_persist_post_op(Slapi_PBlock *pb)
  93. {
  94. Slapi_Entry *e, *e_prev;
  95. if ( !SYNC_IS_INITIALIZED()) {
  96. return(0);
  97. }
  98. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  99. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e_prev);
  100. sync_queue_change(e, e_prev, LDAP_REQ_MODRDN);
  101. return( 0 );
  102. }
  103. void
  104. sync_queue_change( Slapi_Entry *e, Slapi_Entry *eprev, ber_int_t chgtype )
  105. {
  106. SyncRequest *req = NULL;
  107. SyncQueueNode *node = NULL;
  108. int matched = 0;
  109. int prev_match = 0;
  110. int cur_match = 0;
  111. if ( !SYNC_IS_INITIALIZED()) {
  112. return;
  113. }
  114. if ( NULL == e ) {
  115. /* For now, some backends such as the chaining backend do not provide a post-op entry */
  116. return;
  117. }
  118. SYNC_LOCK_READ();
  119. for ( req = sync_request_list->sync_req_head; NULL != req; req = req->req_next ) {
  120. Slapi_DN *base = NULL;
  121. int scope;
  122. Slapi_Operation *op;
  123. /* Skip the nodes that have no more active operation
  124. */
  125. slapi_pblock_get( req->req_pblock, SLAPI_OPERATION, &op );
  126. if ( op == NULL || slapi_op_abandoned( req->req_pblock ) ) {
  127. continue;
  128. }
  129. slapi_pblock_get( req->req_pblock, SLAPI_SEARCH_TARGET_SDN, &base );
  130. slapi_pblock_get( req->req_pblock, SLAPI_SEARCH_SCOPE, &scope );
  131. if (NULL == base) {
  132. base = slapi_sdn_new_dn_byref(req->req_orig_base);
  133. slapi_pblock_set(req->req_pblock, SLAPI_SEARCH_TARGET_SDN, base);
  134. }
  135. /*
  136. * See if the entry meets the scope and filter criteria.
  137. * We cannot do the acl check here as this thread
  138. * would then potentially clash with the ps_send_results()
  139. * thread on the aclpb in ps->req_pblock.
  140. * By avoiding the acl check in this thread, and leaving all the acl
  141. * checking to the ps_send_results() thread we avoid
  142. * the req_pblock contention problem.
  143. * The lesson here is "Do not give multiple threads arbitary access
  144. * to the same pblock" this kind of muti-threaded access
  145. * to the same pblock must be done carefully--there is currently no
  146. * generic satisfactory way to do this.
  147. */
  148. /* if the change is a modrdn then we need to check if the entry was
  149. * moved into scope, out of scope, or stays in scope
  150. */
  151. if (chgtype == LDAP_REQ_MODRDN)
  152. prev_match = slapi_sdn_scope_test( slapi_entry_get_sdn_const(eprev), base, scope ) &&
  153. ( 0 == slapi_vattr_filter_test( req->req_pblock, eprev, req->req_filter, 0 /* verify_access */ ));
  154. cur_match = slapi_sdn_scope_test( slapi_entry_get_sdn_const(e), base, scope ) &&
  155. ( 0 == slapi_vattr_filter_test( req->req_pblock, e, req->req_filter, 0 /* verify_access */ ));
  156. if (prev_match || cur_match) {
  157. SyncQueueNode *pOldtail;
  158. /* The scope and the filter match - enqueue it */
  159. matched++;
  160. node = (SyncQueueNode *)slapi_ch_calloc( 1, sizeof( SyncQueueNode ));
  161. node->sync_entry = slapi_entry_dup( e );
  162. if ( chgtype == LDAP_REQ_MODRDN) {
  163. if (prev_match && cur_match)
  164. node->sync_chgtype = LDAP_REQ_MODIFY;
  165. else if (prev_match)
  166. node->sync_chgtype = LDAP_REQ_DELETE;
  167. else
  168. node->sync_chgtype = LDAP_REQ_ADD;
  169. } else {
  170. node->sync_chgtype = chgtype;
  171. }
  172. /* Put it on the end of the list for this sync search */
  173. PR_Lock( req->req_lock );
  174. pOldtail = req->ps_eq_tail;
  175. req->ps_eq_tail = node;
  176. if ( NULL == req->ps_eq_head ) {
  177. req->ps_eq_head = req->ps_eq_tail;
  178. }
  179. else {
  180. pOldtail->sync_next = req->ps_eq_tail;
  181. }
  182. PR_Unlock( req->req_lock );
  183. }
  184. }
  185. SYNC_UNLOCK_READ();
  186. /* Were there any matches? */
  187. if ( matched ) {
  188. /* Notify update threads */
  189. sync_request_wakeup_all();
  190. slapi_log_error (SLAPI_LOG_TRACE, SYNC_PLUGIN_SUBSYSTEM, "sync search: enqueued entry "
  191. "\"%s\" on %d request listeners\n", slapi_entry_get_dn_const(e), matched );
  192. } else {
  193. slapi_log_error (SLAPI_LOG_TRACE, SYNC_PLUGIN_SUBSYSTEM, "sync search: entry "
  194. "\"%s\" not enqueued on any request search listeners\n", slapi_entry_get_dn_const(e) );
  195. }
  196. }
  197. /*
  198. * Initialize the list structure which contains the list
  199. * of established content sync persistent requests
  200. */
  201. int
  202. sync_persist_initialize (int argc, char **argv)
  203. {
  204. if ( !SYNC_IS_INITIALIZED()) {
  205. sync_request_list = (SyncRequestList *) slapi_ch_calloc( 1, sizeof( SyncRequestList ));
  206. if (( sync_request_list->sync_req_rwlock = slapi_new_rwlock()) == NULL ) {
  207. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_persist_initialize: cannot initialize lock structure(1). ");
  208. return( -1 );
  209. }
  210. if (( sync_request_list->sync_req_cvarlock = PR_NewLock()) == NULL ) {
  211. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_persist_initialize: cannot initialize lock structure(2). ");
  212. return( -1 );
  213. }
  214. if (( sync_request_list->sync_req_cvar = PR_NewCondVar( sync_request_list->sync_req_cvarlock )) == NULL ) {
  215. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_persist_initialize: cannot initialize condition variable. ");
  216. return( -1 );
  217. }
  218. sync_request_list->sync_req_head = NULL;
  219. sync_request_list->sync_req_cur_persist = 0;
  220. sync_request_list->sync_req_max_persist = SYNC_MAX_CONCURRENT;
  221. if (argc > 0) {
  222. /* for now the only plugin arg is the max concurrent
  223. * persistent sync searches
  224. */
  225. sync_request_list->sync_req_max_persist = sync_number2int(argv[0]);
  226. if (sync_request_list->sync_req_max_persist == -1) {
  227. sync_request_list->sync_req_max_persist = SYNC_MAX_CONCURRENT;
  228. }
  229. }
  230. }
  231. return (0);
  232. }
  233. /*
  234. * Add the given pblock to the list of established sync searches.
  235. * Then, start a thread to send the results to the client as they
  236. * are dispatched by add, modify, and modrdn operations.
  237. */
  238. PRThread *
  239. sync_persist_add (Slapi_PBlock *pb)
  240. {
  241. SyncRequest *req = NULL;
  242. char *base;
  243. Slapi_Filter *filter;
  244. if ( SYNC_IS_INITIALIZED() && NULL != pb ) {
  245. /* Create the new node */
  246. req = sync_request_alloc();
  247. req->req_pblock = sync_pblock_copy(pb);
  248. slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET_DN, &base);
  249. req->req_orig_base = slapi_ch_strdup(base);
  250. slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &filter );
  251. req->req_filter = slapi_filter_dup(filter);
  252. /* Add it to the head of the list of persistent searches */
  253. if ( 0 == sync_add_request( req )) {
  254. /* Start a thread to send the results */
  255. req->req_tid = PR_CreateThread( PR_USER_THREAD, sync_send_results,
  256. (void *) req, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  257. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE );
  258. /* Checking if the thread is succesfully created and
  259. * if the thread is not created succesfully.... we send
  260. * error messages to the Log file
  261. */
  262. if(NULL == (req->req_tid)){
  263. int prerr;
  264. prerr = PR_GetError();
  265. slapi_log_error(SLAPI_LOG_FATAL, "Content Synchronization Search",
  266. "sync_persist_add function: failed to create persitent thread, error %d (%s)\n",
  267. prerr, slapi_pr_strerror(prerr));
  268. /* Now remove the ps from the list so call the function ps_remove */
  269. sync_remove_request(req);
  270. PR_DestroyLock ( req->req_lock );
  271. req->req_lock = NULL;
  272. slapi_ch_free((void **) &req->req_pblock );
  273. slapi_ch_free((void **) &req );
  274. } else {
  275. return( req->req_tid);
  276. }
  277. }
  278. }
  279. return( NULL);
  280. }
  281. int
  282. sync_persist_startup (PRThread *tid, Sync_Cookie *cookie)
  283. {
  284. SyncRequest *cur;
  285. int rc = 1;
  286. if ( SYNC_IS_INITIALIZED() && NULL != tid ) {
  287. SYNC_LOCK_READ();
  288. /* Find and change */
  289. cur = sync_request_list->sync_req_head;
  290. while ( NULL != cur ) {
  291. if ( cur->req_tid == tid ) {
  292. cur->req_active = PR_TRUE;
  293. cur->req_cookie = cookie;
  294. rc = 0;
  295. break;
  296. }
  297. cur = cur->req_next;
  298. }
  299. SYNC_UNLOCK_READ();
  300. }
  301. return (rc);
  302. }
  303. int
  304. sync_persist_terminate (PRThread *tid)
  305. {
  306. SyncRequest *cur;
  307. int rc = 1;
  308. if ( SYNC_IS_INITIALIZED() && NULL != tid ) {
  309. SYNC_LOCK_READ();
  310. /* Find and change */
  311. cur = sync_request_list->sync_req_head;
  312. while ( NULL != cur ) {
  313. if ( cur->req_tid == tid ) {
  314. cur->req_active = PR_TRUE;
  315. rc = 0;
  316. break;
  317. }
  318. cur = cur->req_next;
  319. }
  320. SYNC_UNLOCK_READ();
  321. }
  322. if (rc == 0) {
  323. sync_remove_request(cur);
  324. }
  325. return(rc);
  326. }
  327. int
  328. sync_persist_terminate_all ()
  329. {
  330. SyncRequest *cur;
  331. if ( SYNC_IS_INITIALIZED() ) {
  332. SYNC_LOCK_READ();
  333. cur = sync_request_list->sync_req_head;
  334. while ( NULL != cur ) {
  335. cur->req_complete = PR_TRUE;
  336. cur = cur->req_next;
  337. }
  338. SYNC_UNLOCK_READ();
  339. }
  340. return (0);
  341. }
  342. /*
  343. * Allocate and initialize an empty Sync node.
  344. */
  345. static SyncRequest *
  346. sync_request_alloc()
  347. {
  348. SyncRequest *req;
  349. req = (SyncRequest *) slapi_ch_calloc( 1, sizeof( SyncRequest ));
  350. req->req_pblock = NULL;
  351. if (( req->req_lock = PR_NewLock()) == NULL ) {
  352. slapi_log_error (SLAPI_LOG_FATAL, SYNC_PLUGIN_SUBSYSTEM, "sync_request_alloc: cannot initialize lock structure. ");
  353. slapi_ch_free((void **)&req);
  354. return( NULL );
  355. }
  356. req->req_tid = (PRThread *) NULL;
  357. req->req_complete = 0;
  358. req->req_cookie = NULL;
  359. req->ps_eq_head = req->ps_eq_tail = (SyncQueueNode *) NULL;
  360. req->req_next = NULL;
  361. req->req_active = PR_FALSE;
  362. return req;
  363. }
  364. /*
  365. * Add the given persistent search to the
  366. * head of the list of persistent searches.
  367. */
  368. static int
  369. sync_add_request( SyncRequest *req )
  370. {
  371. int rc = 0;
  372. if ( SYNC_IS_INITIALIZED() && NULL != req ) {
  373. SYNC_LOCK_WRITE();
  374. if (sync_request_list->sync_req_cur_persist < sync_request_list->sync_req_max_persist) {
  375. sync_request_list->sync_req_cur_persist++;
  376. req->req_next = sync_request_list->sync_req_head;
  377. sync_request_list->sync_req_head = req;
  378. } else {
  379. rc = 1;
  380. }
  381. SYNC_UNLOCK_WRITE();
  382. }
  383. return(rc);
  384. }
  385. static void
  386. sync_remove_request( SyncRequest *req )
  387. {
  388. SyncRequest *cur;
  389. int removed = 0;
  390. if ( SYNC_IS_INITIALIZED() && NULL != req ) {
  391. SYNC_LOCK_WRITE();
  392. if ( NULL == sync_request_list->sync_req_head ) {
  393. /* should not happen, attempt to remove a request never added */
  394. } else if ( req == sync_request_list->sync_req_head ) {
  395. /* Remove from head */
  396. sync_request_list->sync_req_head = sync_request_list->sync_req_head->req_next;
  397. removed = 1;
  398. } else {
  399. /* Find and remove from list */
  400. cur = sync_request_list->sync_req_head;
  401. while ( NULL != cur->req_next ) {
  402. if ( cur->req_next == req ) {
  403. cur->req_next = cur->req_next->req_next;
  404. removed = 1;
  405. break;
  406. } else {
  407. cur = cur->req_next;
  408. }
  409. }
  410. }
  411. if (removed) {
  412. sync_request_list->sync_req_cur_persist--;
  413. }
  414. SYNC_UNLOCK_WRITE();
  415. if (!removed) {
  416. slapi_log_error (SLAPI_LOG_PLUGIN, SYNC_PLUGIN_SUBSYSTEM, "attempt to remove nonexistent req");
  417. }
  418. }
  419. }
  420. static void
  421. sync_request_wakeup_all()
  422. {
  423. if ( SYNC_IS_INITIALIZED()) {
  424. PR_Lock( sync_request_list->sync_req_cvarlock );
  425. PR_NotifyAllCondVar( sync_request_list->sync_req_cvar );
  426. PR_Unlock( sync_request_list->sync_req_cvarlock );
  427. }
  428. }
  429. static int
  430. sync_acquire_connection (Slapi_Connection *conn)
  431. {
  432. int rc;
  433. /* need to acquire a reference to this connection so that it will not
  434. be released or cleaned up out from under us
  435. in psearch.c it is implemented as:
  436. PR_Lock( ps->req_pblock->pb_conn->c_mutex );
  437. conn_acq_flag = connection_acquire_nolock(ps->req_pblock->pb_conn);
  438. PR_Unlock( ps->req_pblock->pb_conn->c_mutex );
  439. HOW TO DO FROM A PLUGIN
  440. - either expose the functions from the connection code in the private api
  441. and allow to link them in
  442. - or fake a connection structure
  443. struct fake_conn {
  444. void *needed1
  445. void *needed2
  446. void *pad1
  447. void *pad2
  448. void *needed3;
  449. }
  450. struct fake_conn *c = (struct fake_conn *) conn;
  451. c->needed3 ++;
  452. this would require knowledge or analysis of the connection structure,
  453. could probably be done for servers with a common history
  454. */
  455. /* use exposed slapi_connection functions */
  456. rc = slapi_connection_acquire(conn);
  457. return (rc);
  458. }
  459. static int
  460. sync_release_connection (Slapi_PBlock *pb, Slapi_Connection *conn, Slapi_Operation *op, int release)
  461. {
  462. /* see comments in sync_acquire_connection */
  463. /* using exposed connection handling functions */
  464. slapi_connection_remove_operation(pb, conn, op, release);
  465. return(0);
  466. }
  467. /*
  468. * Thread routine for sending search results to a client
  469. * which is persistently waiting for them.
  470. *
  471. * This routine will terminate when either (a) the ps_complete
  472. * flag is set, or (b) the associated operation is abandoned.
  473. * In any case, the thread won't notice until it wakes from
  474. * sleeping on the ps_list condition variable, so it needs
  475. * to be awakened.
  476. */
  477. static void
  478. sync_send_results( void *arg )
  479. {
  480. SyncRequest *req = (SyncRequest *)arg;
  481. SyncQueueNode *qnode, *qnodenext;
  482. int conn_acq_flag = 0;
  483. Slapi_Connection *conn = NULL;
  484. Slapi_Operation *op = NULL;
  485. int rc;
  486. PRUint64 connid;
  487. int opid;
  488. slapi_pblock_get(req->req_pblock, SLAPI_CONN_ID, &connid);
  489. slapi_pblock_get(req->req_pblock, SLAPI_OPERATION_ID, &opid);
  490. slapi_pblock_get(req->req_pblock, SLAPI_CONNECTION, &conn);
  491. slapi_pblock_get(req->req_pblock, SLAPI_OPERATION, &op);
  492. conn_acq_flag = sync_acquire_connection (conn);
  493. if (conn_acq_flag) {
  494. slapi_log_error(SLAPI_LOG_FATAL, "Content Synchronization Search",
  495. "conn=%" NSPRIu64 " op=%d Could not acquire the connection - aborted\n",
  496. (long long unsigned int)connid, opid);
  497. }
  498. PR_Lock( sync_request_list->sync_req_cvarlock );
  499. while ( (conn_acq_flag == 0) && !req->req_complete ) {
  500. /* Check for an abandoned operation */
  501. Slapi_Operation *op;
  502. slapi_pblock_get(req->req_pblock, SLAPI_OPERATION, &op);
  503. if ( op == NULL || slapi_op_abandoned( req->req_pblock ) ) {
  504. slapi_log_error(SLAPI_LOG_PLUGIN, "Content Synchronization Search",
  505. "conn=%" NSPRIu64 " op=%d Operation no longer active - terminating\n",
  506. (long long unsigned int)connid, opid);
  507. break;
  508. }
  509. if ( NULL == req->ps_eq_head || !req->req_active) {
  510. /* Nothing to do yet, or the refresh phase is not yet completed */
  511. /* If an operation is abandoned, we do not get notified by the
  512. * connection code. Wake up every second to check if thread
  513. * should terminate.
  514. */
  515. PR_WaitCondVar( sync_request_list->sync_req_cvar, PR_SecondsToInterval(1) );
  516. } else {
  517. /* dequeue the item */
  518. int attrsonly;
  519. char **attrs;
  520. char **noattrs = NULL;
  521. LDAPControl **ectrls = NULL;
  522. Slapi_Entry *ec;
  523. int chg_type = LDAP_SYNC_NONE;
  524. /* deque one element */
  525. PR_Lock( req->req_lock );
  526. qnode = req->ps_eq_head;
  527. req->ps_eq_head = qnode->sync_next;
  528. if ( NULL == req->ps_eq_head ) {
  529. req->ps_eq_tail = NULL;
  530. }
  531. PR_Unlock( req->req_lock );
  532. /* Get all the information we need to send the result */
  533. ec = qnode->sync_entry;
  534. slapi_pblock_get( req->req_pblock, SLAPI_SEARCH_ATTRS, &attrs );
  535. slapi_pblock_get( req->req_pblock, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
  536. /*
  537. * Send the result. Since send_ldap_search_entry can block for
  538. * up to 30 minutes, we relinquish all locks before calling it.
  539. */
  540. PR_Unlock(sync_request_list->sync_req_cvarlock);
  541. /*
  542. * The entry is in the right scope and matches the filter
  543. * but we need to redo the filter test here to check access
  544. * controls. See the comments at the slapi_filter_test()
  545. * call in sync_persist_add().
  546. */
  547. if ( slapi_vattr_filter_test( req->req_pblock, ec, req->req_filter,
  548. 1 /* verify_access */ ) == 0 ) {
  549. slapi_pblock_set( req->req_pblock, SLAPI_SEARCH_RESULT_ENTRY, ec );
  550. /* NEED TO BUILD THE CONTROL */
  551. switch (qnode->sync_chgtype){
  552. case LDAP_REQ_ADD:
  553. chg_type = LDAP_SYNC_ADD;
  554. break;
  555. case LDAP_REQ_MODIFY:
  556. chg_type = LDAP_SYNC_MODIFY;
  557. break;
  558. case LDAP_REQ_MODRDN:
  559. chg_type = LDAP_SYNC_MODIFY;
  560. break;
  561. case LDAP_REQ_DELETE:
  562. chg_type = LDAP_SYNC_DELETE;
  563. noattrs = (char **)slapi_ch_calloc(2, sizeof (char *));
  564. noattrs[0] = slapi_ch_strdup("1.1");
  565. noattrs[1] = NULL;
  566. break;
  567. }
  568. ectrls = (LDAPControl **)slapi_ch_calloc(2, sizeof (LDAPControl *));
  569. if (req->req_cookie)
  570. sync_cookie_update(req->req_cookie, ec);
  571. sync_create_state_control(ec, &ectrls[0], chg_type, req->req_cookie);
  572. rc = slapi_send_ldap_search_entry( req->req_pblock,
  573. ec, ectrls,
  574. noattrs?noattrs:attrs, attrsonly );
  575. if (rc) {
  576. slapi_log_error(SLAPI_LOG_CONNS, SYNC_PLUGIN_SUBSYSTEM,
  577. "Error %d sending entry %s\n",
  578. rc, slapi_entry_get_dn_const(ec));
  579. }
  580. ldap_controls_free(ectrls);
  581. slapi_ch_array_free(noattrs);
  582. }
  583. PR_Lock(sync_request_list->sync_req_cvarlock);
  584. /* Deallocate our wrapper for this entry */
  585. sync_node_free( &qnode );
  586. }
  587. }
  588. PR_Unlock( sync_request_list->sync_req_cvarlock );
  589. sync_remove_request( req );
  590. /* indicate the end of search */
  591. sync_release_connection(req->req_pblock, conn, op, conn_acq_flag == 0);
  592. PR_DestroyLock ( req->req_lock );
  593. req->req_lock = NULL;
  594. slapi_ch_free((void **) &req->req_pblock );
  595. slapi_ch_free((void **) &req->req_orig_base );
  596. slapi_filter_free(req->req_filter, 1);
  597. sync_cookie_free(&req->req_cookie);
  598. for ( qnode = req->ps_eq_head; qnode; qnode = qnodenext) {
  599. qnodenext = qnode->sync_next;
  600. sync_node_free( &qnode );
  601. }
  602. slapi_ch_free((void **) &req );
  603. }
  604. /*
  605. * Free a sync update node (and everything it holds).
  606. */
  607. static void
  608. sync_node_free( SyncQueueNode **node )
  609. {
  610. if ( node != NULL && *node != NULL ) {
  611. if ( (*node)->sync_entry != NULL ) {
  612. slapi_entry_free( (*node)->sync_entry );
  613. (*node)->sync_entry = NULL;
  614. }
  615. slapi_ch_free( (void **)node );
  616. }
  617. }