schema_reload.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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,
  51. Slapi_Entry *eAfter, int *returncode, char *returntext,
  52. void *arg);
  53. static int schemareload_start(Slapi_PBlock *pb);
  54. static int schemareload_close(Slapi_PBlock *pb);
  55. static void schemareload_destructor(Slapi_Task *task);
  56. /*
  57. * Init function
  58. * Specified in the plugin entry as "nsslapd-pluginInitfunc: schemareload_init"
  59. */
  60. int
  61. schemareload_init( Slapi_PBlock *pb )
  62. {
  63. int rc = 0;
  64. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  65. (void *) SLAPI_PLUGIN_VERSION_03 );
  66. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  67. (void *)&pdesc );
  68. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  69. (void *) schemareload_start );
  70. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN, (void *) schemareload_close );
  71. return rc;
  72. }
  73. /*
  74. * Task start function
  75. * Register the function schemareload_add, which invokes the task on demand.
  76. */
  77. static int
  78. schemareload_start(Slapi_PBlock *pb)
  79. {
  80. int rc = 0;
  81. if ((schemareload_lock = PR_NewLock()) == NULL) {
  82. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Failed to create global schema reload lock.");
  83. return -1;
  84. }
  85. rc = slapi_plugin_task_register_handler("schema reload task", schemareload_add, pb);
  86. if(rc != LDAP_SUCCESS){
  87. PR_DestroyLock(schemareload_lock);
  88. }
  89. return rc;
  90. }
  91. static int
  92. schemareload_close(Slapi_PBlock *pb)
  93. {
  94. slapi_plugin_task_unregister_handler("schema reload task", schemareload_add);
  95. PR_DestroyLock(schemareload_lock);
  96. return 0;
  97. }
  98. typedef struct _task_data
  99. {
  100. char *schemadir;
  101. char *bind_dn;
  102. } task_data;
  103. /*
  104. * Task thread
  105. * This is the heart of the reload-schema-file task:
  106. * - calling the schema APIs to validate the schema files,
  107. * - reloading them if the files are valid.
  108. * Not necessary be a thread, but it'd not disturb the server's other jobs.
  109. */
  110. static void
  111. schemareload_thread(void *arg)
  112. {
  113. Slapi_Task *task = (Slapi_Task *)arg;
  114. int rv = 0;
  115. int total_work = 2;
  116. task_data *td = NULL;
  117. if (!task) {
  118. return; /* no task */
  119. }
  120. slapi_task_inc_refcount(task);
  121. slapi_log_error(SLAPI_LOG_PLUGIN, "schemareload",
  122. "schemareload_thread --> refcount incremented.\n" );
  123. /* Fetch our task data from the task */
  124. td = (task_data *)slapi_task_get_data(task);
  125. /* Initialize and set the bind dn in the thread data */
  126. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  127. /* update task state to show it's running */
  128. slapi_task_begin(task, total_work);
  129. PR_Lock(schemareload_lock); /* make schema reload serialized */
  130. slapi_task_log_notice(task, "Schema reload task starts (schema dir: %s) ...\n", td->schemadir?td->schemadir:"default");
  131. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema reload task starts (schema dir: %s) ...\n", td->schemadir?td->schemadir:"default");
  132. rv = slapi_validate_schema_files(td->schemadir);
  133. slapi_task_inc_progress(task);
  134. if (slapi_is_shutting_down()) {
  135. slapi_task_log_notice(task, "Server is shuttoing down; Schema validation aborted.");
  136. slapi_task_log_status(task, "Server is shuttoing down; Schema validation aborted.");
  137. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Server is shuttoing down; Schema validation aborted.");
  138. } else if (LDAP_SUCCESS == rv) {
  139. slapi_task_log_notice(task, "Schema validation passed.");
  140. slapi_task_log_status(task, "Schema validation passed.");
  141. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema validation passed.\n");
  142. rv = slapi_reload_schema_files(td->schemadir);
  143. slapi_task_inc_progress(task);
  144. /* update task state to say we're finished */
  145. if (LDAP_SUCCESS == rv) {
  146. slapi_task_log_notice(task, "Schema reload task finished.");
  147. slapi_task_log_status(task, "Schema reload task finished.");
  148. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema reload task finished.\n");
  149. } else {
  150. slapi_task_log_notice(task, "Schema reload task failed.");
  151. slapi_task_log_status(task, "Schema reload task failed.");
  152. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema reload task failed.\n");
  153. }
  154. } else {
  155. slapi_task_log_notice(task, "Schema validation failed.");
  156. slapi_task_log_status(task, "Schema validation failed.");
  157. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema validation failed.\n");
  158. }
  159. PR_Unlock(schemareload_lock);
  160. /* this will queue the destruction of the task */
  161. slapi_task_finish(task, rv);
  162. slapi_task_dec_refcount(task);
  163. slapi_log_error(SLAPI_LOG_PLUGIN, "schemareload",
  164. "schemareload_thread <-- refcount decremented.\n");
  165. }
  166. /* extract a single value from the entry (as a string) -- if it's not in the
  167. * entry, the default will be returned (which can be NULL).
  168. * you do not need to free anything returned by this.
  169. */
  170. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  171. const char *default_val)
  172. {
  173. Slapi_Attr *attr;
  174. Slapi_Value *val = NULL;
  175. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  176. return default_val;
  177. slapi_attr_first_value(attr, &val);
  178. return slapi_value_get_string(val);
  179. }
  180. static void
  181. schemareload_destructor(Slapi_Task *task)
  182. {
  183. if (task) {
  184. task_data *mydata = (task_data *)slapi_task_get_data(task);
  185. while (slapi_task_get_refcount(task) > 0) {
  186. /* Yield to wait for the fixup task finishes. */
  187. DS_Sleep (PR_MillisecondsToInterval(100));
  188. }
  189. if (mydata) {
  190. slapi_ch_free_string(&mydata->schemadir);
  191. slapi_ch_free_string(&mydata->bind_dn);
  192. /* Need to cast to avoid a compiler warning */
  193. slapi_ch_free((void **)&mydata);
  194. }
  195. }
  196. }
  197. /*
  198. * Invoked when the task instance is added by the client (step 5 of the comment)
  199. * Get the necessary attributes from the task entry, and spawns a thread to do
  200. * the task.
  201. */
  202. static int
  203. schemareload_add(Slapi_PBlock *pb, Slapi_Entry *e,
  204. Slapi_Entry *eAfter, int *returncode, char *returntext,
  205. void *arg)
  206. {
  207. PRThread *thread = NULL;
  208. const char *schemadir = NULL;
  209. int rv = SLAPI_DSE_CALLBACK_OK;
  210. Slapi_Task *task = NULL;
  211. task_data *mytaskdata = NULL;
  212. char *bind_dn;
  213. *returncode = LDAP_SUCCESS;
  214. if (fetch_attr(e, "cn", NULL) == NULL) {
  215. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  216. rv = SLAPI_DSE_CALLBACK_ERROR;
  217. goto out;
  218. }
  219. /* get the requestor dn for our thread data*/
  220. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  221. /* get arg(s) */
  222. schemadir = fetch_attr(e, "schemadir", NULL);
  223. /* allocate new task now */
  224. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  225. if (task == NULL) {
  226. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "unable to allocate new task!\n");
  227. *returncode = LDAP_OPERATIONS_ERROR;
  228. rv = SLAPI_DSE_CALLBACK_ERROR;
  229. goto out;
  230. }
  231. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  232. if (mytaskdata == NULL)
  233. {
  234. *returncode = LDAP_OPERATIONS_ERROR;
  235. rv = SLAPI_DSE_CALLBACK_ERROR;
  236. goto out;
  237. }
  238. mytaskdata->schemadir = slapi_ch_strdup(schemadir);
  239. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  240. /* set a destructor that will clean up schemadir for us when the task is complete */
  241. slapi_task_set_destructor_fn(task, schemareload_destructor);
  242. /* Stash our task_data for use by the task thread */
  243. slapi_task_set_data(task, mytaskdata);
  244. /* start the schema reload task as a separate thread */
  245. thread = PR_CreateThread(PR_USER_THREAD, schemareload_thread,
  246. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  247. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  248. if (thread == NULL) {
  249. slapi_log_error(SLAPI_LOG_FATAL, "schemareload",
  250. "unable to create schema reload task thread!\n");
  251. *returncode = LDAP_OPERATIONS_ERROR;
  252. rv = SLAPI_DSE_CALLBACK_ERROR;
  253. } else {
  254. /* thread successful */
  255. rv = SLAPI_DSE_CALLBACK_OK;
  256. }
  257. out:
  258. return rv;
  259. }