schema_reload.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include "dirver.h" /* PRODUCTTEXT */
  75. #include <dirlite_strings.h> /* PLUGIN_MAGIC_VENDOR_STR */
  76. static PRLock *schemareload_lock = NULL;
  77. static Slapi_PluginDesc pdesc = { "schemareload",
  78. PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  79. "task plugin to reload schema files" };
  80. static int schemareload_add(Slapi_PBlock *pb, Slapi_Entry *e,
  81. Slapi_Entry *eAfter, int *returncode, char *returntext,
  82. void *arg);
  83. static int schemareload_start(Slapi_PBlock *pb);
  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. schemareload_lock = PR_NewLock();
  93. if (NULL == schemareload_lock) {
  94. slapi_log_error(SLAPI_LOG_FATAL, "schemareload",
  95. "Failed to create global schema reload lock.");
  96. return rc;
  97. }
  98. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  99. (void *) SLAPI_PLUGIN_VERSION_03 );
  100. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  101. (void *)&pdesc );
  102. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  103. (void *) schemareload_start );
  104. return rc;
  105. }
  106. /*
  107. * Task start function
  108. * Register the function schemareload_add, which invokes the task on demand.
  109. */
  110. static int
  111. schemareload_start(Slapi_PBlock *pb)
  112. {
  113. int rc = slapi_task_register_handler("schema reload task", schemareload_add);
  114. return rc;
  115. }
  116. /*
  117. * Task thread
  118. * This is the heart of the reload-schema-file task:
  119. * - calling the schema APIs to validate the schema files,
  120. * - reloading them if the files are valid.
  121. * Not necessary be a thread, but it'd not disturb the server's other jobs.
  122. */
  123. static void
  124. schemareload_thread(void *arg)
  125. {
  126. Slapi_Task *task = (Slapi_Task *)arg;
  127. int rv = 0;
  128. int total_work = 2;
  129. /* fetch our argument from the task */
  130. char *schemadir = (char *)slapi_task_get_data(task);
  131. /* update task state to show it's running */
  132. slapi_task_begin(task, total_work);
  133. PR_Lock(schemareload_lock); /* make schema reload serialized */
  134. slapi_task_log_notice(task, "Schema reload task starts (schema dir: %s) ...\n", schemadir?schemadir:"default");
  135. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema reload task starts (schema dir: %s) ...\n", schemadir?schemadir:"default");
  136. rv = slapi_validate_schema_files(schemadir);
  137. slapi_task_inc_progress(task);
  138. 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(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. PR_Unlock(schemareload_lock);
  155. } else {
  156. slapi_task_log_notice(task, "Schema validation failed.");
  157. slapi_task_log_status(task, "Schema validation failed.");
  158. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "Schema validation failed.\n");
  159. PR_Unlock(schemareload_lock);
  160. }
  161. /* this will queue the destruction of the task */
  162. slapi_task_finish(task, rv);
  163. }
  164. /* extract a single value from the entry (as a string) -- if it's not in the
  165. * entry, the default will be returned (which can be NULL).
  166. * you do not need to free anything returned by this.
  167. */
  168. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  169. const char *default_val)
  170. {
  171. Slapi_Attr *attr;
  172. Slapi_Value *val = NULL;
  173. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  174. return default_val;
  175. slapi_attr_first_value(attr, &val);
  176. return slapi_value_get_string(val);
  177. }
  178. static void
  179. schemareload_destructor(Slapi_Task *task)
  180. {
  181. if (task) {
  182. char *schemadir = (char *)slapi_task_get_data(task);
  183. slapi_ch_free_string(&schemadir);
  184. }
  185. }
  186. /*
  187. * Invoked when the task instance is added by the client (step 5 of the comment)
  188. * Get the necessary attributes from the task entry, and spawns a thread to do
  189. * the task.
  190. */
  191. static int
  192. schemareload_add(Slapi_PBlock *pb, Slapi_Entry *e,
  193. Slapi_Entry *eAfter, int *returncode, char *returntext,
  194. void *arg)
  195. {
  196. PRThread *thread = NULL;
  197. const char *cn;
  198. const char *schemadir = NULL;
  199. int rv = SLAPI_DSE_CALLBACK_OK;
  200. Slapi_Task *task = NULL;
  201. *returncode = LDAP_SUCCESS;
  202. if ((cn = fetch_attr(e, "cn", NULL)) == NULL) {
  203. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  204. rv = SLAPI_DSE_CALLBACK_ERROR;
  205. goto out;
  206. }
  207. /* get arg(s) */
  208. schemadir = fetch_attr(e, "schemadir", NULL);
  209. /* allocate new task now */
  210. task = slapi_new_task(slapi_entry_get_ndn(e));
  211. if (task == NULL) {
  212. slapi_log_error(SLAPI_LOG_FATAL, "schemareload", "unable to allocate new task!\n");
  213. *returncode = LDAP_OPERATIONS_ERROR;
  214. rv = SLAPI_DSE_CALLBACK_ERROR;
  215. goto out;
  216. }
  217. /* set a destructor that will clean up schemadir for us when the task is complete */
  218. slapi_task_set_destructor_fn(task, schemareload_destructor);
  219. /* Stash our argument in the task for use by the task thread */
  220. slapi_task_set_data(task, slapi_ch_strdup(schemadir));
  221. /* start the schema reload task as a separate thread */
  222. thread = PR_CreateThread(PR_USER_THREAD, schemareload_thread,
  223. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  224. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  225. if (thread == NULL) {
  226. slapi_log_error(SLAPI_LOG_FATAL, "schemareload",
  227. "unable to create schema reload task thread!\n");
  228. *returncode = LDAP_OPERATIONS_ERROR;
  229. rv = SLAPI_DSE_CALLBACK_ERROR;
  230. slapi_task_finish(task, *returncode);
  231. } else {
  232. /* thread successful */
  233. rv = SLAPI_DSE_CALLBACK_OK;
  234. }
  235. out:
  236. return rv;
  237. }