task.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * directory online tasks (import, export, backup, restore)
  43. */
  44. #include "slap.h"
  45. /***********************************
  46. * Static Global Variables
  47. ***********************************/
  48. /* don't panic, this is only used when creating new tasks or removing old
  49. * ones...
  50. */
  51. static Slapi_Task *global_task_list = NULL;
  52. static PRLock *global_task_lock = NULL;
  53. static int shutting_down = 0;
  54. /***********************************
  55. * Private Defines
  56. ***********************************/
  57. #define TASK_BASE_DN "cn=tasks,cn=config"
  58. #define TASK_IMPORT_DN "cn=import,cn=tasks,cn=config"
  59. #define TASK_EXPORT_DN "cn=export,cn=tasks,cn=config"
  60. #define TASK_BACKUP_DN "cn=backup,cn=tasks,cn=config"
  61. #define TASK_RESTORE_DN "cn=restore,cn=tasks,cn=config"
  62. #define TASK_INDEX_DN "cn=index,cn=tasks,cn=config"
  63. #define TASK_UPGRADEDB_DN "cn=upgradedb,cn=tasks,cn=config"
  64. #define TASK_LOG_NAME "nsTaskLog"
  65. #define TASK_STATUS_NAME "nsTaskStatus"
  66. #define TASK_EXITCODE_NAME "nsTaskExitCode"
  67. #define TASK_PROGRESS_NAME "nsTaskCurrentItem"
  68. #define TASK_WORK_NAME "nsTaskTotalItems"
  69. #define DEFAULT_TTL "120" /* seconds */
  70. #define TASK_SYSCONFIG_FILE_ATTR "sysconfigfile" /* sysconfig reload task file attr */
  71. #define TASK_SYSCONFIG_LOGCHANGES_ATTR "logchanges"
  72. #define LOG_BUFFER 256
  73. /* if the cumul. log gets larger than this, it's truncated: */
  74. #define MAX_SCROLLBACK_BUFFER 8192
  75. #define NEXTMOD(_type, _val) do { \
  76. modlist[cur].mod_op = LDAP_MOD_REPLACE; \
  77. modlist[cur].mod_type = (_type); \
  78. modlist[cur].mod_values = (char **)slapi_ch_malloc(2*sizeof(char *)); \
  79. modlist[cur].mod_values[0] = (_val); \
  80. modlist[cur].mod_values[1] = NULL; \
  81. mod[cur] = &modlist[cur]; \
  82. cur++; \
  83. } while (0)
  84. /***********************************
  85. * Static Function Prototypes
  86. ***********************************/
  87. static Slapi_Task *new_task(const char *dn, void *plugin);
  88. static void destroy_task(time_t when, void *arg);
  89. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  90. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  91. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  92. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  93. static void task_generic_destructor(Slapi_Task *task);
  94. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  95. const char *default_val);
  96. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn);
  97. static void modify_internal_entry(char *dn, LDAPMod **mods);
  98. /***********************************
  99. * Public Functions
  100. ***********************************/
  101. /*
  102. * slapi_new_task: create a new task, fill in DN, and setup modify callback
  103. * argument:
  104. * dn: task dn
  105. * result:
  106. * Success: Slapi_Task object
  107. * Failure: NULL
  108. */
  109. Slapi_Task *
  110. slapi_new_task(const char *dn)
  111. {
  112. return new_task(dn, NULL);
  113. }
  114. Slapi_Task *
  115. slapi_plugin_new_task(const char *dn, void *plugin)
  116. {
  117. return new_task(dn, plugin);
  118. }
  119. /* slapi_destroy_task: destroy a task
  120. * argument:
  121. * task: task to destroy
  122. * result:
  123. * none
  124. */
  125. void
  126. slapi_destroy_task(void *arg)
  127. {
  128. if (arg) {
  129. destroy_task(1, arg);
  130. }
  131. }
  132. /*
  133. * Sets the initial task state and updated status
  134. */
  135. void slapi_task_begin(Slapi_Task *task, int total_work)
  136. {
  137. if (task) {
  138. task->task_work = total_work;
  139. task->task_progress = 0;
  140. task->task_state = SLAPI_TASK_RUNNING;
  141. slapi_task_status_changed(task);
  142. }
  143. }
  144. /*
  145. * Increments task progress and updates status
  146. */
  147. void slapi_task_inc_progress(Slapi_Task *task)
  148. {
  149. if (task) {
  150. task->task_progress++;
  151. slapi_task_status_changed(task);
  152. }
  153. }
  154. /*
  155. * Sets completed task state and updates status
  156. */
  157. void slapi_task_finish(Slapi_Task *task, int rc)
  158. {
  159. if (task) {
  160. task->task_exitcode = rc;
  161. task->task_state = SLAPI_TASK_FINISHED;
  162. slapi_plugin_op_finished(task->origin_plugin);
  163. slapi_task_status_changed(task);
  164. }
  165. }
  166. /*
  167. * Cancels a task
  168. */
  169. void slapi_task_cancel(Slapi_Task *task, int rc)
  170. {
  171. if (task) {
  172. task->task_exitcode = rc;
  173. task->task_state = SLAPI_TASK_CANCELLED;
  174. slapi_task_status_changed(task);
  175. }
  176. }
  177. /*
  178. * Get the current state of a task
  179. */
  180. int slapi_task_get_state(Slapi_Task *task)
  181. {
  182. if (task) {
  183. return task->task_state;
  184. }
  185. return 0; /* return value not currently used */
  186. }
  187. /* this changes the 'nsTaskStatus' value, which is transient (anything logged
  188. * here wipes out any previous status)
  189. */
  190. void slapi_task_log_status(Slapi_Task *task, char *format, ...)
  191. {
  192. va_list ap;
  193. if (! task->task_status)
  194. task->task_status = (char *)slapi_ch_malloc(10 * LOG_BUFFER);
  195. if (! task->task_status)
  196. return; /* out of memory? */
  197. va_start(ap, format);
  198. PR_vsnprintf(task->task_status, (10 * LOG_BUFFER), format, ap);
  199. va_end(ap);
  200. slapi_task_status_changed(task);
  201. }
  202. void slapi_task_log_status_ext(Slapi_Task *task, char *format, va_list ap)
  203. {
  204. if (! task->task_status)
  205. task->task_status = (char *)slapi_ch_malloc(10 * LOG_BUFFER);
  206. if (! task->task_status)
  207. return; /* out of memory? */
  208. PR_vsnprintf(task->task_status, (10 * LOG_BUFFER), format, ap);
  209. slapi_task_status_changed(task);
  210. }
  211. /* this adds a line to the 'nsTaskLog' value, which is cumulative (anything
  212. * logged here is added to the end)
  213. */
  214. void slapi_task_log_notice(Slapi_Task *task, char *format, ...)
  215. {
  216. va_list ap;
  217. char buffer[LOG_BUFFER];
  218. size_t len;
  219. va_start(ap, format);
  220. PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
  221. va_end(ap);
  222. if (task->task_log_lock) {
  223. PR_Lock(task->task_log_lock);
  224. }
  225. len = 2 + strlen(buffer) + (task->task_log ? strlen(task->task_log) : 0);
  226. if ((len > MAX_SCROLLBACK_BUFFER) && task->task_log) {
  227. size_t i;
  228. char *newbuf;
  229. /* start from middle of buffer, and find next linefeed */
  230. i = strlen(task->task_log)/2;
  231. while (task->task_log[i] && (task->task_log[i] != '\n'))
  232. i++;
  233. if (task->task_log[i])
  234. i++;
  235. len = strlen(task->task_log) - i + 2 + strlen(buffer);
  236. newbuf = (char *)slapi_ch_malloc(len);
  237. strcpy(newbuf, task->task_log + i);
  238. slapi_ch_free((void **)&task->task_log);
  239. task->task_log = newbuf;
  240. } else {
  241. if (! task->task_log) {
  242. task->task_log = (char *)slapi_ch_malloc(len);
  243. task->task_log[0] = 0;
  244. } else {
  245. task->task_log = (char *)slapi_ch_realloc(task->task_log, len);
  246. }
  247. }
  248. if (task->task_log[0])
  249. strcat(task->task_log, "\n");
  250. strcat(task->task_log, buffer);
  251. if (task->task_log_lock) {
  252. PR_Unlock(task->task_log_lock);
  253. }
  254. slapi_task_status_changed(task);
  255. }
  256. void slapi_task_log_notice_ext(Slapi_Task *task, char *format, va_list ap)
  257. {
  258. char buffer[LOG_BUFFER];
  259. size_t len;
  260. PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
  261. if (task->task_log_lock) {
  262. PR_Lock(task->task_log_lock);
  263. }
  264. len = 2 + strlen(buffer) + (task->task_log ? strlen(task->task_log) : 0);
  265. if ((len > MAX_SCROLLBACK_BUFFER) && task->task_log) {
  266. size_t i;
  267. char *newbuf;
  268. /* start from middle of buffer, and find next linefeed */
  269. i = strlen(task->task_log)/2;
  270. while (task->task_log[i] && (task->task_log[i] != '\n'))
  271. i++;
  272. if (task->task_log[i])
  273. i++;
  274. len = strlen(task->task_log) - i + 2 + strlen(buffer);
  275. newbuf = (char *)slapi_ch_malloc(len);
  276. strcpy(newbuf, task->task_log + i);
  277. slapi_ch_free((void **)&task->task_log);
  278. task->task_log = newbuf;
  279. } else {
  280. if (! task->task_log) {
  281. task->task_log = (char *)slapi_ch_malloc(len);
  282. task->task_log[0] = 0;
  283. } else {
  284. task->task_log = (char *)slapi_ch_realloc(task->task_log, len);
  285. }
  286. }
  287. if (task->task_log[0])
  288. strcat(task->task_log, "\n");
  289. strcat(task->task_log, buffer);
  290. if (task->task_log_lock) {
  291. PR_Unlock(task->task_log_lock);
  292. }
  293. slapi_task_status_changed(task);
  294. }
  295. /* update attributes in the entry under "cn=tasks" to match the current
  296. * status of the task. */
  297. void slapi_task_status_changed(Slapi_Task *task)
  298. {
  299. LDAPMod modlist[20];
  300. LDAPMod *mod[20];
  301. int cur = 0, i;
  302. char s1[20], s2[20], s3[20];
  303. if (shutting_down) {
  304. /* don't care about task status updates anymore */
  305. return;
  306. }
  307. if (task->task_log_lock) {
  308. PR_Lock(task->task_log_lock);
  309. }
  310. NEXTMOD(TASK_LOG_NAME, task->task_log);
  311. if (task->task_log_lock) {
  312. PR_Unlock(task->task_log_lock);
  313. }
  314. NEXTMOD(TASK_STATUS_NAME, task->task_status);
  315. sprintf(s1, "%d", task->task_exitcode);
  316. sprintf(s2, "%d", task->task_progress);
  317. sprintf(s3, "%d", task->task_work);
  318. NEXTMOD(TASK_PROGRESS_NAME, s2);
  319. NEXTMOD(TASK_WORK_NAME, s3);
  320. /* only add the exit code when the job is done */
  321. if ((task->task_state == SLAPI_TASK_FINISHED) ||
  322. (task->task_state == SLAPI_TASK_CANCELLED)) {
  323. NEXTMOD(TASK_EXITCODE_NAME, s1);
  324. /* make sure the console can tell the task has ended */
  325. if (task->task_progress != task->task_work) {
  326. task->task_progress = task->task_work;
  327. }
  328. }
  329. mod[cur] = NULL;
  330. modify_internal_entry(task->task_dn, mod);
  331. for (i = 0; i < cur; i++)
  332. slapi_ch_free((void **)&modlist[i].mod_values);
  333. /*
  334. * Removed (task->task_state == SLAPI_TASK_CANCELLED) from
  335. * task_state checking to fix bz 515805.
  336. */
  337. if ((task->task_state == SLAPI_TASK_FINISHED) &&
  338. !(task->task_flags & SLAPI_TASK_DESTROYING)) {
  339. Slapi_PBlock *pb = slapi_pblock_new();
  340. Slapi_Entry *e;
  341. int ttl;
  342. time_t expire;
  343. e = get_internal_entry(pb, task->task_dn);
  344. if (e == NULL)
  345. return;
  346. ttl = atoi(fetch_attr(e, "ttl", DEFAULT_TTL));
  347. if (ttl > 3600)
  348. ttl = 3600; /* be reasonable. */
  349. expire = time(NULL) + ttl;
  350. task->task_flags |= SLAPI_TASK_DESTROYING;
  351. /* queue an event to destroy the state info */
  352. slapi_eq_once(destroy_task, (void *)task, expire);
  353. slapi_free_search_results_internal(pb);
  354. slapi_pblock_destroy(pb);
  355. }
  356. }
  357. /*
  358. * Stash some opaque task specific data in the task for later use.
  359. */
  360. void slapi_task_set_data(Slapi_Task *task, void *data)
  361. {
  362. if (task) {
  363. task->task_private = data;
  364. }
  365. }
  366. /*
  367. * Retrieve some opaque task specific data from the task.
  368. */
  369. void * slapi_task_get_data(Slapi_Task *task)
  370. {
  371. if (task) {
  372. return task->task_private;
  373. }
  374. return NULL; /* return value not currently used */
  375. }
  376. /*
  377. * Increment the task reference count
  378. */
  379. void slapi_task_inc_refcount(Slapi_Task *task)
  380. {
  381. if (task) {
  382. task->task_refcount++;
  383. }
  384. }
  385. /*
  386. * Decrement the task reference count
  387. */
  388. void slapi_task_dec_refcount(Slapi_Task *task)
  389. {
  390. if (task) {
  391. task->task_refcount--;
  392. }
  393. }
  394. /*
  395. * Returns the task reference count
  396. */
  397. int slapi_task_get_refcount(Slapi_Task *task)
  398. {
  399. if (task) {
  400. return task->task_refcount;
  401. }
  402. return 0; /* return value not currently used */
  403. }
  404. int
  405. slapi_plugin_task_register_handler(const char *name, dseCallbackFn func, Slapi_PBlock *plugin_pb)
  406. {
  407. Slapi_PBlock *add_pb = NULL;
  408. Slapi_Operation *op;
  409. LDAPMod *mods[3];
  410. LDAPMod mod[3];
  411. const char *objectclass[3];
  412. const char *cnvals[2];
  413. char *dn = NULL;
  414. int ret = -1;
  415. int x;
  416. dn = slapi_create_dn_string("cn=%s,%s", name, TASK_BASE_DN);
  417. if (NULL == dn) {
  418. LDAPDebug1Arg( LDAP_DEBUG_ANY,
  419. "slapi_task_register_handler: "
  420. "failed to create task dn for %s\n", name);
  421. return ret;
  422. }
  423. add_pb = slapi_pblock_new();
  424. if (add_pb == NULL) {
  425. goto out;
  426. }
  427. /* this is painful :( */
  428. mods[0] = &mod[0];
  429. mod[0].mod_op = LDAP_MOD_ADD;
  430. mod[0].mod_type = "objectClass";
  431. mod[0].mod_values = (char **)objectclass;
  432. objectclass[0] = "top";
  433. objectclass[1] = "extensibleObject";
  434. objectclass[2] = NULL;
  435. mods[1] = &mod[1];
  436. mod[1].mod_op = LDAP_MOD_ADD;
  437. mod[1].mod_type = "cn";
  438. mod[1].mod_values = (char **)cnvals;
  439. cnvals[0] = name;
  440. cnvals[1] = NULL;
  441. mods[2] = NULL;
  442. slapi_add_internal_set_pb(add_pb, dn, mods, NULL,
  443. plugin_get_default_component_id(), 0);
  444. x = 1;
  445. slapi_pblock_set(add_pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  446. /* Make sure these adds don't appear in the audit and change logs */
  447. slapi_pblock_get(add_pb, SLAPI_OPERATION, &op);
  448. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  449. slapi_add_internal_pb(add_pb);
  450. slapi_pblock_get(add_pb, SLAPI_PLUGIN_INTOP_RESULT, &x);
  451. if ((x != LDAP_SUCCESS) && (x != LDAP_ALREADY_EXISTS)) {
  452. LDAPDebug(LDAP_DEBUG_ANY,
  453. "Can't create task node '%s' (error %d)\n",
  454. name, x, 0);
  455. ret = x;
  456. goto out;
  457. }
  458. /* register add callback */
  459. slapi_config_register_callback_plugin(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP,
  460. dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)", func, NULL, plugin_pb);
  461. /* deny modify/delete of the root task entry */
  462. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  463. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  464. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  465. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  466. ret = 0;
  467. out:
  468. slapi_ch_free_string(&dn);
  469. if (add_pb) {
  470. slapi_pblock_destroy(add_pb);
  471. }
  472. return ret;
  473. }
  474. /* name is, for example, "import" */
  475. int slapi_task_register_handler(const char *name, dseCallbackFn func)
  476. {
  477. return slapi_plugin_task_register_handler(name, func, NULL);
  478. }
  479. void slapi_task_set_destructor_fn(Slapi_Task *task, TaskCallbackFn func)
  480. {
  481. if (task) {
  482. task->destructor = func;
  483. }
  484. }
  485. void slapi_task_set_cancel_fn(Slapi_Task *task, TaskCallbackFn func)
  486. {
  487. if (task) {
  488. task->cancel = func;
  489. }
  490. }
  491. /***********************************
  492. * Static Helper Functions
  493. ***********************************/
  494. /* create a new task, fill in DN, and setup modify callback */
  495. static Slapi_Task *
  496. new_task(const char *rawdn, void *plugin)
  497. {
  498. Slapi_Task *task = NULL;
  499. char *dn = NULL;
  500. if (rawdn == NULL) {
  501. return NULL;
  502. }
  503. dn = slapi_create_dn_string("%s", rawdn);
  504. if (NULL == dn) {
  505. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  506. "new_task failed: invalid task dn: %s\n", rawdn);
  507. return NULL;
  508. }
  509. task = (Slapi_Task *)slapi_ch_calloc(1, sizeof(Slapi_Task));
  510. PR_Lock(global_task_lock);
  511. task->next = global_task_list;
  512. global_task_list = task;
  513. PR_Unlock(global_task_lock);
  514. task->task_dn = dn;
  515. task->task_state = SLAPI_TASK_SETUP;
  516. task->task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  517. task->destructor = NULL;
  518. task->cancel = NULL;
  519. task->task_private = NULL;
  520. task->origin_plugin = plugin;
  521. slapi_plugin_op_started(task->origin_plugin);
  522. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, dn,
  523. LDAP_SCOPE_BASE, "(objectclass=*)", task_modify, (void *)task);
  524. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, dn,
  525. LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  526. /* don't add entries under this one */
  527. #if 0
  528. /* don't know why, but this doesn't work. it makes the current add
  529. * operation fail. :(
  530. */
  531. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, dn,
  532. LDAP_SCOPE_SUBTREE, "(objectclass=*)", task_deny, NULL);
  533. #endif
  534. /* To protect task_log to be realloced if it's in use */
  535. task->task_log_lock = PR_NewLock();
  536. return task;
  537. }
  538. /* called by the event queue to destroy a task */
  539. static void
  540. destroy_task(time_t when, void *arg)
  541. {
  542. Slapi_Task *task = (Slapi_Task *)arg;
  543. Slapi_Task *t1;
  544. Slapi_PBlock *pb;
  545. if (task == NULL) return;
  546. pb = slapi_pblock_new();
  547. /* Call the custom destructor callback if one was provided,
  548. * then perform the internal task destruction. */
  549. if (task->destructor != NULL) {
  550. (*task->destructor)(task);
  551. }
  552. task_generic_destructor(task);
  553. /* if when == 0, we're already locked (called during shutdown) */
  554. if (when != 0) {
  555. PR_Lock(global_task_lock);
  556. }
  557. if (global_task_list == task) {
  558. global_task_list = task->next;
  559. } else {
  560. for (t1 = global_task_list; t1; t1 = t1->next) {
  561. if (t1->next == task) {
  562. t1->next = task->next;
  563. break;
  564. }
  565. }
  566. }
  567. if (when != 0) {
  568. PR_Unlock(global_task_lock);
  569. }
  570. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  571. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_modify);
  572. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  573. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny);
  574. slapi_delete_internal_set_pb(pb, task->task_dn, NULL, NULL,
  575. (void *)plugin_get_default_component_id(), 0);
  576. slapi_delete_internal_pb(pb);
  577. slapi_pblock_destroy(pb);
  578. slapi_ch_free((void **)&task->task_dn);
  579. slapi_ch_free((void **)&task);
  580. }
  581. /* extract a single value from the entry (as a string) -- if it's not in the
  582. * entry, the default will be returned (which can be NULL).
  583. * you do not need to free anything returned by this.
  584. */
  585. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  586. const char *default_val)
  587. {
  588. Slapi_Attr *attr;
  589. Slapi_Value *val = NULL;
  590. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  591. return default_val;
  592. slapi_attr_first_value(attr, &val);
  593. return slapi_value_get_string(val);
  594. }
  595. /* supply the pblock, destroy it when you're done */
  596. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn)
  597. {
  598. Slapi_Entry **entries = NULL;
  599. int ret = 0;
  600. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
  601. NULL, 0, NULL, NULL, (void *)plugin_get_default_component_id(), 0);
  602. slapi_search_internal_pb(pb);
  603. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  604. if (ret != LDAP_SUCCESS) {
  605. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  606. dn, 0, 0);
  607. return NULL;
  608. }
  609. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  610. if ((NULL == entries) || (NULL == entries[0])) {
  611. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  612. dn, 0, 0);
  613. return NULL;
  614. }
  615. return entries[0];
  616. }
  617. static void modify_internal_entry(char *dn, LDAPMod **mods)
  618. {
  619. Slapi_PBlock pb;
  620. Slapi_Operation *op;
  621. int ret = 0;
  622. int tries = 0;
  623. int dont_write_file = 1;
  624. do {
  625. pblock_init(&pb);
  626. slapi_modify_internal_set_pb(&pb, dn, mods, NULL, NULL,
  627. (void *)plugin_get_default_component_id(), 0);
  628. /* all modifications to the cn=tasks subtree are transient --
  629. * we erase them all when the server starts up next time, so there's
  630. * no need to save them in the dse file.
  631. */
  632. slapi_pblock_set(&pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
  633. /* Make sure these mods are not logged in audit or changelog */
  634. slapi_pblock_get(&pb, SLAPI_OPERATION, &op);
  635. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  636. slapi_modify_internal_pb(&pb);
  637. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  638. if (ret != LDAP_SUCCESS) {
  639. /* could be waiting for another thread to finish adding this
  640. * entry -- try at least 3 times before giving up.
  641. */
  642. tries++;
  643. if (tries == 3) {
  644. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't modify task "
  645. "entry '%s'; %s (%d)\n", dn, ldap_err2string(ret), ret);
  646. pblock_done(&pb);
  647. return;
  648. }
  649. DS_Sleep(PR_SecondsToInterval(1));
  650. }
  651. pblock_done(&pb);
  652. } while (ret != LDAP_SUCCESS);
  653. }
  654. static void task_generic_destructor(Slapi_Task *task)
  655. {
  656. if (task->task_log) {
  657. slapi_ch_free((void **)&task->task_log);
  658. }
  659. if (task->task_status) {
  660. slapi_ch_free((void **)&task->task_status);
  661. }
  662. if (task->task_log_lock) {
  663. PR_DestroyLock(task->task_log_lock);
  664. task->task_log_lock = NULL;
  665. }
  666. task->task_log = task->task_status = NULL;
  667. }
  668. /********** actual task callbacks **********/
  669. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  670. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  671. {
  672. /* internal operations (conn=NULL) are allowed to do whatever they want */
  673. if (pb->pb_conn == NULL) {
  674. *returncode = LDAP_SUCCESS;
  675. return SLAPI_DSE_CALLBACK_OK;
  676. }
  677. *returncode = LDAP_UNWILLING_TO_PERFORM;
  678. return SLAPI_DSE_CALLBACK_ERROR;
  679. }
  680. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  681. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  682. {
  683. Slapi_Task *task = (Slapi_Task *)arg;
  684. LDAPMod **mods;
  685. int i;
  686. /* the connection block will be NULL for internal operations */
  687. if (pb->pb_conn == NULL) {
  688. *returncode = LDAP_SUCCESS;
  689. return SLAPI_DSE_CALLBACK_OK;
  690. }
  691. /* ignore eAfter, just scan the mods for anything unacceptable */
  692. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  693. for (i = 0; (mods != NULL) && (mods[i] != NULL); i++) {
  694. /* for some reason, "modifiersName" and "modifyTimestamp" are
  695. * stuck in by the server */
  696. if ((strcasecmp(mods[i]->mod_type, "ttl") != 0) &&
  697. (strcasecmp(mods[i]->mod_type, "nsTaskCancel") != 0) &&
  698. (strcasecmp(mods[i]->mod_type, "modifiersName") != 0) &&
  699. (strcasecmp(mods[i]->mod_type, "modifyTimestamp") != 0)) {
  700. /* you aren't allowed to change this! */
  701. *returncode = LDAP_UNWILLING_TO_PERFORM;
  702. return SLAPI_DSE_CALLBACK_ERROR;
  703. }
  704. }
  705. /* okay, we've decided to accept these changes. now look at the new
  706. * entry and absorb any new values.
  707. */
  708. if (strcasecmp(fetch_attr(eAfter, "nsTaskCancel", "false"), "true") == 0) {
  709. /* cancel this task, if not already */
  710. if (task->task_state != SLAPI_TASK_CANCELLED) {
  711. task->task_state = SLAPI_TASK_CANCELLED;
  712. if (task->cancel) {
  713. (*task->cancel)(task);
  714. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  715. fetch_attr(eAfter, "cn", "?"), 0, 0);
  716. }
  717. }
  718. }
  719. /* we fetch ttl from the entry when it's needed */
  720. *returncode = LDAP_SUCCESS;
  721. return SLAPI_DSE_CALLBACK_OK;
  722. }
  723. static int task_import_add(Slapi_PBlock *pb, Slapi_Entry *e,
  724. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  725. {
  726. Slapi_Attr *attr;
  727. Slapi_Value *val = NULL;
  728. Slapi_Backend *be = NULL;
  729. const char *instance_name;
  730. char **ldif_file = NULL, **include = NULL, **exclude = NULL;
  731. int idx, rv = 0;
  732. const char *do_attr_indexes, *uniqueid_kind_str;
  733. int uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  734. Slapi_PBlock mypb;
  735. Slapi_Task *task;
  736. char *nameFrombe_name = NULL;
  737. const char *encrypt_on_import = NULL;
  738. if (fetch_attr(e, "cn", NULL) == NULL) {
  739. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  740. return SLAPI_DSE_CALLBACK_ERROR;
  741. }
  742. instance_name = fetch_attr(e, "nsInstance", NULL);
  743. encrypt_on_import = fetch_attr(e, "nsImportEncrypt", NULL);
  744. /* include/exclude suffixes */
  745. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  746. for (idx = slapi_attr_first_value(attr, &val);
  747. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  748. rv = charray_normdn_add(&include,
  749. (char *)slapi_value_get_string(val),
  750. "nsIncludeSuffix");
  751. if (0 != rv) {
  752. *returncode = LDAP_PARAM_ERROR;
  753. return SLAPI_DSE_CALLBACK_ERROR;
  754. }
  755. }
  756. }
  757. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  758. for (idx = slapi_attr_first_value(attr, &val);
  759. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  760. rv = charray_normdn_add(&exclude,
  761. (char *)slapi_value_get_string(val),
  762. "nsExcludeSuffix");
  763. if (0 != rv) {
  764. *returncode = LDAP_PARAM_ERROR;
  765. return SLAPI_DSE_CALLBACK_ERROR;
  766. }
  767. }
  768. }
  769. /*
  770. * if instance is given, just use it to get the backend.
  771. * otherwise, we use included/excluded suffix list to specify a backend.
  772. */
  773. if (NULL == instance_name) {
  774. char **instances, **ip;
  775. int counter;
  776. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  777. &instances) < 0) {
  778. LDAPDebug(LDAP_DEBUG_ANY,
  779. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  780. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  781. return SLAPI_DSE_CALLBACK_ERROR;
  782. }
  783. if (instances) {
  784. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  785. ;
  786. if (counter == 1){
  787. instance_name = *instances;
  788. nameFrombe_name = *instances;
  789. }
  790. else if (counter == 0) {
  791. LDAPDebug(LDAP_DEBUG_ANY,
  792. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  793. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  794. return SLAPI_DSE_CALLBACK_ERROR;
  795. } else {
  796. LDAPDebug(LDAP_DEBUG_ANY,
  797. "ERROR: Multiple backend instances are specified: "
  798. "%s, %s, ...\n", instances[0], instances[1], 0);
  799. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  800. return SLAPI_DSE_CALLBACK_ERROR;
  801. }
  802. } else {
  803. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  804. return SLAPI_DSE_CALLBACK_ERROR;
  805. }
  806. }
  807. /* lookup the backend */
  808. be = slapi_be_select_by_instance_name(instance_name);
  809. if (be == NULL) {
  810. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  811. instance_name, 0, 0);
  812. slapi_ch_free_string(&nameFrombe_name);
  813. *returncode = LDAP_NO_SUCH_OBJECT;
  814. return SLAPI_DSE_CALLBACK_ERROR;
  815. }
  816. /* refuse to do an import on pre-V3 plugins. plugin api V3 is the one
  817. * for DS 5.0 where the import/export stuff changed a lot.
  818. */
  819. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  820. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an import with pre-V3 "
  821. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  822. *returncode = LDAP_UNWILLING_TO_PERFORM;
  823. slapi_ch_free_string(&nameFrombe_name);
  824. return SLAPI_DSE_CALLBACK_ERROR;
  825. }
  826. if (be->be_database->plg_ldif2db == NULL) {
  827. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no ldif2db function defined for "
  828. "backend %s\n", be->be_database->plg_name, 0, 0);
  829. *returncode = LDAP_UNWILLING_TO_PERFORM;
  830. slapi_ch_free_string(&nameFrombe_name);
  831. return SLAPI_DSE_CALLBACK_ERROR;
  832. }
  833. /* get ldif filenames -- from here on, memory has been allocated */
  834. if (slapi_entry_attr_find(e, "nsFilename", &attr) != 0) {
  835. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  836. slapi_ch_free_string(&nameFrombe_name);
  837. return SLAPI_DSE_CALLBACK_ERROR;
  838. }
  839. for (idx = slapi_attr_first_value(attr, &val);
  840. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  841. charray_add(&ldif_file, slapi_ch_strdup(slapi_value_get_string(val)));
  842. }
  843. do_attr_indexes = fetch_attr(e, "nsImportIndexAttrs", "true");
  844. uniqueid_kind_str = fetch_attr(e, "nsUniqueIdGenerator", NULL);
  845. if (uniqueid_kind_str != NULL) {
  846. if (strcasecmp(uniqueid_kind_str, "none") == 0) {
  847. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NONE;
  848. } else if (strcasecmp(uniqueid_kind_str, "deterministic") == 0) {
  849. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NAME_BASED;
  850. } else {
  851. /* default - time based */
  852. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  853. }
  854. }
  855. /* allocate new task now */
  856. task = slapi_new_task(slapi_entry_get_ndn(e));
  857. if (task == NULL) {
  858. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  859. rv = LDAP_OPERATIONS_ERROR;
  860. goto out;
  861. }
  862. memset(&mypb, 0, sizeof(mypb));
  863. mypb.pb_backend = be;
  864. mypb.pb_plugin = be->be_database;
  865. mypb.pb_removedupvals = atoi(fetch_attr(e, "nsImportChunkSize", "0"));
  866. mypb.pb_ldif2db_noattrindexes =
  867. !(strcasecmp(do_attr_indexes, "true") == 0);
  868. mypb.pb_ldif_generate_uniqueid = uniqueid_kind;
  869. mypb.pb_ldif_namespaceid =
  870. (char *)fetch_attr(e, "nsUniqueIdGeneratorNamespace", NULL);
  871. mypb.pb_instance_name = (char *)instance_name;
  872. mypb.pb_ldif_files = ldif_file;
  873. mypb.pb_ldif_include = include;
  874. mypb.pb_ldif_exclude = exclude;
  875. mypb.pb_task = task;
  876. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  877. if (NULL != encrypt_on_import && 0 == strcasecmp(encrypt_on_import, "true") ) {
  878. mypb.pb_ldif_encrypt = 1;
  879. }
  880. rv = (*mypb.pb_plugin->plg_ldif2db)(&mypb);
  881. if (rv == 0) {
  882. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  883. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  884. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  885. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  886. }
  887. out:
  888. slapi_ch_free_string(&nameFrombe_name);
  889. charray_free(ldif_file);
  890. charray_free(include);
  891. charray_free(exclude);
  892. if (rv != 0) {
  893. *returncode = LDAP_OPERATIONS_ERROR;
  894. destroy_task(1, task);
  895. return SLAPI_DSE_CALLBACK_ERROR;
  896. }
  897. *returncode = LDAP_SUCCESS;
  898. return SLAPI_DSE_CALLBACK_OK;
  899. }
  900. static void task_export_thread(void *arg)
  901. {
  902. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  903. char **instance_names = (char **)pb->pb_instance_name;
  904. char **inp;
  905. char *ldif_file = pb->pb_ldif_file;
  906. char *this_ldif_file = NULL;
  907. Slapi_Backend *be = NULL;
  908. int rv = -1;
  909. int count;
  910. Slapi_Task *task = pb->pb_task;
  911. g_incr_active_threadcnt();
  912. for (count = 0, inp = instance_names; *inp; inp++, count++)
  913. ;
  914. slapi_task_begin(task, count);
  915. for (inp = instance_names; *inp; inp++) {
  916. int release_me = 0;
  917. /* lookup the backend */
  918. be = slapi_be_select_by_instance_name((const char *)*inp);
  919. if (be == NULL) {
  920. /* shouldn't happen */
  921. LDAPDebug(LDAP_DEBUG_ANY, "ldbm2ldif: backend '%s' is AWOL!\n",
  922. (const char *)*inp, 0, 0);
  923. continue;
  924. }
  925. pb->pb_backend = be;
  926. pb->pb_plugin = be->be_database;
  927. pb->pb_instance_name = (char *)*inp;
  928. /* ldif_file name for each? */
  929. if (pb->pb_ldif_printkey & EXPORT_APPENDMODE) {
  930. if (inp == instance_names) { /* first export */
  931. pb->pb_ldif_printkey |= EXPORT_APPENDMODE_1;
  932. } else {
  933. pb->pb_ldif_printkey &= ~EXPORT_APPENDMODE_1;
  934. }
  935. } else {
  936. if (strcmp(ldif_file, "-")) { /* not '-' */
  937. char *p;
  938. #if defined( _WIN32 )
  939. char sep = '\\';
  940. if (NULL != strchr(ldif_file, '/'))
  941. sep = '/';
  942. #else
  943. char sep = '/';
  944. #endif
  945. this_ldif_file = (char *)slapi_ch_malloc(strlen(ldif_file) +
  946. strlen(*inp) + 2);
  947. p = strrchr(ldif_file, sep);
  948. if (NULL == p) {
  949. sprintf(this_ldif_file, "%s_%s", *inp, ldif_file);
  950. } else {
  951. char *q;
  952. q = p + 1;
  953. *p = '\0';
  954. sprintf(this_ldif_file, "%s%c%s_%s",
  955. ldif_file, sep, *inp, q);
  956. *p = sep;
  957. }
  958. pb->pb_ldif_file = this_ldif_file;
  959. release_me = 1;
  960. }
  961. }
  962. slapi_task_log_notice(task, "Beginning export of '%s'", *inp);
  963. LDAPDebug(LDAP_DEBUG_ANY, "Beginning export of '%s'\n", *inp, 0, 0);
  964. rv = (*pb->pb_plugin->plg_db2ldif)(pb);
  965. if (rv != 0) {
  966. slapi_task_log_notice(task, "backend '%s' export failed (%d)",
  967. *inp, rv);
  968. LDAPDebug(LDAP_DEBUG_ANY,
  969. "ldbm2ldif: backend '%s' export failed (%d)\n",
  970. (const char *)*inp, rv, 0);
  971. }
  972. if (release_me) {
  973. slapi_ch_free((void **)&this_ldif_file);
  974. }
  975. if (rv != 0)
  976. break;
  977. slapi_task_inc_progress(task);
  978. }
  979. /* free the memory now */
  980. charray_free(instance_names);
  981. slapi_ch_free((void **)&ldif_file);
  982. charray_free(pb->pb_ldif_include);
  983. charray_free(pb->pb_ldif_exclude);
  984. slapi_pblock_destroy(pb);
  985. if (rv == 0) {
  986. slapi_task_log_notice(task, "Export finished.");
  987. LDAPDebug(LDAP_DEBUG_ANY, "Export finished.\n", 0, 0, 0);
  988. } else {
  989. slapi_task_log_notice(task, "Export failed.");
  990. LDAPDebug(LDAP_DEBUG_ANY, "Export failed.\n", 0, 0, 0);
  991. }
  992. slapi_task_finish(task, rv);
  993. g_decr_active_threadcnt();
  994. }
  995. static int task_export_add(Slapi_PBlock *pb, Slapi_Entry *e,
  996. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  997. {
  998. Slapi_Attr *attr;
  999. Slapi_Value *val = NULL;
  1000. Slapi_Backend *be = NULL;
  1001. char *ldif_file = NULL;
  1002. char **instance_names = NULL, **inp;
  1003. char **include = NULL, **exclude = NULL;
  1004. int idx, rv = SLAPI_DSE_CALLBACK_OK;
  1005. int export_replica_flag = 0;
  1006. int ldif_printkey_flag = 0;
  1007. int dump_uniqueid_flag = 0;
  1008. int instance_cnt = 0;
  1009. const char *my_ldif_file;
  1010. const char *use_one_file;
  1011. const char *export_replica;
  1012. const char *ldif_printkey;
  1013. const char *dump_uniqueid;
  1014. Slapi_PBlock *mypb = NULL;
  1015. Slapi_Task *task = NULL;
  1016. PRThread *thread;
  1017. const char *decrypt_on_export = NULL;
  1018. *returncode = LDAP_SUCCESS;
  1019. if (fetch_attr(e, "cn", NULL) == NULL) {
  1020. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1021. rv = SLAPI_DSE_CALLBACK_ERROR;
  1022. goto out;
  1023. }
  1024. decrypt_on_export = fetch_attr(e, "nsExportDecrypt", NULL);
  1025. /* nsInstances -- from here on, memory has been allocated */
  1026. if (slapi_entry_attr_find(e, "nsInstance", &attr) == 0) {
  1027. for (idx = slapi_attr_first_value(attr, &val);
  1028. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1029. charray_add(&instance_names,
  1030. slapi_ch_strdup(slapi_value_get_string(val)));
  1031. instance_cnt++;
  1032. }
  1033. }
  1034. /* include/exclude suffixes */
  1035. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  1036. for (idx = slapi_attr_first_value(attr, &val);
  1037. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1038. rv = charray_normdn_add(&include,
  1039. (char *)slapi_value_get_string(val),
  1040. "nsIncludeSuffix");
  1041. if (0 != rv) {
  1042. *returncode = LDAP_PARAM_ERROR;
  1043. goto out;
  1044. }
  1045. }
  1046. }
  1047. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  1048. for (idx = slapi_attr_first_value(attr, &val);
  1049. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1050. rv = charray_normdn_add(&exclude,
  1051. (char *)slapi_value_get_string(val),
  1052. "nsExcludeSuffix");
  1053. if (0 != rv) {
  1054. *returncode = LDAP_PARAM_ERROR;
  1055. goto out;
  1056. }
  1057. }
  1058. }
  1059. if (NULL == instance_names) {
  1060. char **ip;
  1061. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  1062. &instance_names) < 0) {
  1063. LDAPDebug(LDAP_DEBUG_ANY,
  1064. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1065. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1066. rv = SLAPI_DSE_CALLBACK_ERROR;
  1067. goto out;
  1068. }
  1069. if (instance_names) {
  1070. for (ip = instance_names, instance_cnt = 0; ip && *ip;
  1071. ip++, instance_cnt++)
  1072. ;
  1073. if (instance_cnt == 0) {
  1074. LDAPDebug(LDAP_DEBUG_ANY,
  1075. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1076. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1077. rv = SLAPI_DSE_CALLBACK_ERROR;
  1078. goto out;
  1079. }
  1080. } else {
  1081. LDAPDebug(LDAP_DEBUG_ANY,
  1082. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1083. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1084. rv = SLAPI_DSE_CALLBACK_ERROR;
  1085. goto out;
  1086. }
  1087. }
  1088. /* ldif file name */
  1089. if ((my_ldif_file = fetch_attr(e, "nsFilename", NULL)) == NULL) {
  1090. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1091. rv = SLAPI_DSE_CALLBACK_ERROR;
  1092. goto out;
  1093. }
  1094. ldif_file = slapi_ch_strdup(my_ldif_file);
  1095. /* if true, multiple backends are dumped into one ldif file */
  1096. use_one_file = fetch_attr(e, "nsUseOneFile", "true");
  1097. if (strcasecmp(use_one_file, "true") == 0) {
  1098. ldif_printkey_flag |= EXPORT_APPENDMODE;
  1099. }
  1100. /* -r: export replica */
  1101. export_replica = fetch_attr(e, "nsExportReplica", "false");
  1102. if (!strcasecmp(export_replica, "true")) /* true */
  1103. export_replica_flag = 1;
  1104. /* -N: eq "false" ==> does not print out key value */
  1105. ldif_printkey = fetch_attr(e, "nsPrintKey", "true");
  1106. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1107. ldif_printkey_flag |= EXPORT_PRINTKEY;
  1108. /* -C: eq "true" ==> use only id2entry file */
  1109. ldif_printkey = fetch_attr(e, "nsUseId2Entry", "false");
  1110. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1111. ldif_printkey_flag |= EXPORT_ID2ENTRY_ONLY;
  1112. /* if "true" ==> 8-bit strings are not base64 encoded */
  1113. ldif_printkey = fetch_attr(e, "nsMinimalEncoding", "false");
  1114. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1115. ldif_printkey_flag |= EXPORT_MINIMAL_ENCODING;
  1116. /* -U: eq "true" ==> does not fold the output */
  1117. ldif_printkey = fetch_attr(e, "nsNoWrap", "false");
  1118. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1119. ldif_printkey_flag |= EXPORT_NOWRAP;
  1120. /* -1: eq "true" ==> does not print version line */
  1121. ldif_printkey = fetch_attr(e, "nsNoVersionLine", "false");
  1122. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1123. ldif_printkey_flag |= EXPORT_NOVERSION;
  1124. /* -u: eq "false" ==> does not dump unique id */
  1125. dump_uniqueid = fetch_attr(e, "nsDumpUniqId", "true");
  1126. if (!strcasecmp(dump_uniqueid, "true")) /* true */
  1127. dump_uniqueid_flag = 1;
  1128. /* check that all the backends are ok */
  1129. for (inp = instance_names; *inp; inp++) {
  1130. /* lookup the backend */
  1131. be = slapi_be_select_by_instance_name((const char *)*inp);
  1132. if (be == NULL) {
  1133. LDAPDebug(LDAP_DEBUG_ANY,
  1134. "can't export to nonexistent backend %s\n", *inp, 0, 0);
  1135. *returncode = LDAP_NO_SUCH_OBJECT;
  1136. rv = SLAPI_DSE_CALLBACK_ERROR;
  1137. goto out;
  1138. }
  1139. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1140. * for DS 5.0 where the import/export stuff changed a lot.
  1141. */
  1142. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1143. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an export with pre-V3 "
  1144. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1145. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1146. rv = SLAPI_DSE_CALLBACK_ERROR;
  1147. goto out;
  1148. }
  1149. if (be->be_database->plg_db2ldif == NULL) {
  1150. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2ldif function defined for "
  1151. "backend %s\n", be->be_database->plg_name, 0, 0);
  1152. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1153. rv = SLAPI_DSE_CALLBACK_ERROR;
  1154. goto out;
  1155. }
  1156. }
  1157. /* allocate new task now */
  1158. task = slapi_new_task(slapi_entry_get_ndn(e));
  1159. if (task == NULL) {
  1160. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1161. *returncode = LDAP_OPERATIONS_ERROR;
  1162. rv = SLAPI_DSE_CALLBACK_ERROR;
  1163. goto out;
  1164. }
  1165. mypb = slapi_pblock_new();
  1166. if (mypb == NULL) {
  1167. *returncode = LDAP_OPERATIONS_ERROR;
  1168. rv = SLAPI_DSE_CALLBACK_ERROR;
  1169. goto out;
  1170. }
  1171. mypb->pb_ldif_include = include;
  1172. mypb->pb_ldif_exclude = exclude;
  1173. mypb->pb_ldif_printkey = ldif_printkey_flag;
  1174. mypb->pb_ldif_dump_replica = export_replica_flag;
  1175. mypb->pb_ldif_dump_uniqueid = dump_uniqueid_flag;
  1176. mypb->pb_ldif_file = ldif_file;
  1177. /* horrible hack */
  1178. mypb->pb_instance_name = (char *)instance_names;
  1179. mypb->pb_task = task;
  1180. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1181. if (NULL != decrypt_on_export && 0 == strcasecmp(decrypt_on_export, "true") ) {
  1182. mypb->pb_ldif_encrypt = 1;
  1183. }
  1184. /* start the export as a separate thread */
  1185. thread = PR_CreateThread(PR_USER_THREAD, task_export_thread,
  1186. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1187. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1188. if (thread == NULL) {
  1189. LDAPDebug(LDAP_DEBUG_ANY,
  1190. "unable to create ldbm2ldif thread!\n", 0, 0, 0);
  1191. *returncode = LDAP_OPERATIONS_ERROR;
  1192. rv = SLAPI_DSE_CALLBACK_ERROR;
  1193. slapi_pblock_destroy(mypb);
  1194. goto out;
  1195. }
  1196. /* thread successful -- don't free the pb, let the thread do that. */
  1197. return SLAPI_DSE_CALLBACK_OK;
  1198. out:
  1199. charray_free(instance_names);
  1200. charray_free(include);
  1201. charray_free(exclude);
  1202. if (ldif_file != NULL) {
  1203. slapi_ch_free((void **)&ldif_file);
  1204. }
  1205. if (task) {
  1206. destroy_task(1, task);
  1207. }
  1208. return rv;
  1209. }
  1210. static void task_backup_thread(void *arg)
  1211. {
  1212. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1213. Slapi_Task *task = pb->pb_task;
  1214. int rv;
  1215. g_incr_active_threadcnt();
  1216. slapi_task_begin(task, 1);
  1217. slapi_task_log_notice(task, "Beginning backup of '%s'",
  1218. pb->pb_plugin->plg_name);
  1219. LDAPDebug(LDAP_DEBUG_ANY, "Beginning backup of '%s'\n",
  1220. pb->pb_plugin->plg_name, 0, 0);
  1221. rv = (*pb->pb_plugin->plg_db2archive)(pb);
  1222. if (rv != 0) {
  1223. slapi_task_log_notice(task, "Backup failed (error %d)", rv);
  1224. slapi_task_log_status(task, "Backup failed (error %d)", rv);
  1225. LDAPDebug(LDAP_DEBUG_ANY, "Backup failed (error %d)\n", rv, 0, 0);
  1226. } else {
  1227. slapi_task_log_notice(task, "Backup finished.");
  1228. slapi_task_log_status(task, "Backup finished.");
  1229. LDAPDebug(LDAP_DEBUG_ANY, "Backup finished.\n", 0, 0, 0);
  1230. }
  1231. slapi_task_finish(task, rv);
  1232. slapi_ch_free((void **)&pb->pb_seq_val);
  1233. slapi_pblock_destroy(pb);
  1234. g_decr_active_threadcnt();
  1235. }
  1236. static int task_backup_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1237. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1238. {
  1239. Slapi_Backend *be = NULL;
  1240. PRThread *thread = NULL;
  1241. const char *archive_dir = NULL;
  1242. const char *my_database_type = NULL;
  1243. const char *database_type = "ldbm database";
  1244. char *cookie = NULL;
  1245. int rv = SLAPI_DSE_CALLBACK_OK;
  1246. Slapi_PBlock *mypb = NULL;
  1247. Slapi_Task *task = NULL;
  1248. *returncode = LDAP_SUCCESS;
  1249. if (fetch_attr(e, "cn", NULL) == NULL) {
  1250. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1251. rv = SLAPI_DSE_CALLBACK_ERROR;
  1252. goto out;
  1253. }
  1254. /* archive dir name */
  1255. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1256. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1257. rv = SLAPI_DSE_CALLBACK_ERROR;
  1258. goto out;
  1259. }
  1260. /* database type */
  1261. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1262. if (NULL != my_database_type)
  1263. database_type = my_database_type;
  1264. /* get backend that has db2archive and the database type matches. */
  1265. cookie = NULL;
  1266. be = slapi_get_first_backend(&cookie);
  1267. while (be) {
  1268. if (NULL != be->be_database->plg_db2archive &&
  1269. !strcasecmp(database_type, be->be_database->plg_name))
  1270. break;
  1271. be = (backend *)slapi_get_next_backend (cookie);
  1272. }
  1273. slapi_ch_free((void **)&cookie);
  1274. if (NULL == be || NULL == be->be_database->plg_db2archive) {
  1275. LDAPDebug(LDAP_DEBUG_ANY,
  1276. "ERROR: no db2archive function defined.\n", 0, 0, 0);
  1277. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1278. rv = SLAPI_DSE_CALLBACK_ERROR;
  1279. goto out;
  1280. }
  1281. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1282. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an backup with pre-V3 "
  1283. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1284. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1285. rv = SLAPI_DSE_CALLBACK_ERROR;
  1286. goto out;
  1287. }
  1288. /* allocate new task now */
  1289. task = slapi_new_task(slapi_entry_get_ndn(e));
  1290. if (task == NULL) {
  1291. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1292. *returncode = LDAP_OPERATIONS_ERROR;
  1293. rv = SLAPI_DSE_CALLBACK_ERROR;
  1294. goto out;
  1295. }
  1296. mypb = slapi_pblock_new();
  1297. if (mypb == NULL) {
  1298. *returncode = LDAP_OPERATIONS_ERROR;
  1299. rv = SLAPI_DSE_CALLBACK_ERROR;
  1300. goto out;
  1301. }
  1302. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1303. mypb->pb_plugin = be->be_database;
  1304. mypb->pb_task = task;
  1305. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1306. /* start the backup as a separate thread */
  1307. thread = PR_CreateThread(PR_USER_THREAD, task_backup_thread,
  1308. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1309. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1310. if (thread == NULL) {
  1311. LDAPDebug(LDAP_DEBUG_ANY,
  1312. "unable to create backup thread!\n", 0, 0, 0);
  1313. *returncode = LDAP_OPERATIONS_ERROR;
  1314. rv = SLAPI_DSE_CALLBACK_ERROR;
  1315. slapi_ch_free((void **)&mypb->pb_seq_val);
  1316. slapi_pblock_destroy(mypb);
  1317. goto out;
  1318. }
  1319. /* thread successful -- don't free the pb, let the thread do that. */
  1320. return SLAPI_DSE_CALLBACK_OK;
  1321. out:
  1322. if (task) {
  1323. destroy_task(1, task);
  1324. }
  1325. return rv;
  1326. }
  1327. static void task_restore_thread(void *arg)
  1328. {
  1329. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1330. Slapi_Task *task = pb->pb_task;
  1331. int rv;
  1332. g_incr_active_threadcnt();
  1333. slapi_task_begin(task, 1);
  1334. slapi_task_log_notice(task, "Beginning restore to '%s'",
  1335. pb->pb_plugin->plg_name);
  1336. LDAPDebug(LDAP_DEBUG_ANY, "Beginning restore to '%s'\n",
  1337. pb->pb_plugin->plg_name, 0, 0);
  1338. rv = (*pb->pb_plugin->plg_archive2db)(pb);
  1339. if (rv != 0) {
  1340. slapi_task_log_notice(task, "Restore failed (error %d)", rv);
  1341. slapi_task_log_status(task, "Restore failed (error %d)", rv);
  1342. LDAPDebug(LDAP_DEBUG_ANY, "Restore failed (error %d)\n", rv, 0, 0);
  1343. } else {
  1344. slapi_task_log_notice(task, "Restore finished.");
  1345. slapi_task_log_status(task, "Restore finished.");
  1346. LDAPDebug(LDAP_DEBUG_ANY, "Restore finished.\n", 0, 0, 0);
  1347. }
  1348. slapi_task_finish(task, rv);
  1349. slapi_ch_free((void **)&pb->pb_seq_val);
  1350. slapi_ch_free_string(&pb->pb_instance_name);
  1351. slapi_pblock_destroy(pb);
  1352. g_decr_active_threadcnt();
  1353. }
  1354. static int task_restore_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1355. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1356. {
  1357. Slapi_Backend *be = NULL;
  1358. const char *instance_name = NULL;
  1359. const char *archive_dir = NULL;
  1360. const char *my_database_type = NULL;
  1361. const char *database_type = "ldbm database";
  1362. char *cookie = NULL;
  1363. int rv = SLAPI_DSE_CALLBACK_OK;
  1364. Slapi_PBlock *mypb = NULL;
  1365. Slapi_Task *task = NULL;
  1366. PRThread *thread = NULL;
  1367. *returncode = LDAP_SUCCESS;
  1368. if (fetch_attr(e, "cn", NULL) == NULL) {
  1369. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1370. rv = SLAPI_DSE_CALLBACK_ERROR;
  1371. goto out;
  1372. }
  1373. /* archive dir name */
  1374. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1375. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1376. rv = SLAPI_DSE_CALLBACK_ERROR;
  1377. goto out;
  1378. }
  1379. /* database type */
  1380. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1381. if (NULL != my_database_type)
  1382. database_type = my_database_type;
  1383. instance_name = fetch_attr(e, "nsInstance", NULL);
  1384. /* get backend that has archive2db and the database type matches. */
  1385. cookie = NULL;
  1386. be = slapi_get_first_backend (&cookie);
  1387. while (be) {
  1388. if (NULL != be->be_database->plg_archive2db &&
  1389. !strcasecmp(database_type, be->be_database->plg_name))
  1390. break;
  1391. be = (backend *)slapi_get_next_backend (cookie);
  1392. }
  1393. slapi_ch_free((void **)&cookie);
  1394. if (NULL == be || NULL == be->be_database->plg_archive2db) {
  1395. LDAPDebug(LDAP_DEBUG_ANY,
  1396. "ERROR: no archive2db function defined.\n", 0, 0, 0);
  1397. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1398. rv = SLAPI_DSE_CALLBACK_ERROR;
  1399. goto out;
  1400. }
  1401. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1402. * for DS 5.0 where the import/export stuff changed a lot.
  1403. */
  1404. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1405. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an restore with pre-V3 "
  1406. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1407. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1408. rv = SLAPI_DSE_CALLBACK_ERROR;
  1409. goto out;
  1410. }
  1411. /* allocate new task now */
  1412. task = slapi_new_task(slapi_entry_get_ndn(e));
  1413. if (task == NULL) {
  1414. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1415. *returncode = LDAP_OPERATIONS_ERROR;
  1416. rv = SLAPI_DSE_CALLBACK_ERROR;
  1417. goto out;
  1418. }
  1419. mypb = slapi_pblock_new();
  1420. if (mypb == NULL) {
  1421. *returncode = LDAP_OPERATIONS_ERROR;
  1422. rv = SLAPI_DSE_CALLBACK_ERROR;
  1423. goto out;
  1424. }
  1425. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1426. mypb->pb_plugin = be->be_database;
  1427. if (NULL != instance_name)
  1428. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1429. mypb->pb_task = task;
  1430. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1431. /* start the restore as a separate thread */
  1432. thread = PR_CreateThread(PR_USER_THREAD, task_restore_thread,
  1433. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1434. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1435. if (thread == NULL) {
  1436. LDAPDebug(LDAP_DEBUG_ANY,
  1437. "unable to create restore thread!\n", 0, 0, 0);
  1438. *returncode = LDAP_OPERATIONS_ERROR;
  1439. rv = SLAPI_DSE_CALLBACK_ERROR;
  1440. slapi_ch_free((void **)&mypb->pb_seq_val);
  1441. slapi_ch_free_string(&pb->pb_instance_name);
  1442. slapi_pblock_destroy(mypb);
  1443. goto out;
  1444. }
  1445. /* thread successful -- don't free the pb, let the thread do that. */
  1446. return SLAPI_DSE_CALLBACK_OK;
  1447. out:
  1448. if (task) {
  1449. destroy_task(1, task);
  1450. }
  1451. return rv;
  1452. }
  1453. static void task_index_thread(void *arg)
  1454. {
  1455. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1456. Slapi_Task *task = pb->pb_task;
  1457. int rv;
  1458. g_incr_active_threadcnt();
  1459. slapi_task_begin(task, 1);
  1460. rv = (*pb->pb_plugin->plg_db2index)(pb);
  1461. if (rv != 0) {
  1462. slapi_task_log_notice(task, "Index failed (error %d)", rv);
  1463. slapi_task_log_status(task, "Index failed (error %d)", rv);
  1464. LDAPDebug(LDAP_DEBUG_ANY, "Index failed (error %d)\n", rv, 0, 0);
  1465. }
  1466. slapi_task_finish(task, rv);
  1467. charray_free(pb->pb_db2index_attrs);
  1468. slapi_ch_free((void **)&pb->pb_instance_name);
  1469. slapi_pblock_destroy(pb);
  1470. g_decr_active_threadcnt();
  1471. }
  1472. static int task_index_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1473. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1474. {
  1475. const char *instance_name;
  1476. int rv = SLAPI_DSE_CALLBACK_OK;
  1477. Slapi_Backend *be = NULL;
  1478. Slapi_Task *task = NULL;
  1479. Slapi_Attr *attr;
  1480. Slapi_Value *val = NULL;
  1481. char **indexlist = NULL;
  1482. int idx;
  1483. Slapi_PBlock *mypb = NULL;
  1484. PRThread *thread = NULL;
  1485. *returncode = LDAP_SUCCESS;
  1486. if (fetch_attr(e, "cn", NULL) == NULL) {
  1487. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1488. rv = SLAPI_DSE_CALLBACK_ERROR;
  1489. goto out;
  1490. }
  1491. if ((instance_name = fetch_attr(e, "nsInstance", NULL)) == NULL) {
  1492. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1493. rv = SLAPI_DSE_CALLBACK_ERROR;
  1494. goto out;
  1495. }
  1496. /* lookup the backend */
  1497. be = slapi_be_select_by_instance_name(instance_name);
  1498. if (be == NULL) {
  1499. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  1500. instance_name, 0, 0);
  1501. *returncode = LDAP_NO_SUCH_OBJECT;
  1502. return SLAPI_DSE_CALLBACK_ERROR;
  1503. }
  1504. if (be->be_database->plg_db2index == NULL) {
  1505. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2index function defined for "
  1506. "backend %s\n", be->be_database->plg_name, 0, 0);
  1507. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1508. return SLAPI_DSE_CALLBACK_ERROR;
  1509. }
  1510. /* normal indexes */
  1511. if (slapi_entry_attr_find(e, "nsIndexAttribute", &attr) == 0) {
  1512. for (idx = slapi_attr_first_value(attr, &val);
  1513. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1514. const char *indexname = slapi_value_get_string(val);
  1515. char *index = slapi_ch_smprintf("t%s", indexname);
  1516. if (index != NULL) {
  1517. charray_add(&indexlist, index);
  1518. }
  1519. }
  1520. }
  1521. /* vlv indexes */
  1522. if (slapi_entry_attr_find(e, "nsIndexVlvAttribute", &attr) == 0) {
  1523. for (idx = slapi_attr_first_value(attr, &val);
  1524. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1525. const char *indexname = slapi_value_get_string(val);
  1526. char *index = slapi_ch_smprintf("T%s", indexname);
  1527. if (index != NULL) {
  1528. charray_add(&indexlist, index);
  1529. }
  1530. }
  1531. }
  1532. if (NULL == indexlist) {
  1533. LDAPDebug(LDAP_DEBUG_ANY, "no index is specified!\n", 0, 0, 0);
  1534. *returncode = LDAP_OPERATIONS_ERROR;
  1535. rv = SLAPI_DSE_CALLBACK_OK;
  1536. goto out;
  1537. }
  1538. /* allocate new task now */
  1539. task = slapi_new_task(slapi_entry_get_ndn(e));
  1540. if (task == NULL) {
  1541. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1542. *returncode = LDAP_OPERATIONS_ERROR;
  1543. rv = SLAPI_DSE_CALLBACK_ERROR;
  1544. goto out;
  1545. }
  1546. mypb = slapi_pblock_new();
  1547. if (mypb == NULL) {
  1548. *returncode = LDAP_OPERATIONS_ERROR;
  1549. rv = SLAPI_DSE_CALLBACK_ERROR;
  1550. goto out;
  1551. }
  1552. mypb->pb_backend = be;
  1553. mypb->pb_plugin = be->be_database;
  1554. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1555. mypb->pb_db2index_attrs = indexlist;
  1556. mypb->pb_task = task;
  1557. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1558. /* start the db2index as a separate thread */
  1559. thread = PR_CreateThread(PR_USER_THREAD, task_index_thread,
  1560. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1561. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1562. if (thread == NULL) {
  1563. LDAPDebug(LDAP_DEBUG_ANY,
  1564. "unable to create index thread!\n", 0, 0, 0);
  1565. rv = SLAPI_DSE_CALLBACK_ERROR;
  1566. slapi_ch_free((void **)&mypb->pb_instance_name);
  1567. slapi_pblock_destroy(mypb);
  1568. goto out;
  1569. }
  1570. /* thread successful -- don't free the pb, let the thread do that. */
  1571. return SLAPI_DSE_CALLBACK_OK;
  1572. out:
  1573. if (task) {
  1574. destroy_task(1, task);
  1575. }
  1576. if (indexlist) {
  1577. charray_free(indexlist);
  1578. }
  1579. return rv;
  1580. }
  1581. static int
  1582. task_upgradedb_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1583. int *returncode, char *returntext, void *arg)
  1584. {
  1585. int rv = SLAPI_DSE_CALLBACK_OK;
  1586. Slapi_Backend *be = NULL;
  1587. Slapi_Task *task = NULL;
  1588. Slapi_PBlock mypb;
  1589. const char *archive_dir = NULL;
  1590. const char *force = NULL;
  1591. const char *database_type = "ldbm database";
  1592. const char *my_database_type = NULL;
  1593. char *cookie = NULL;
  1594. *returncode = LDAP_SUCCESS;
  1595. if (fetch_attr(e, "cn", NULL) == NULL) {
  1596. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1597. rv = SLAPI_DSE_CALLBACK_ERROR;
  1598. goto out;
  1599. }
  1600. /* archive dir name */
  1601. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1602. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1603. rv = SLAPI_DSE_CALLBACK_ERROR;
  1604. goto out;
  1605. }
  1606. /* database type */
  1607. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1608. if (NULL != my_database_type)
  1609. database_type = my_database_type;
  1610. /* force to reindex? */
  1611. force = fetch_attr(e, "nsForceToReindex", NULL);
  1612. /* get backend that has db2archive and the database type matches. */
  1613. cookie = NULL;
  1614. be = slapi_get_first_backend(&cookie);
  1615. while (be) {
  1616. if (NULL != be->be_database->plg_upgradedb)
  1617. break;
  1618. be = (backend *)slapi_get_next_backend (cookie);
  1619. }
  1620. slapi_ch_free((void **)&cookie);
  1621. if (NULL == be) {
  1622. LDAPDebug(LDAP_DEBUG_ANY,
  1623. "ERROR: no upgradedb is defined.\n", 0, 0, 0);
  1624. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1625. rv = SLAPI_DSE_CALLBACK_ERROR;
  1626. goto out;
  1627. }
  1628. if (NULL == be->be_database->plg_upgradedb ||
  1629. strcasecmp(database_type, be->be_database->plg_name)) {
  1630. LDAPDebug(LDAP_DEBUG_ANY,
  1631. "ERROR: no upgradedb is defined in %s.\n",
  1632. be->be_database->plg_name, 0, 0);
  1633. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1634. rv = SLAPI_DSE_CALLBACK_ERROR;
  1635. goto out;
  1636. }
  1637. /* allocate new task now */
  1638. task = slapi_new_task(slapi_entry_get_ndn(e));
  1639. if (task == NULL) {
  1640. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1641. *returncode = LDAP_OPERATIONS_ERROR;
  1642. rv = SLAPI_DSE_CALLBACK_ERROR;
  1643. goto out;
  1644. }
  1645. /* NGK - This could use some cleanup to use the SLAPI task API, such as slapi_task_begin() */
  1646. task->task_work = 1;
  1647. task->task_progress = 0;
  1648. memset(&mypb, 0, sizeof(mypb));
  1649. mypb.pb_backend = be;
  1650. mypb.pb_plugin = be->be_database;
  1651. if (force && 0 == strcasecmp(force, "true"))
  1652. mypb.pb_seq_type = SLAPI_UPGRADEDB_FORCE; /* force; reindex all regardless the dbversion */
  1653. mypb.pb_seq_val = slapi_ch_strdup(archive_dir);
  1654. mypb.pb_task = task;
  1655. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1656. rv = (mypb.pb_plugin->plg_upgradedb)(&mypb);
  1657. if (rv == 0) {
  1658. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  1659. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  1660. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  1661. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  1662. }
  1663. out:
  1664. slapi_ch_free((void **)&mypb.pb_seq_val);
  1665. if (rv != 0) {
  1666. if (task)
  1667. destroy_task(1, task);
  1668. *returncode = LDAP_OPERATIONS_ERROR;
  1669. return SLAPI_DSE_CALLBACK_ERROR;
  1670. }
  1671. *returncode = LDAP_SUCCESS;
  1672. return SLAPI_DSE_CALLBACK_OK;
  1673. }
  1674. /*
  1675. * sysconfig reload task
  1676. *
  1677. * dn: cn=keytab_update,cn=sysconfig reload,cn=tasks,cn=config
  1678. * objectclass: top
  1679. * objectclass: extensibleObject
  1680. * cn: keytab_update
  1681. * sysconfigfile: /etc/sysconfig/dirsrv-localhost
  1682. * logchanges: <boolean>
  1683. *
  1684. * Reload environment variables from the instance sysconfig file, or
  1685. * any file using the following formats:
  1686. *
  1687. * VARIABLE=value
  1688. * export VARIABLE=value
  1689. * set VARIABLE value
  1690. * unset VARIABLE
  1691. * setenv VARIABLE value
  1692. * unsetenv VARIABLE
  1693. */
  1694. static int
  1695. task_sysconfig_reload_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode,
  1696. char *returntext, void *arg)
  1697. {
  1698. FILE *file = NULL;
  1699. char *filename = NULL;
  1700. PRBool logchanges = 0;
  1701. int rc = SLAPI_DSE_CALLBACK_OK;
  1702. *returncode = LDAP_SUCCESS;
  1703. if(( filename = slapi_entry_attr_get_charptr(e, TASK_SYSCONFIG_FILE_ATTR))){
  1704. file = fopen ( filename, "r" );
  1705. } else {
  1706. *returncode = LDAP_OPERATIONS_ERROR;
  1707. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "missing required attribute \"%s\".",
  1708. TASK_SYSCONFIG_FILE_ATTR);
  1709. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n", returntext, 0, 0);
  1710. rc = SLAPI_DSE_CALLBACK_ERROR;
  1711. goto done;
  1712. }
  1713. /* see if we should log the changes made */
  1714. logchanges = slapi_entry_attr_get_bool(e, TASK_SYSCONFIG_LOGCHANGES_ATTR);
  1715. if ( file != NULL ){
  1716. char line[4096];
  1717. char *s = NULL;
  1718. /* fgets() reads in at most one less than size characters */
  1719. char *end_of_line = line + sizeof(line) - 1;
  1720. if(logchanges){
  1721. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: processing file (%s)\n",
  1722. filename, 0 , 0);
  1723. }
  1724. while ( fgets ( line, sizeof line, file ) != NULL ){
  1725. if(line[0] == '#'){
  1726. /* skip comments */
  1727. continue;
  1728. } else {
  1729. char env_value[sizeof(line)];
  1730. char env_var[sizeof(line)];
  1731. int using_setenv = 0;
  1732. int value_index = 0;
  1733. int start_value = 0;
  1734. int var_index = 0;
  1735. int inquotes = 0;
  1736. memset(env_var, 0, sizeof(env_var));
  1737. memset(env_value, 0, sizeof(env_value));
  1738. /*
  1739. * Remove leading spaces and tabs
  1740. */
  1741. for (s = line; s && *s; s++){
  1742. if(*s != ' ' && *s != '\t'){
  1743. break;
  1744. }
  1745. }
  1746. /*
  1747. * Check for "export", "setenv", and "unsetenv" assignments
  1748. */
  1749. if(strncmp(s, "export ", 7) == 0 ||
  1750. strncmp(s, "set ", 4) == 0 ||
  1751. strncmp(s, "unset ", 6) == 0 ||
  1752. strncmp(s, "setenv ", 7) == 0 ||
  1753. strncmp(s, "unsetenv ", 9) == 0 )
  1754. {
  1755. if(*s == 's' || *s == 'u'){ /* */
  1756. /*
  1757. * Using setenv/unsetenv/set/unset, so we need to handle spaces
  1758. * differently for these assignments.
  1759. */
  1760. using_setenv = 1;
  1761. }
  1762. if(strncmp(s, "export ", 7) == 0){
  1763. /* strip off "export " */
  1764. s = s + 7;
  1765. } else if(strncmp(s, "set ", 4) == 0){
  1766. /* strip off "set " */
  1767. s = s + 4;
  1768. } else if(strncmp(s, "unset ", 6) == 0){
  1769. /* strip off "unset " */
  1770. s = s + 6;
  1771. } else if(strncmp(s, "setenv ", 7) == 0){
  1772. /* strip off "setenv " */
  1773. s = s + 7;
  1774. } else if(strncmp(s, "unsetenv ", 9) == 0){
  1775. /* strip off "unsetenv " */
  1776. s = s + 9;
  1777. }
  1778. while(*s == ' ' || *s == '\t'){
  1779. /* remove any extra spaces/tabs between the assignment cmd and the name */
  1780. s++;
  1781. }
  1782. }
  1783. /*
  1784. * Start parsing the names and values
  1785. */
  1786. for (; s && (s < end_of_line) && *s; s++){
  1787. /*
  1788. * If using "setenv", allow the first space/tab only, and start on the env value
  1789. */
  1790. if(using_setenv && (*s == ' ' || *s == '\t')){
  1791. using_setenv = 0; /* finished doing special space parsing for setenv */
  1792. start_value = 1; /* start working on the value */
  1793. while(*s == ' ' || *s == '\t'){
  1794. /* remove any extra spaces/tabs between variable name and value */
  1795. s++;
  1796. }
  1797. } else if( ((*s == ';' || *s == ' ') && !inquotes) || *s == '\0' || *s == '\n' || *s == '\r'){
  1798. /* we're done processing the value */
  1799. break;
  1800. }
  1801. /* need to handle quoted values */
  1802. if(*s == '"'){
  1803. if(inquotes){
  1804. inquotes = 0;
  1805. } else {
  1806. inquotes = 1;
  1807. }
  1808. }
  1809. if(start_value){
  1810. /* Build the environment variable value */
  1811. env_value[value_index] = *s;
  1812. value_index++;
  1813. } else if(*s == '='){
  1814. /* Start on the environment variable value next */
  1815. start_value = 1;
  1816. } else {
  1817. /* Build the environment variable name. skip "export" */
  1818. env_var[var_index] = *s;
  1819. var_index++;
  1820. }
  1821. }
  1822. if(var_index > 0){
  1823. /* Update the environment variable */
  1824. if(setenv(env_var, env_value, 1) != 0){
  1825. *returncode = LDAP_OPERATIONS_ERROR;
  1826. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"failed to set (%s)", env_var);
  1827. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n",returntext,0,0);
  1828. rc = SLAPI_DSE_CALLBACK_ERROR;
  1829. break;
  1830. }
  1831. if(logchanges){
  1832. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: set (%s) to (%s)\n",
  1833. env_var, env_value , 0);
  1834. }
  1835. }
  1836. }
  1837. }
  1838. fclose ( file );
  1839. } else {
  1840. *returncode = LDAP_OPERATIONS_ERROR;
  1841. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"failed to open file \"%s\" (%s)",
  1842. filename, strerror(errno));
  1843. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n", returntext, 0, 0);
  1844. rc = SLAPI_DSE_CALLBACK_ERROR;
  1845. }
  1846. done:
  1847. slapi_ch_free_string(&filename);
  1848. return rc;
  1849. }
  1850. /* cleanup old tasks that may still be in the DSE from a previous session
  1851. * (this can happen if the server crashes [no matter how unlikely we like
  1852. * to think that is].)
  1853. */
  1854. void task_cleanup(void)
  1855. {
  1856. Slapi_PBlock *pb = slapi_pblock_new();
  1857. Slapi_Entry **entries = NULL;
  1858. int ret = 0, i, x;
  1859. Slapi_DN *rootDN;
  1860. slapi_search_internal_set_pb(pb, TASK_BASE_DN, LDAP_SCOPE_SUBTREE,
  1861. "(objectclass=*)", NULL, 0, NULL, NULL,
  1862. (void *)plugin_get_default_component_id(), 0);
  1863. slapi_search_internal_pb(pb);
  1864. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1865. if (ret != LDAP_SUCCESS) {
  1866. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  1867. "be AWOL!\n", 0, 0, 0);
  1868. slapi_pblock_destroy(pb);
  1869. return;
  1870. }
  1871. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1872. if (NULL == entries) {
  1873. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  1874. "be AWOL!\n", 0, 0, 0);
  1875. slapi_pblock_destroy(pb);
  1876. return;
  1877. }
  1878. rootDN = slapi_sdn_new_dn_byval(TASK_BASE_DN);
  1879. /* rotate through entries, skipping the base dn */
  1880. for (i = 0; entries[i] != NULL; i++) {
  1881. const Slapi_DN *sdn = slapi_entry_get_sdn_const(entries[i]);
  1882. Slapi_PBlock *mypb;
  1883. Slapi_Operation *op;
  1884. if (slapi_sdn_compare(sdn, rootDN) == 0)
  1885. continue;
  1886. mypb = slapi_pblock_new();
  1887. if (mypb == NULL) {
  1888. continue;
  1889. }
  1890. slapi_delete_internal_set_pb(mypb, slapi_sdn_get_dn(sdn), NULL, NULL,
  1891. plugin_get_default_component_id(), 0);
  1892. /* Make sure these deletes don't appear in the audit and change logs */
  1893. slapi_pblock_get(mypb, SLAPI_OPERATION, &op);
  1894. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  1895. x = 1;
  1896. slapi_pblock_set(mypb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  1897. slapi_delete_internal_pb(mypb);
  1898. slapi_pblock_destroy(mypb);
  1899. }
  1900. slapi_sdn_free(&rootDN);
  1901. slapi_free_search_results_internal(pb);
  1902. slapi_pblock_destroy(pb);
  1903. }
  1904. void task_init(void)
  1905. {
  1906. global_task_lock = PR_NewLock();
  1907. if (global_task_lock == NULL) {
  1908. LDAPDebug(LDAP_DEBUG_ANY, "unable to create global tasks lock! "
  1909. "(that's bad)\n", 0, 0, 0);
  1910. return;
  1911. }
  1912. slapi_task_register_handler("import", task_import_add);
  1913. slapi_task_register_handler("export", task_export_add);
  1914. slapi_task_register_handler("backup", task_backup_add);
  1915. slapi_task_register_handler("restore", task_restore_add);
  1916. slapi_task_register_handler("index", task_index_add);
  1917. slapi_task_register_handler("upgradedb", task_upgradedb_add);
  1918. slapi_task_register_handler("sysconfig reload", task_sysconfig_reload_add);
  1919. }
  1920. /* called when the server is shutting down -- abort all existing tasks */
  1921. void task_shutdown(void)
  1922. {
  1923. Slapi_Task *task;
  1924. int found_any = 0;
  1925. /* first, cancel all tasks */
  1926. PR_Lock(global_task_lock);
  1927. shutting_down = 1;
  1928. for (task = global_task_list; task; task = task->next) {
  1929. if ((task->task_state != SLAPI_TASK_CANCELLED) &&
  1930. (task->task_state != SLAPI_TASK_FINISHED)) {
  1931. task->task_state = SLAPI_TASK_CANCELLED;
  1932. if (task->cancel) {
  1933. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  1934. task->task_dn, 0, 0);
  1935. (*task->cancel)(task);
  1936. found_any = 1;
  1937. }
  1938. }
  1939. }
  1940. if (found_any) {
  1941. /* give any tasks 1 second to say their last rites */
  1942. DS_Sleep(PR_SecondsToInterval( 1 ));
  1943. }
  1944. while (global_task_list) {
  1945. destroy_task(0, global_task_list);
  1946. }
  1947. PR_Unlock(global_task_lock);
  1948. }