test_repl_session_plugin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #include "slapi-plugin.h"
  9. #include "repl-session-plugin.h"
  10. #include <string.h>
  11. #define REPL_SESSION_v1_0_GUID "210D7559-566B-41C6-9B03-5523BDF30880"
  12. static char *test_repl_session_plugin_name = "test_repl_session_api";
  13. /*
  14. * Plugin identifiers
  15. */
  16. static Slapi_PluginDesc test_repl_session_pdesc = {
  17. "test-repl-session-plugin",
  18. "Test Vendor",
  19. "1.0",
  20. "test replication session plugin"
  21. };
  22. static Slapi_ComponentId *test_repl_session_plugin_id = NULL;
  23. /*
  24. * Replication Session Callbacks
  25. */
  26. /*
  27. * This is called on a master when a replication agreement is
  28. * initialized at startup. A cookie can be allocated at this
  29. * time which is passed to other callbacks on the master side.
  30. */
  31. static void *
  32. test_repl_session_plugin_agmt_init_cb(const Slapi_DN *repl_subtree)
  33. {
  34. char *cookie = NULL;
  35. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  36. "test_repl_session_plugin_init_cb() called for suffix \"%s\".\n",
  37. slapi_sdn_get_ndn(repl_subtree));
  38. /* allocate a string and set as the cookie */
  39. cookie = slapi_ch_smprintf("cookie test");
  40. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  41. "test_repl_session_plugin_init_cb(): Setting cookie: \"%s\".\n",
  42. cookie);
  43. return cookie;
  44. }
  45. /*
  46. * This is called on a master when we are about to acquire a
  47. * replica. This callback can allocate some extra data to
  48. * be sent to the replica in the start replication request.
  49. * This memory will be free'd by the replication plug-in
  50. * after it is sent. A guid string must be set that is to
  51. * be used by the receiving side to ensure that the data is
  52. * from the same replication session plug-in.
  53. *
  54. * Returning non-0 will abort the replication session. This
  55. * results in the master going into incremental backoff mode.
  56. */
  57. static int
  58. test_repl_session_plugin_pre_acquire_cb(void *cookie, const Slapi_DN *repl_subtree,
  59. int is_total, char **data_guid, struct berval **data)
  60. {
  61. int rc = 0;
  62. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  63. "test_repl_session_plugin_pre_acquire_cb() called for suffix \"%s\", "
  64. "is_total: \"%s\", cookie: \"%s\".\n", slapi_sdn_get_ndn(repl_subtree),
  65. is_total ? "TRUE" : "FALSE", cookie ? (char *)cookie : "NULL");
  66. /* allocate some data to be sent to the replica */
  67. *data_guid = slapi_ch_smprintf("test-guid");
  68. *data = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  69. (*data)->bv_val = slapi_ch_smprintf("test-data");
  70. (*data)->bv_len = strlen((*data)->bv_val) + 1;
  71. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  72. "test_repl_session_plugin_pre_acquire_cb() sending data: guid: \"%s\" data: \"%s\".\n",
  73. *data_guid, (*data)->bv_val);
  74. return rc;
  75. }
  76. /*
  77. * This is called on a replica when we are about to reply to
  78. * a start replication request from a master. This callback
  79. * can allocate some extra data to be sent to the master in
  80. * the start replication response. This memory will be free'd
  81. * by the replication plug-in after it is sent. A guid string
  82. * must be set that is to be used by the receiving side to ensure
  83. * that the data is from the same replication session plug-in.
  84. *
  85. * Returning non-0 will abort the replication session. This
  86. * results in the master going into incremental backoff mode.
  87. */
  88. static int
  89. test_repl_session_plugin_reply_acquire_cb(const char *repl_subtree, int is_total,
  90. char **data_guid, struct berval **data)
  91. {
  92. int rc = 0;
  93. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  94. "test_repl_session_plugin_reply_acquire_cb() called for suffix \"%s\", is_total: \"%s\".\n",
  95. repl_subtree, is_total ? "TRUE" : "FALSE");
  96. /* allocate some data to be sent to the master */
  97. *data_guid = slapi_ch_smprintf("test-reply-guid");
  98. *data = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  99. (*data)->bv_val = slapi_ch_smprintf("test-reply-data");
  100. (*data)->bv_len = strlen((*data)->bv_val) + 1;
  101. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  102. "test_repl_session_plugin_reply_acquire_cb() sending data: guid: \"%s\" data: \"%s\".\n",
  103. *data_guid, (*data)->bv_val);
  104. return rc;
  105. }
  106. /*
  107. * This is called on a master when it receives a reply to a
  108. * start replication extop that we sent to a replica. Any
  109. * extra data sent by a replication session callback on the
  110. * replica will be set here as the data parameter. The data_guid
  111. * should be checked first to ensure that the sending side is
  112. * using the same replication session plug-in before making any
  113. * assumptions about the contents of the data parameter. You
  114. * should not free data_guid or data. The replication plug-in
  115. * will take care of freeing this memory.
  116. *
  117. * Returning non-0 will abort the replication session. This
  118. * results in the master going into incremental backoff mode.
  119. */
  120. static int
  121. test_repl_session_plugin_post_acquire_cb(void *cookie, const Slapi_DN *repl_subtree, int is_total,
  122. const char *data_guid, const struct berval *data)
  123. {
  124. int rc = 0;
  125. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  126. "test_repl_session_plugin_post_acquire_cb() called for suffix \"%s\", "
  127. "is_total: \"%s\" cookie: \"%s\".\n", slapi_sdn_get_ndn(repl_subtree),
  128. is_total ? "TRUE" : "FALSE", cookie ? (char *)cookie : "NULL");
  129. /* log any extra data that was sent from the replica */
  130. if (data_guid && data) {
  131. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  132. "test_repl_session_plugin_post_acquire_cb() received data: guid: \"%s\" data: \"%s\".\n",
  133. data_guid, data->bv_val);
  134. }
  135. return rc;
  136. }
  137. /*
  138. * This is called on a replica when it receives a start replication
  139. * extended operation from a master. If the replication session
  140. * plug-in on the master sent any extra data, it will be set here
  141. * as the data parameter. The data_guid should be checked first to
  142. * ensure that the sending side is using the same replication session
  143. * plug-in before making any assumptions about the contents of the
  144. * data parameter. You should not free data_guid or data. The
  145. * replication plug-in will take care of freeing this memory.
  146. *
  147. * Returning non-0 will abort the replication session. This
  148. * results in the master going into incremental backoff mode.
  149. */
  150. static int
  151. test_repl_session_plugin_recv_acquire_cb(const char *repl_subtree, int is_total,
  152. const char *data_guid, const struct berval *data)
  153. {
  154. int rc = 0;
  155. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  156. "test_repl_session_plugin_recv_acquire_cb() called for suffix \"%s\", is_total: \"%s\".\n",
  157. repl_subtree, is_total ? "TRUE" : "FALSE");
  158. /* log any extra data that was sent from the master */
  159. if (data_guid && data) {
  160. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  161. "test_repl_session_plugin_recv_acquire_cb() received data: guid: \"%s\" data: \"%s\".\n",
  162. data_guid, data->bv_val);
  163. }
  164. return rc;
  165. }
  166. /*
  167. * This is called on a master when a replication agreement is
  168. * destroyed. Any cookie allocated when the agreement was initialized
  169. * should be free'd here.
  170. */
  171. static void
  172. test_repl_session_plugin_destroy_cb(void *cookie, const Slapi_DN *repl_subtree)
  173. {
  174. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  175. "test_repl_session_plugin_destroy_cb() called for suffix \"%s\".\n",
  176. slapi_sdn_get_ndn(repl_subtree));
  177. /* free cookie */
  178. slapi_ch_free_string((char **)&cookie);
  179. return;
  180. }
  181. /*
  182. * Callback list for registering API
  183. */
  184. static void *test_repl_session_api[] = {
  185. NULL, /* reserved for api broker use, must be zero */
  186. test_repl_session_plugin_agmt_init_cb,
  187. test_repl_session_plugin_pre_acquire_cb,
  188. test_repl_session_plugin_reply_acquire_cb,
  189. test_repl_session_plugin_post_acquire_cb,
  190. test_repl_session_plugin_recv_acquire_cb,
  191. test_repl_session_plugin_destroy_cb
  192. };
  193. /*
  194. * Plug-in framework functions
  195. */
  196. static int
  197. test_repl_session_plugin_start(Slapi_PBlock *pb)
  198. {
  199. slapi_log_error(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  200. "--> test_repl_session_plugin_start -- begin\n");
  201. slapi_log_error(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  202. "<-- test_repl_session_plugin_start -- end\n");
  203. return 0;
  204. }
  205. static int
  206. test_repl_session_plugin_close(Slapi_PBlock *pb)
  207. {
  208. slapi_log_error(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  209. "--> test_repl_session_plugin_close -- begin\n");
  210. slapi_apib_unregister(REPL_SESSION_v1_0_GUID);
  211. slapi_log_error(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  212. "<-- test_repl_session_plugin_close -- end\n");
  213. return 0;
  214. }
  215. int test_repl_session_plugin_init(Slapi_PBlock *pb)
  216. {
  217. slapi_log_error(SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  218. "--> test_repl_session_plugin_init -- begin\n");
  219. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  220. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  221. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  222. (void *) test_repl_session_plugin_start ) != 0 ||
  223. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  224. (void *) test_repl_session_plugin_close ) != 0 ||
  225. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  226. (void *)&test_repl_session_pdesc ) != 0 )
  227. {
  228. slapi_log_error( SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  229. "<-- test_repl_session_plugin_init -- failed to register plugin -- end\n");
  230. return -1;
  231. }
  232. if( slapi_apib_register(REPL_SESSION_v1_0_GUID, test_repl_session_api) ) {
  233. slapi_log_error( SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  234. "<-- test_repl_session_plugin_start -- failed to register repl_session api -- end\n");
  235. return -1;
  236. }
  237. /* Retrieve and save the plugin identity to later pass to
  238. internal operations */
  239. if (slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &test_repl_session_plugin_id) != 0) {
  240. slapi_log_error(SLAPI_LOG_FATAL, test_repl_session_plugin_name,
  241. "<-- test_repl_session_plugin_init -- failed to retrieve plugin identity -- end\n");
  242. return -1;
  243. }
  244. slapi_log_error( SLAPI_LOG_PLUGIN, test_repl_session_plugin_name,
  245. "<-- test_repl_session_plugin_init -- end\n");
  246. return 0;
  247. }
  248. /*
  249. dn: cn=Test Replication Session API,cn=plugins,cn=config
  250. objectclass: top
  251. objectclass: nsSlapdPlugin
  252. objectclass: extensibleObject
  253. cn: Test Replication Session API
  254. nsslapd-pluginpath: libtestreplsession-plugin
  255. nsslapd-plugininitfunc: test_repl_session_plugin_init
  256. nsslapd-plugintype: preoperation
  257. nsslapd-pluginenabled: on
  258. nsslapd-plugin-depends-on-type: database
  259. nsslapd-plugin-depends-on-named: Multimaster Replication Plugin
  260. */