schema_reload.c 12 KB

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