sampletask.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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) 2006 Red Hat, Inc.
  34. * All rights reserved.
  35. * END COPYRIGHT BLOCK **/
  36. /*
  37. * sample task plugin
  38. *
  39. * [How to set up the plugin for testing]
  40. * 1. compile and package with the other plugins
  41. * 2. put the plugin libsampletask-plugin.so at <prefix>/usr/lib/<PACKAGE_NAME>/plugins
  42. * 3. register it as a plugin in dse.ldif
  43. * Plugin entry:
  44. * dn: cn=sampletask,cn=plugins,cn=config
  45. * objectClass: top
  46. * objectClass: nsSlapdPlugin
  47. * objectClass: extensibleObject
  48. * cn: sampletask
  49. * nsslapd-pluginPath: libsampletask-plugin
  50. * nsslapd-pluginInitfunc: sampletask_init
  51. * nsslapd-pluginType: object
  52. * nsslapd-pluginEnabled: on
  53. * nsslapd-pluginId: sampletask
  54. * nsslapd-pluginVersion: <plugin_version>
  55. * nsslapd-pluginVendor: <vendor name>
  56. * nsslapd-pluginDescription: Sample task plugin
  57. *
  58. * 4. create a config task entry in dse.ldif
  59. * Task entry:
  60. * dn: cn=sampletask, cn=tasks, cn=config
  61. * objectClass: top
  62. * objectClass: extensibleObject
  63. * cn: sampletask
  64. *
  65. * 5. to invoke the sample task, run the command line:
  66. * $ ./ldapmodify -h <host> -p <port> -D "cn=Directory Manager" -w <pw> -a
  67. * dn: cn=sampletask 0, cn=sample task, cn=tasks, cn=config
  68. * objectClass: top
  69. * objectClass: extensibleObject
  70. * cn: sample task 0
  71. * myarg: sample task myarg
  72. *
  73. * Result is in the errors log
  74. * [...] - Sample task starts (arg: sample task myarg) ...
  75. * [...] - Sample task finished.
  76. */
  77. #include "slapi-plugin.h"
  78. #include "nspr.h"
  79. static int task_sampletask_add(Slapi_PBlock *pb, Slapi_Entry *e,
  80. Slapi_Entry *eAfter, int *returncode, char *returntext,
  81. void *arg);
  82. static int task_sampletask_start(Slapi_PBlock *pb);
  83. /*
  84. * Init function
  85. * Specified in the plugin entry as "nsslapd-pluginInitfunc: sampletask_init"
  86. */
  87. int
  88. sampletask_init( Slapi_PBlock *pb )
  89. {
  90. int rc = 0;
  91. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  92. (void *) SLAPI_PLUGIN_VERSION_03 );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  94. (void *) task_sampletask_start );
  95. return rc;
  96. }
  97. /*
  98. * Task start function
  99. * Register the function task_sampletask_add, which invokes the task on demand.
  100. */
  101. static int
  102. task_sampletask_start(Slapi_PBlock *pb)
  103. {
  104. int rc = slapi_task_register_handler("sample task", task_sampletask_add);
  105. return rc;
  106. }
  107. /*
  108. * Task thread
  109. * Not necessary be a thread, but it'd not disturb the server's other jobs.
  110. */
  111. static void
  112. task_sampletask_thread(void *arg)
  113. {
  114. Slapi_Task *task = (Slapi_Task *)arg;
  115. char *myarg = NULL;
  116. int i, rv = 0;
  117. int total_work = 3;
  118. /* fetch our argument from the task */
  119. myarg = (char *)slapi_task_get_data(task);
  120. /* update task state to show it's running */
  121. slapi_task_begin(task, total_work);
  122. slapi_task_log_notice(task, "Sample task starts (arg: %s) ...\n", myarg);
  123. slapi_log_error(SLAPI_LOG_FATAL, "sampletask", "Sample task starts (arg: %s) ...\n", myarg);
  124. /* real work would be done here */
  125. for (i = 0; i < total_work; i++) {
  126. PR_Sleep(10000);
  127. slapi_task_inc_progress(task);
  128. }
  129. /* update task state to say we're finished */
  130. slapi_task_log_notice(task, "Sample task finished.");
  131. slapi_task_log_status(task, "Sample task finished.");
  132. slapi_log_error(SLAPI_LOG_FATAL, "sampletask", "Sample task finished.\n");
  133. /* this will queue the destruction of the task */
  134. slapi_task_finish(task, rv);
  135. }
  136. /* extract a single value from the entry (as a string) -- if it's not in the
  137. * entry, the default will be returned (which can be NULL).
  138. * you do not need to free anything returned by this.
  139. */
  140. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  141. const char *default_val)
  142. {
  143. Slapi_Attr *attr;
  144. Slapi_Value *val = NULL;
  145. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  146. return default_val;
  147. slapi_attr_first_value(attr, &val);
  148. return slapi_value_get_string(val);
  149. }
  150. static void
  151. task_sampletask_destructor(Slapi_Task *task)
  152. {
  153. if (task) {
  154. char *myarg = (char *)slapi_task_get_data(task);
  155. if (myarg) {
  156. slapi_ch_free_string(&myarg);
  157. }
  158. }
  159. }
  160. /*
  161. * Invoked when the task instance is added by the client (step 5 of the comment)
  162. * Get the necessary attributes from the task entry, and spawns a thread to do
  163. * the task.
  164. */
  165. static int
  166. task_sampletask_add(Slapi_PBlock *pb, Slapi_Entry *e,
  167. Slapi_Entry *eAfter, int *returncode, char *returntext,
  168. void *arg)
  169. {
  170. PRThread *thread = NULL;
  171. const char *cn;
  172. int rv = SLAPI_DSE_CALLBACK_OK;
  173. Slapi_PBlock *mypb = NULL;
  174. Slapi_Task *task = NULL;
  175. const char *myarg;
  176. *returncode = LDAP_SUCCESS;
  177. if ((cn = fetch_attr(e, "cn", NULL)) == NULL) {
  178. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  179. rv = SLAPI_DSE_CALLBACK_ERROR;
  180. goto out;
  181. }
  182. /* get arg(s) */
  183. if ((myarg = fetch_attr(e, "myarg", NULL)) == NULL) {
  184. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  185. rv = SLAPI_DSE_CALLBACK_ERROR;
  186. goto out;
  187. }
  188. /* allocate new task now */
  189. task = slapi_new_task(slapi_entry_get_ndn(e));
  190. if (task == NULL) {
  191. slapi_log_error(SLAPI_LOG_FATAL, "sampletask", "unable to allocate new task!\n");
  192. *returncode = LDAP_OPERATIONS_ERROR;
  193. rv = SLAPI_DSE_CALLBACK_ERROR;
  194. goto out;
  195. }
  196. /* set a destructor that will clean up myarg for us when the task is complete */
  197. slapi_task_set_destructor_fn(task, task_sampletask_destructor);
  198. /* Stash our argument in the task for use by the task thread */
  199. slapi_task_set_data(task, slapi_ch_strdup(myarg));
  200. /* start the sample task as a separate thread */
  201. thread = PR_CreateThread(PR_USER_THREAD, task_sampletask_thread,
  202. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  203. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  204. if (thread == NULL) {
  205. slapi_log_error(SLAPI_LOG_FATAL, "sampletask",
  206. "unable to create sample task thread!\n");
  207. *returncode = LDAP_OPERATIONS_ERROR;
  208. rv = SLAPI_DSE_CALLBACK_ERROR;
  209. slapi_task_finish(task, *returncode);
  210. } else {
  211. /* thread successful */
  212. rv = SLAPI_DSE_CALLBACK_OK;
  213. }
  214. out:
  215. return rv;
  216. }