schema_reload.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2008 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. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. /*
  12. * Dynamic schema file reload plugin
  13. *
  14. * plugin entry in dse.ldif
  15. * dn: cn=Schema Reload,cn=plugins,cn=config
  16. * objectClass: top
  17. * objectClass: nsSlapdPlugin
  18. * objectClass: extensibleObject
  19. * cn: Schema Reload
  20. * nsslapd-pluginPath: libschemareload-plugin
  21. * nsslapd-pluginInitfunc: schemareload_init
  22. * nsslapd-pluginType: object
  23. * nsslapd-pluginEnabled: on
  24. * nsslapd-pluginId: schemareload
  25. * nsslapd-pluginVersion: <plugin_version>
  26. * nsslapd-pluginVendor: <vendor name>
  27. * nsslapd-pluginDescription: task plugin to reload schema files
  28. *
  29. * config task entry in dse.ldif (registered in schemareload_start)
  30. * dn: cn=schema reload task, cn=tasks, cn=config
  31. * objectClass: top
  32. * objectClass: extensibleObject
  33. * cn: schema reload task
  34. *
  35. * To invoke the sample task, run the command line:
  36. * $ ldapmodify -h <host> -p <port> -D "cn=Directory Manager" -w <pw> -a
  37. * dn: cn=schema reload task 0, cn=schema reload task, cn=tasks, cn=config
  38. * objectClass: top
  39. * objectClass: extensibleObject
  40. * cn: schema reload task 0
  41. * [ schemadir: path to reload files from ]
  42. */
  43. #include "slap.h"
  44. #include "slapi-plugin.h"
  45. #include "nspr.h"
  46. static PRLock *schemareload_lock = NULL;
  47. static Slapi_PluginDesc pdesc = {"schemareload",
  48. VENDOR, DS_PACKAGE_VERSION,
  49. "task plugin to reload schema files"};
  50. static int schemareload_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  51. static int schemareload_start(Slapi_PBlock *pb);
  52. static int schemareload_close(Slapi_PBlock *pb);
  53. static void schemareload_destructor(Slapi_Task *task);
  54. /*
  55. * Init function
  56. * Specified in the plugin entry as "nsslapd-pluginInitfunc: schemareload_init"
  57. */
  58. int
  59. schemareload_init(Slapi_PBlock *pb)
  60. {
  61. int rc = 0;
  62. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  63. (void *)SLAPI_PLUGIN_VERSION_03);
  64. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  65. (void *)&pdesc);
  66. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  67. (void *)schemareload_start);
  68. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, (void *)schemareload_close);
  69. return rc;
  70. }
  71. /*
  72. * Task start function
  73. * Register the function schemareload_add, which invokes the task on demand.
  74. */
  75. static int
  76. schemareload_start(Slapi_PBlock *pb)
  77. {
  78. int rc = 0;
  79. if ((schemareload_lock = PR_NewLock()) == NULL) {
  80. slapi_log_err(SLAPI_LOG_ERR, "schemareload", "schemareload_start - Failed to create global schema reload lock.");
  81. return -1;
  82. }
  83. rc = slapi_plugin_task_register_handler("schema reload task", schemareload_add, pb);
  84. if (rc != LDAP_SUCCESS) {
  85. PR_DestroyLock(schemareload_lock);
  86. }
  87. return rc;
  88. }
  89. static int
  90. schemareload_close(Slapi_PBlock *pb __attribute__((unused)))
  91. {
  92. slapi_plugin_task_unregister_handler("schema reload task", schemareload_add);
  93. PR_DestroyLock(schemareload_lock);
  94. return 0;
  95. }
  96. typedef struct _task_data
  97. {
  98. char *schemadir;
  99. char *bind_dn;
  100. } task_data;
  101. /*
  102. * Task thread
  103. * This is the heart of the reload-schema-file task:
  104. * - calling the schema APIs to validate the schema files,
  105. * - reloading them if the files are valid.
  106. * Not necessary be a thread, but it'd not disturb the server's other jobs.
  107. */
  108. static void
  109. schemareload_thread(void *arg)
  110. {
  111. Slapi_Task *task = (Slapi_Task *)arg;
  112. int rv = 0;
  113. int total_work = 2;
  114. task_data *td = NULL;
  115. if (!task) {
  116. return; /* no task */
  117. }
  118. slapi_task_inc_refcount(task);
  119. slapi_log_err(SLAPI_LOG_PLUGIN, "schemareload",
  120. "schemareload_thread --> refcount incremented.\n");
  121. /* Fetch our task data from the task */
  122. td = (task_data *)slapi_task_get_data(task);
  123. /* Initialize and set the bind dn in the thread data */
  124. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  125. /* update task state to show it's running */
  126. slapi_task_begin(task, total_work);
  127. PR_Lock(schemareload_lock); /* make schema reload serialized */
  128. slapi_task_log_notice(task, "Schema reload task starts (schema dir: %s) ...\n", td->schemadir ? td->schemadir : "default");
  129. slapi_log_err(SLAPI_LOG_INFO, "schemareload", "schemareload_thread - Schema reload task starts (schema dir: %s) ...\n", td->schemadir ? td->schemadir : "default");
  130. rv = slapi_validate_schema_files(td->schemadir);
  131. slapi_task_inc_progress(task);
  132. if (slapi_is_shutting_down()) {
  133. slapi_task_log_notice(task, "Server is shuttoing down; Schema validation aborted.");
  134. slapi_task_log_status(task, "Server is shuttoing down; Schema validation aborted.");
  135. slapi_log_err(SLAPI_LOG_ERR, "schemareload", "schemareload_thread - Server is shutting down; Schema validation aborted.");
  136. } else if (LDAP_SUCCESS == rv) {
  137. slapi_task_log_notice(task, "Schema validation passed.");
  138. slapi_task_log_status(task, "Schema validation passed.");
  139. slapi_log_err(SLAPI_LOG_INFO, "schemareload", "schemareload_thread - Schema validation passed.\n");
  140. rv = slapi_reload_schema_files(td->schemadir);
  141. slapi_task_inc_progress(task);
  142. /* update task state to say we're finished */
  143. if (LDAP_SUCCESS == rv) {
  144. slapi_task_log_notice(task, "Schema reload task finished.");
  145. slapi_task_log_status(task, "Schema reload task finished.");
  146. slapi_log_err(SLAPI_LOG_INFO, "schemareload", "schemareload_thread - Schema reload task finished.\n");
  147. } else {
  148. slapi_task_log_notice(task, "Schema reload task failed.");
  149. slapi_task_log_status(task, "Schema reload task failed.");
  150. slapi_log_err(SLAPI_LOG_ERR, "schemareload", "schemareload_thread - Schema reload task failed.\n");
  151. }
  152. } else {
  153. slapi_task_log_notice(task, "Schema validation failed.");
  154. slapi_task_log_status(task, "Schema validation failed.");
  155. slapi_log_err(SLAPI_LOG_ERR, "schemareload", "schemareload_thread - Schema validation failed.\n");
  156. }
  157. PR_Unlock(schemareload_lock);
  158. /* this will queue the destruction of the task */
  159. slapi_task_finish(task, rv);
  160. slapi_task_dec_refcount(task);
  161. slapi_log_err(SLAPI_LOG_PLUGIN, "schemareload",
  162. "schemareload_thread <-- refcount decremented.\n");
  163. }
  164. static void
  165. schemareload_destructor(Slapi_Task *task)
  166. {
  167. if (task) {
  168. task_data *mydata = (task_data *)slapi_task_get_data(task);
  169. while (slapi_task_get_refcount(task) > 0) {
  170. /* Yield to wait for the fixup task finishes. */
  171. DS_Sleep(PR_MillisecondsToInterval(100));
  172. }
  173. if (mydata) {
  174. slapi_ch_free_string(&mydata->schemadir);
  175. slapi_ch_free_string(&mydata->bind_dn);
  176. /* Need to cast to avoid a compiler warning */
  177. slapi_ch_free((void **)&mydata);
  178. }
  179. }
  180. }
  181. /*
  182. * Invoked when the task instance is added by the client (step 5 of the comment)
  183. * Get the necessary attributes from the task entry, and spawns a thread to do
  184. * the task.
  185. */
  186. static int
  187. schemareload_add(Slapi_PBlock *pb,
  188. Slapi_Entry *e,
  189. Slapi_Entry *eAfter __attribute__((unused)),
  190. int *returncode,
  191. char *returntext __attribute__((unused)),
  192. void *arg)
  193. {
  194. PRThread *thread = NULL;
  195. const char *schemadir = NULL;
  196. int rv = SLAPI_DSE_CALLBACK_OK;
  197. Slapi_Task *task = NULL;
  198. task_data *mytaskdata = NULL;
  199. char *bind_dn;
  200. *returncode = LDAP_SUCCESS;
  201. if (fetch_attr(e, "cn", NULL) == NULL) {
  202. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  203. rv = SLAPI_DSE_CALLBACK_ERROR;
  204. goto out;
  205. }
  206. /* get the requestor dn for our thread data*/
  207. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  208. /* get arg(s) */
  209. schemadir = fetch_attr(e, "schemadir", NULL);
  210. /* allocate new task now */
  211. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  212. if (task == NULL) {
  213. slapi_log_err(SLAPI_LOG_ERR, "schemareload", "schemareload_add - Unable to allocate new task!\n");
  214. *returncode = LDAP_OPERATIONS_ERROR;
  215. rv = SLAPI_DSE_CALLBACK_ERROR;
  216. goto out;
  217. }
  218. mytaskdata = (task_data *)slapi_ch_malloc(sizeof(task_data));
  219. if (mytaskdata == NULL) {
  220. *returncode = LDAP_OPERATIONS_ERROR;
  221. rv = SLAPI_DSE_CALLBACK_ERROR;
  222. goto out;
  223. }
  224. mytaskdata->schemadir = slapi_ch_strdup(schemadir);
  225. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  226. /* set a destructor that will clean up schemadir for us when the task is complete */
  227. slapi_task_set_destructor_fn(task, schemareload_destructor);
  228. /* Stash our task_data for use by the task thread */
  229. slapi_task_set_data(task, mytaskdata);
  230. /* start the schema reload task as a separate thread */
  231. thread = PR_CreateThread(PR_USER_THREAD, schemareload_thread,
  232. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  233. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  234. if (thread == NULL) {
  235. slapi_log_err(SLAPI_LOG_ERR, "schemareload",
  236. "schemareload_add - Unable to create schema reload task thread!\n");
  237. *returncode = LDAP_OPERATIONS_ERROR;
  238. rv = SLAPI_DSE_CALLBACK_ERROR;
  239. } else {
  240. /* thread successful */
  241. rv = SLAPI_DSE_CALLBACK_OK;
  242. }
  243. out:
  244. return rv;
  245. }