import-threads.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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. /*
  39. * the threads that make up an import:
  40. * producer (1)
  41. * foreman (1)
  42. * worker (N: 1 for each index)
  43. *
  44. * a wire import (aka "fast replica" import) won't have a producer thread.
  45. */
  46. #include "back-ldbm.h"
  47. #include "vlv_srch.h"
  48. #include "import.h"
  49. #ifdef XP_WIN32
  50. #define STDIN_FILENO 0
  51. #endif
  52. static struct backentry *import_make_backentry(Slapi_Entry *e, ID id)
  53. {
  54. struct backentry *ep = backentry_alloc();
  55. if (NULL != ep) {
  56. ep->ep_entry = e;
  57. ep->ep_id = id;
  58. }
  59. return ep;
  60. }
  61. static void import_decref_entry(struct backentry *ep)
  62. {
  63. PR_AtomicDecrement(&(ep->ep_refcnt));
  64. PR_ASSERT(ep->ep_refcnt >= 0);
  65. }
  66. /* generate uniqueid if requested */
  67. static void import_generate_uniqueid(ImportJob *job, Slapi_Entry *e)
  68. {
  69. const char *uniqueid = slapi_entry_get_uniqueid(e);
  70. int rc;
  71. if (!uniqueid && (job->uuid_gen_type != SLAPI_UNIQUEID_GENERATE_NONE)) {
  72. char *newuniqueid;
  73. /* generate id based on dn */
  74. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  75. char *dn = slapi_entry_get_dn(e);
  76. rc = slapi_uniqueIDGenerateFromNameString(&newuniqueid,
  77. job->uuid_namespace, dn, strlen(dn));
  78. } else {
  79. /* time based */
  80. rc = slapi_uniqueIDGenerateString(&newuniqueid);
  81. }
  82. if (rc == UID_SUCCESS) {
  83. slapi_entry_set_uniqueid (e, newuniqueid);
  84. } else {
  85. char ebuf[BUFSIZ];
  86. LDAPDebug( LDAP_DEBUG_ANY,
  87. "import_generate_uniqueid: failed to generate "
  88. "uniqueid for %s; error=%d.\n",
  89. escape_string(slapi_entry_get_dn_const(e), ebuf), rc, 0 );
  90. }
  91. }
  92. }
  93. /********** BETTER LDIF PARSER **********/
  94. /* like the function in libldif, except this one doesn't need to use
  95. * FILE (which breaks on various platforms for >4G files or large numbers
  96. * of open files)
  97. */
  98. #define LDIF_BUFFER_SIZE 8192
  99. typedef struct {
  100. char *b; /* buffer */
  101. size_t size; /* how full the buffer is */
  102. size_t offset; /* where the current entry starts */
  103. } ldif_context;
  104. static void import_init_ldif(ldif_context *c)
  105. {
  106. c->size = c->offset = 0;
  107. c->b = NULL;
  108. }
  109. static void import_free_ldif(ldif_context *c)
  110. {
  111. if (c->b)
  112. FREE(c->b);
  113. import_init_ldif(c);
  114. }
  115. static char *import_get_entry(ldif_context *c, int fd, int *lineno)
  116. {
  117. int ret;
  118. int done = 0, got_lf = 0;
  119. size_t bufSize = 0, bufOffset = 0, i;
  120. char *buf = NULL;
  121. while (!done) {
  122. /* If there's no data in the buffer, get some */
  123. if ((c->size == 0) || (c->offset == c->size)) {
  124. /* Do we even have a buffer ? */
  125. if (! c->b) {
  126. c->b = slapi_ch_malloc(LDIF_BUFFER_SIZE);
  127. if (! c->b)
  128. return NULL;
  129. }
  130. ret = read(fd, c->b, LDIF_BUFFER_SIZE);
  131. if (ret < 0) {
  132. /* Must be error */
  133. goto error;
  134. } else if (ret == 0) {
  135. /* eof */
  136. if (buf) {
  137. /* last entry */
  138. buf[bufOffset] = 0;
  139. return buf;
  140. }
  141. return NULL;
  142. } else {
  143. /* read completed OK */
  144. c->size = ret;
  145. c->offset = 0;
  146. }
  147. }
  148. /* skip blank lines at start of entry */
  149. if (bufOffset == 0) {
  150. size_t n;
  151. char *p;
  152. for (n = c->offset, p = c->b + n; n < c->size; n++, p++) {
  153. if (!(*p == '\r' || *p == '\n' || *p == ' '|| *p == '\t'))
  154. break;
  155. }
  156. c->offset = n;
  157. if (c->offset == c->size) continue;
  158. }
  159. i = c->offset;
  160. while (!done && (i < c->size)) {
  161. /* scan forward in the buffer, looking for the end of the entry */
  162. while ((i < c->size) && (c->b[i] != '\n'))
  163. i++;
  164. if ((i < c->size) && (c->b[i] == '\n')) {
  165. if (got_lf && ((i == 0) || ((i == 1) && (c->b[0] == '\r')))) {
  166. /* saw an lf at the end of the last buffer */
  167. i++, (*lineno)++;
  168. done = 1;
  169. got_lf = 0;
  170. break;
  171. }
  172. got_lf = 0;
  173. (*lineno)++;
  174. /* is this the end? (need another linefeed) */
  175. if (++i < c->size) {
  176. if (c->b[i] == '\n') {
  177. /* gotcha! */
  178. i++, (*lineno)++;
  179. done = 1;
  180. } else if (c->b[i] == '\r') {
  181. if (++i < c->size) {
  182. if (c->b[i] == '\n') {
  183. /* gotcha! (nt) */
  184. i++, (*lineno)++;
  185. done = 1;
  186. }
  187. } else {
  188. got_lf = 1;
  189. }
  190. }
  191. } else {
  192. /* lf at the very end of the buffer */
  193. got_lf = 1;
  194. }
  195. }
  196. }
  197. /* copy what we did so far into the output buffer */
  198. /* (first, make sure the output buffer is large enough) */
  199. if (bufSize - bufOffset < i - c->offset + 1) {
  200. char *newbuf = NULL;
  201. size_t newsize = (buf ? bufSize*2 : LDIF_BUFFER_SIZE);
  202. newbuf = slapi_ch_malloc(newsize);
  203. if (! newbuf)
  204. goto error;
  205. /* copy over the old data (if there was any) */
  206. if (buf) {
  207. memmove(newbuf, buf, bufOffset);
  208. slapi_ch_free((void **)&buf);
  209. }
  210. buf = newbuf;
  211. bufSize = newsize;
  212. }
  213. memmove(buf + bufOffset, c->b + c->offset, i - c->offset);
  214. bufOffset += (i - c->offset);
  215. c->offset = i;
  216. }
  217. /* add terminating NUL char */
  218. buf[bufOffset] = 0;
  219. return buf;
  220. error:
  221. if (buf)
  222. slapi_ch_free((void **)&buf);
  223. return NULL;
  224. }
  225. /********** THREADS **********/
  226. /*
  227. * Description:
  228. * 1) return the ldif version #
  229. * 2) replace "version: 1" with "#ersion: 1"
  230. * to pretend like a comment for the str2entry
  231. */
  232. static int
  233. import_get_version(char *str)
  234. {
  235. char *s;
  236. char *type;
  237. char *valuecharptr;
  238. char *mystr, *ms;
  239. int offset;
  240. int valuelen;
  241. int my_version = 0;
  242. int retmalloc = 0;
  243. if ((s = strstr(str, "version:")) == NULL)
  244. return 0;
  245. offset = s - str;
  246. mystr = ms = slapi_ch_strdup(str);
  247. while ( (s = ldif_getline( &ms )) != NULL ) {
  248. char *errmsg = NULL;
  249. if ( (retmalloc = ldif_parse_line( s, &type, &valuecharptr, &valuelen, &errmsg )) >= 0 ) {
  250. if (!strcasecmp(type, "version")) {
  251. my_version = atoi(valuecharptr);
  252. *(str + offset) = '#';
  253. /* the memory below was not allocated by the slapi_ch_ functions */
  254. if (errmsg) PR_smprintf_free(errmsg);
  255. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  256. break;
  257. }
  258. } else if ( errmsg != NULL ) {
  259. LDAPDebug( LDAP_DEBUG_PARSE, "%s", errmsg, 0, 0 );
  260. }
  261. /* the memory below was not allocated by the slapi_ch_ functions */
  262. if (errmsg) slapi_ch_free((void **) &errmsg);
  263. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  264. }
  265. slapi_ch_free((void **)&mystr);
  266. return my_version;
  267. }
  268. /* producer thread:
  269. * read through the given file list, parsing entries (str2entry), assigning
  270. * them IDs and queueing them on the entry FIFO. other threads will do
  271. * the indexing.
  272. */
  273. void import_producer(void *param)
  274. {
  275. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  276. ImportJob *job = info->job;
  277. ID id = job->first_ID, id_filestart = id;
  278. Slapi_Entry *e = NULL;
  279. struct backentry *ep = NULL, *old_ep = NULL;
  280. ldbm_instance *inst = job->inst;
  281. PRIntervalTime sleeptime;
  282. char *estr = NULL;
  283. int str2entry_flags =
  284. SLAPI_STR2ENTRY_TOMBSTONE_CHECK |
  285. SLAPI_STR2ENTRY_REMOVEDUPVALS |
  286. SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES |
  287. SLAPI_STR2ENTRY_ADDRDNVALS |
  288. SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF;
  289. int finished = 0;
  290. int detected_eof = 0;
  291. int fd, curr_file, curr_lineno;
  292. char *curr_filename = NULL;
  293. int idx;
  294. ldif_context c;
  295. int my_version = 0;
  296. size_t newesize = 0;
  297. PR_ASSERT(info != NULL);
  298. PR_ASSERT(inst != NULL);
  299. if ( job->flags & FLAG_ABORT ) {
  300. goto error;
  301. }
  302. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  303. /* pause until we're told to run */
  304. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  305. info->state = WAITING;
  306. DS_Sleep(sleeptime);
  307. }
  308. info->state = RUNNING;
  309. import_init_ldif(&c);
  310. /* jumpstart by opening the first file */
  311. curr_file = 0;
  312. fd = -1;
  313. detected_eof = finished = 0;
  314. /* we loop around reading the input files and processing each entry
  315. * as we read it.
  316. */
  317. while (! finished) {
  318. Slapi_Attr *attr = NULL;
  319. int flags = 0;
  320. int prev_lineno = 0;
  321. int lines_in_entry = 0;
  322. if (job->flags & FLAG_ABORT) {
  323. goto error;
  324. }
  325. /* move on to next file? */
  326. if (detected_eof) {
  327. /* check if the file can still be read, whine if so... */
  328. if (read(fd, (void *)&idx, 1) > 0) {
  329. import_log_notice(job, "WARNING: Unexpected end of file found "
  330. "at line %d of file \"%s\"", curr_lineno,
  331. curr_filename);
  332. }
  333. if (fd == STDIN_FILENO) {
  334. import_log_notice(job, "Finished scanning file stdin (%lu "
  335. "entries)", (u_long)(id-id_filestart));
  336. } else {
  337. import_log_notice(job, "Finished scanning file \"%s\" (%lu "
  338. "entries)", curr_filename, (u_long)(id-id_filestart));
  339. }
  340. close(fd);
  341. fd = -1;
  342. detected_eof = 0;
  343. id_filestart = id;
  344. curr_file++;
  345. if (job->task) {
  346. job->task->task_progress++;
  347. slapi_task_status_changed(job->task);
  348. }
  349. if (job->input_filenames[curr_file] == NULL) {
  350. /* done! */
  351. finished = 1;
  352. break;
  353. }
  354. }
  355. /* separate from above, because this is also triggered when we
  356. * start (to open the first file)
  357. */
  358. if (fd < 0) {
  359. curr_lineno = 0;
  360. curr_filename = job->input_filenames[curr_file];
  361. if (strcmp(curr_filename, "-") == 0) {
  362. fd = STDIN_FILENO;
  363. } else {
  364. int o_flag = O_RDONLY;
  365. #ifdef XP_WIN32
  366. /* 613041 Somehow the windows low level io lose "\n"
  367. at a very particular situation using O_TEXT mode read.
  368. I think it is a windows bug for O_TEXT mode read.
  369. Use O_BINARY instead, which honestly returns chars
  370. without any translation.
  371. */
  372. o_flag |= O_BINARY;
  373. #endif
  374. fd = dblayer_open_huge_file(curr_filename, o_flag, 0);
  375. }
  376. if (fd < 0) {
  377. import_log_notice(job, "Could not open LDIF file \"%s\"",
  378. curr_filename);
  379. goto error;
  380. }
  381. if (fd == STDIN_FILENO) {
  382. import_log_notice(job, "Processing file stdin");
  383. } else {
  384. import_log_notice(job, "Processing file \"%s\"", curr_filename);
  385. }
  386. }
  387. if (job->flags & FLAG_ABORT) {
  388. goto error;
  389. }
  390. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  391. info->state = WAITING;
  392. DS_Sleep(sleeptime);
  393. }
  394. info->state = RUNNING;
  395. prev_lineno = curr_lineno;
  396. estr = import_get_entry(&c, fd, &curr_lineno);
  397. lines_in_entry = curr_lineno - prev_lineno;
  398. if (!estr) {
  399. /* error reading entry, or end of file */
  400. detected_eof = 1;
  401. continue;
  402. }
  403. if (0 == my_version && strstr(estr, "version:")) {
  404. my_version = import_get_version(estr);
  405. str2entry_flags |= SLAPI_STR2ENTRY_INCLUDE_VERSION_STR;
  406. }
  407. /* If there are more than so many lines in the entry, we tell
  408. * str2entry to optimize for a large entry.
  409. */
  410. if (lines_in_entry > STR2ENTRY_ATTRIBUTE_PRESENCE_CHECK_THRESHOLD) {
  411. flags = str2entry_flags | SLAPI_STR2ENTRY_BIGENTRY;
  412. } else {
  413. flags = str2entry_flags;
  414. }
  415. e = slapi_str2entry(estr, flags);
  416. FREE(estr);
  417. if (! e) {
  418. if (!(str2entry_flags & SLAPI_STR2ENTRY_INCLUDE_VERSION_STR))
  419. import_log_notice(job, "WARNING: skipping bad LDIF entry "
  420. "ending line %d of file \"%s\"", curr_lineno,
  421. curr_filename);
  422. continue;
  423. }
  424. if (0 == my_version) {
  425. /* after the first entry version string won't be given */
  426. my_version = -1;
  427. }
  428. if (! import_entry_belongs_here(e, inst->inst_be)) {
  429. /* silently skip */
  430. if (e) {
  431. job->not_here_skipped++;
  432. slapi_entry_free(e);
  433. }
  434. continue;
  435. }
  436. if (slapi_entry_schema_check(NULL, e) != 0) {
  437. char ebuf[BUFSIZ];
  438. import_log_notice(job, "WARNING: skipping entry \"%s\" which "
  439. "violates schema, ending line %d of file "
  440. "\"%s\"", escape_string(slapi_entry_get_dn(e), ebuf),
  441. curr_lineno, curr_filename);
  442. if (e)
  443. slapi_entry_free(e);
  444. job->skipped++;
  445. continue;
  446. }
  447. /* generate uniqueid if necessary */
  448. import_generate_uniqueid(job, e);
  449. ep = import_make_backentry(e, id);
  450. if (!ep)
  451. goto error;
  452. /* check for include/exclude subtree lists */
  453. if (! ldbm_back_ok_to_dump(backentry_get_ndn(ep),
  454. job->include_subtrees,
  455. job->exclude_subtrees)) {
  456. backentry_free(&ep);
  457. continue;
  458. }
  459. /* not sure what this does, but it looked like it could be
  460. * simplified. if it's broken, it's my fault. -robey
  461. */
  462. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  463. Slapi_Value **va = attr_get_present_values(attr);
  464. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  465. }
  466. if (job->flags & FLAG_ABORT) {
  467. goto error;
  468. }
  469. /* Now we have this new entry, all decoded
  470. * Next thing we need to do is:
  471. * (1) see if the appropriate fifo location contains an
  472. * entry which had been processed by the indexers.
  473. * If so, proceed.
  474. * If not, spin waiting for it to become free.
  475. * (2) free the old entry and store the new one there.
  476. * (3) Update the job progress indicators so the indexers
  477. * can use the new entry.
  478. */
  479. idx = id % job->fifo.size;
  480. old_ep = job->fifo.item[idx].entry;
  481. if (old_ep) {
  482. /* for the slot to be recycled, it needs to be already absorbed
  483. * by the foreman (id >= ready_ID), and all the workers need to
  484. * be finished with it (refcount = 0).
  485. */
  486. while (((old_ep->ep_refcnt > 0) ||
  487. (old_ep->ep_id >= job->ready_ID))
  488. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  489. info->state = WAITING;
  490. DS_Sleep(sleeptime);
  491. }
  492. if (job->flags & FLAG_ABORT){
  493. goto error;
  494. }
  495. info->state = RUNNING;
  496. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  497. job->fifo.item[idx].entry = NULL;
  498. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  499. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  500. else
  501. job->fifo.c_bsize = 0;
  502. backentry_free(&old_ep);
  503. }
  504. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  505. if (newesize > job->fifo.bsize) { /* entry too big */
  506. char ebuf[BUFSIZ];
  507. import_log_notice(job, "WARNING: skipping entry \"%s\" "
  508. "ending line %d of file \"%s\"",
  509. escape_string(slapi_entry_get_dn(e), ebuf),
  510. curr_lineno, curr_filename);
  511. import_log_notice(job, "REASON: entry too large (%d bytes) for "
  512. "the buffer size (%d bytes)", newesize, job->fifo.bsize);
  513. backentry_free(&ep);
  514. job->skipped++;
  515. continue;
  516. }
  517. /* Now check if fifo has enough space for the new entry */
  518. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  519. import_wait_for_space_in_fifo( job, newesize );
  520. }
  521. /* We have enough space */
  522. job->fifo.item[idx].filename = curr_filename;
  523. job->fifo.item[idx].line = curr_lineno;
  524. job->fifo.item[idx].entry = ep;
  525. job->fifo.item[idx].bad = 0;
  526. job->fifo.item[idx].esize = newesize;
  527. /* Add the entry size to total fifo size */
  528. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  529. /* Update the job to show our progress */
  530. job->lead_ID = id;
  531. if ((id - info->first_ID) <= job->fifo.size) {
  532. job->trailing_ID = info->first_ID;
  533. } else {
  534. job->trailing_ID = id - job->fifo.size;
  535. }
  536. /* Update our progress meter too */
  537. info->last_ID_processed = id;
  538. id++;
  539. if (job->flags & FLAG_ABORT){
  540. goto error;
  541. }
  542. if (info->command == STOP) {
  543. if (fd >= 0)
  544. close(fd);
  545. finished = 1;
  546. }
  547. }
  548. import_free_ldif(&c);
  549. info->state = FINISHED;
  550. return;
  551. error:
  552. info->state = ABORTED;
  553. }
  554. /* producer thread for re-indexing:
  555. * read id2entry, parsing entries (str2entry) (needed???), assigning
  556. * them IDs (again, needed???) and queueing them on the entry FIFO.
  557. * other threads will do the indexing -- same as in import.
  558. */
  559. void index_producer(void *param)
  560. {
  561. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  562. ImportJob *job = info->job;
  563. ID id = job->first_ID;
  564. Slapi_Entry *e = NULL;
  565. struct backentry *ep = NULL, *old_ep = NULL;
  566. ldbm_instance *inst = job->inst;
  567. PRIntervalTime sleeptime;
  568. int finished = 0;
  569. int idx;
  570. /* vars for Berkeley DB */
  571. DB_ENV *env = NULL;
  572. DB *db = NULL;
  573. DBC *dbc = NULL;
  574. DBT key = {0};
  575. DBT data = {0};
  576. int db_rval = -1;
  577. backend *be = inst->inst_be;
  578. int isfirst = 1;
  579. int curr_entry = 0;
  580. size_t newesize = 0;
  581. PR_ASSERT(info != NULL);
  582. PR_ASSERT(inst != NULL);
  583. PR_ASSERT(be != NULL);
  584. if ( job->flags & FLAG_ABORT )
  585. goto error;
  586. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  587. /* pause until we're told to run */
  588. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  589. info->state = WAITING;
  590. DS_Sleep(sleeptime);
  591. }
  592. info->state = RUNNING;
  593. /* open id2entry with dedicated db env and db handler */
  594. if ( dblayer_get_aux_id2entry( be, &db, &env ) != 0 || db == NULL ||
  595. env == NULL) {
  596. LDAPDebug( LDAP_DEBUG_ANY, "Could not open id2entry\n", 0, 0, 0 );
  597. goto error;
  598. }
  599. /* get a cursor to we can walk over the table */
  600. db_rval = db->cursor(db, NULL, &dbc, 0);
  601. if ( 0 != db_rval ) {
  602. LDAPDebug( LDAP_DEBUG_ANY,
  603. "Failed to get cursor for reindexing\n", 0, 0, 0 );
  604. dblayer_release_id2entry(be, db);
  605. goto error;
  606. }
  607. /* we loop around reading the input files and processing each entry
  608. * as we read it.
  609. */
  610. finished = 0;
  611. while (!finished) {
  612. Slapi_Attr *attr = NULL;
  613. ID temp_id;
  614. if (job->flags & FLAG_ABORT) {
  615. goto error;
  616. }
  617. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  618. info->state = WAITING;
  619. DS_Sleep(sleeptime);
  620. }
  621. info->state = RUNNING;
  622. key.flags = DB_DBT_MALLOC;
  623. data.flags = DB_DBT_MALLOC;
  624. if (isfirst)
  625. {
  626. db_rval = dbc->c_get(dbc, &key, &data, DB_FIRST);
  627. isfirst = 0;
  628. }
  629. else
  630. {
  631. db_rval = dbc->c_get(dbc, &key, &data, DB_NEXT);
  632. }
  633. if (0 != db_rval) {
  634. if (DB_NOTFOUND != db_rval) {
  635. LDAPDebug(LDAP_DEBUG_ANY, "%s: Failed to read database, "
  636. "errno=%d (%s)\n", inst->inst_name, db_rval,
  637. dblayer_strerror(db_rval));
  638. if (job->task) {
  639. slapi_task_log_notice(job->task,
  640. "%s: Failed to read database, err %d (%s)",
  641. inst->inst_name, db_rval,
  642. dblayer_strerror(db_rval));
  643. }
  644. }
  645. break;
  646. }
  647. curr_entry++;
  648. temp_id = id_stored_to_internal((char *)key.data);
  649. free(key.data);
  650. /* call post-entry plugin */
  651. plugin_call_entryfetch_plugins((char **) &data.dptr, &data.dsize);
  652. e = slapi_str2entry(data.data, 0);
  653. if ( NULL == e ) {
  654. if (job->task) {
  655. slapi_task_log_notice(job->task,
  656. "%s: WARNING: skipping badly formatted entry (id %lu)",
  657. inst->inst_name, (u_long)temp_id);
  658. }
  659. LDAPDebug(LDAP_DEBUG_ANY,
  660. "%s: WARNING: skipping badly formatted entry (id %lu)\n",
  661. inst->inst_name, (u_long)temp_id, 0);
  662. continue;
  663. }
  664. free(data.data);
  665. /* generate uniqueid if necessary */
  666. import_generate_uniqueid(job, e);
  667. ep = import_make_backentry(e, temp_id);
  668. if (!ep)
  669. goto error;
  670. /* not sure what this does, but it looked like it could be
  671. * simplified. if it's broken, it's my fault. -robey
  672. */
  673. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  674. Slapi_Value **va = attr_get_present_values(attr);
  675. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  676. }
  677. if (job->flags & FLAG_ABORT)
  678. goto error;
  679. /* Now we have this new entry, all decoded
  680. * Next thing we need to do is:
  681. * (1) see if the appropriate fifo location contains an
  682. * entry which had been processed by the indexers.
  683. * If so, proceed.
  684. * If not, spin waiting for it to become free.
  685. * (2) free the old entry and store the new one there.
  686. * (3) Update the job progress indicators so the indexers
  687. * can use the new entry.
  688. */
  689. idx = id % job->fifo.size;
  690. old_ep = job->fifo.item[idx].entry;
  691. if (old_ep) {
  692. /* for the slot to be recycled, it needs to be already absorbed
  693. * by the foreman (id >= ready_ID), and all the workers need to
  694. * be finished with it (refcount = 0).
  695. */
  696. while (((old_ep->ep_refcnt > 0) ||
  697. (old_ep->ep_id >= job->ready_ID))
  698. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  699. info->state = WAITING;
  700. DS_Sleep(sleeptime);
  701. }
  702. if (job->flags & FLAG_ABORT)
  703. goto error;
  704. info->state = RUNNING;
  705. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  706. job->fifo.item[idx].entry = NULL;
  707. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  708. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  709. else
  710. job->fifo.c_bsize = 0;
  711. backentry_free(&old_ep);
  712. }
  713. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  714. if (newesize > job->fifo.bsize) { /* entry too big */
  715. char ebuf[BUFSIZ];
  716. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  717. escape_string(slapi_entry_get_dn(e), ebuf));
  718. import_log_notice(job, "REASON: entry too large (%d bytes) for "
  719. "the buffer size (%d bytes)", newesize, job->fifo.bsize);
  720. backentry_free(&ep);
  721. job->skipped++;
  722. continue;
  723. }
  724. /* Now check if fifo has enough space for the new entry */
  725. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  726. import_wait_for_space_in_fifo( job, newesize );
  727. }
  728. /* We have enough space */
  729. job->fifo.item[idx].filename = ID2ENTRY LDBM_FILENAME_SUFFIX;
  730. job->fifo.item[idx].line = curr_entry;
  731. job->fifo.item[idx].entry = ep;
  732. job->fifo.item[idx].bad = 0;
  733. job->fifo.item[idx].esize = newesize;
  734. /* Add the entry size to total fifo size */
  735. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  736. /* Update the job to show our progress */
  737. job->lead_ID = id;
  738. if ((id - info->first_ID) <= job->fifo.size) {
  739. job->trailing_ID = info->first_ID;
  740. } else {
  741. job->trailing_ID = id - job->fifo.size;
  742. }
  743. /* Update our progress meter too */
  744. info->last_ID_processed = id;
  745. id++;
  746. if (job->flags & FLAG_ABORT)
  747. goto error;
  748. if (info->command == STOP)
  749. {
  750. finished = 1;
  751. }
  752. }
  753. dbc->c_close(dbc);
  754. dblayer_release_aux_id2entry( be, db, env );
  755. info->state = FINISHED;
  756. return;
  757. error:
  758. dbc->c_close(dbc);
  759. dblayer_release_aux_id2entry( be, db, env );
  760. info->state = ABORTED;
  761. }
  762. static void
  763. import_wait_for_space_in_fifo(ImportJob *job, size_t new_esize)
  764. {
  765. struct backentry *temp_ep = NULL;
  766. size_t i;
  767. int slot_found;
  768. PRIntervalTime sleeptime;
  769. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  770. /* Now check if fifo has enough space for the new entry */
  771. while ((job->fifo.c_bsize + new_esize) > job->fifo.bsize) {
  772. for ( i = 0, slot_found = 0 ; i < job->fifo.size ; i++ ) {
  773. temp_ep = job->fifo.item[i].entry;
  774. if (temp_ep) {
  775. if (temp_ep->ep_refcnt == 0 && temp_ep->ep_id < job->ready_ID) {
  776. job->fifo.item[i].entry = NULL;
  777. if (job->fifo.c_bsize > job->fifo.item[i].esize)
  778. job->fifo.c_bsize -= job->fifo.item[i].esize;
  779. else
  780. job->fifo.c_bsize = 0;
  781. backentry_free(&temp_ep);
  782. slot_found = 1;
  783. }
  784. }
  785. }
  786. if ( slot_found == 0 )
  787. DS_Sleep(sleeptime);
  788. }
  789. }
  790. /* helper function for the foreman: */
  791. static int foreman_do_parentid(ImportJob *job, struct backentry *entry,
  792. struct attrinfo *parentid_ai)
  793. {
  794. backend *be = job->inst->inst_be;
  795. Slapi_Value **svals = NULL;
  796. Slapi_Attr *attr = NULL;
  797. int idl_disposition = 0;
  798. int ret = 0;
  799. if (slapi_entry_attr_find(entry->ep_entry, "parentid", &attr) == 0) {
  800. svals = attr_get_present_values(attr);
  801. ret = index_addordel_values_ext_sv(be, "parentid", svals, NULL, entry->ep_id,
  802. BE_INDEX_ADD, NULL, &idl_disposition, NULL);
  803. if (idl_disposition != IDL_INSERT_NORMAL) {
  804. char *attr_value = slapi_value_get_berval(svals[0])->bv_val;
  805. ID parent_id = atol(attr_value);
  806. if (idl_disposition == IDL_INSERT_NOW_ALLIDS) {
  807. import_subcount_mother_init(job->mothers, parent_id,
  808. idl_get_allidslimit(parentid_ai)+1);
  809. } else if (idl_disposition == IDL_INSERT_ALLIDS) {
  810. import_subcount_mother_count(job->mothers, parent_id);
  811. }
  812. }
  813. if (ret != 0) {
  814. import_log_notice(job, "ERROR: Can't update parentid index "
  815. "(error %d)", ret);
  816. return ret;
  817. }
  818. }
  819. return 0;
  820. }
  821. /* helper function for the foreman: */
  822. static int foreman_do_entrydn(ImportJob *job, FifoItem *fi)
  823. {
  824. backend *be = job->inst->inst_be;
  825. struct berval bv;
  826. int err = 0, ret = 0;
  827. IDList *IDL;
  828. /* insert into the entrydn index */
  829. bv.bv_val = (void*)backentry_get_ndn(fi->entry); /* jcm - Had to cast away const */
  830. bv.bv_len = strlen(bv.bv_val);
  831. /* We need to check here whether the DN is already present in
  832. * the entrydn index. If it is then the input ldif
  833. * contained a duplicate entry, which it isn't allowed to */
  834. /* Due to popular demand, we only warn on this, given the
  835. * tendency for customers to want to import dirty data */
  836. /* So, we do an index read first */
  837. err = 0;
  838. IDL = index_read(be, "entrydn", indextype_EQUALITY, &bv, NULL, &err);
  839. /* Did this work ? */
  840. if (NULL != IDL) {
  841. /* IMPOSTER ! Get thee hence... */
  842. import_log_notice(job, "WARNING: Skipping duplicate entry "
  843. "\"%s\" found at line %d of file \"%s\"",
  844. slapi_entry_get_dn(fi->entry->ep_entry),
  845. fi->line, fi->filename);
  846. idl_free(IDL);
  847. /* skip this one */
  848. fi->bad = 1;
  849. job->skipped++;
  850. return -1; /* skip to next entry */
  851. }
  852. if ((ret = index_addordel_string(be, "entrydn",
  853. bv.bv_val,
  854. fi->entry->ep_id,
  855. BE_INDEX_ADD|BE_INDEX_NORMALIZED, NULL)) != 0) {
  856. import_log_notice(job, "Error writing entrydn index "
  857. "(error %d: %s)",
  858. ret, dblayer_strerror(ret));
  859. return ret;
  860. }
  861. return 0;
  862. }
  863. /* foreman thread:
  864. * i go through the FIFO just like the other worker threads, but i'm
  865. * responsible for the interrelated indexes: entrydn, id2entry, and the
  866. * operational attributes (plus the parentid index).
  867. */
  868. void import_foreman(void *param)
  869. {
  870. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  871. ImportJob *job = info->job;
  872. ldbm_instance *inst = job->inst;
  873. backend *be = inst->inst_be;
  874. PRIntervalTime sleeptime;
  875. int finished = 0;
  876. ID id = info->first_ID;
  877. int ret = 0;
  878. struct attrinfo *parentid_ai;
  879. Slapi_PBlock *pb = slapi_pblock_new();
  880. int shift = 0;
  881. PR_ASSERT(info != NULL);
  882. PR_ASSERT(inst != NULL);
  883. if (job->flags & FLAG_ABORT) {
  884. goto error;
  885. }
  886. /* the pblock is used only by add_op_attrs */
  887. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  888. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  889. info->state = RUNNING;
  890. ainfo_get(be, "parentid", &parentid_ai);
  891. while (! finished) {
  892. FifoItem *fi = NULL;
  893. int parent_status = 0;
  894. if (job->flags & FLAG_ABORT) {
  895. goto error;
  896. }
  897. while ( ((info->command == PAUSE) || (id > job->lead_ID)) &&
  898. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  899. /* Check to see if we've been told to stop */
  900. info->state = WAITING;
  901. DS_Sleep(sleeptime);
  902. }
  903. if (info->command == STOP) {
  904. finished = 1;
  905. continue;
  906. }
  907. if (job->flags & FLAG_ABORT) {
  908. goto error;
  909. }
  910. info->state = RUNNING;
  911. /* Read that entry from the cache */
  912. fi = import_fifo_fetch(job, id, 0, shift);
  913. if (! fi) {
  914. import_log_notice(job, "WARNING: entry id %d is missing", id);
  915. shift++;
  916. continue;
  917. }
  918. /* first, fill in any operational attributes */
  919. /* add_op_attrs wants a pblock for some reason. */
  920. if (add_op_attrs(pb, inst->inst_li, fi->entry, &parent_status) != 0) {
  921. import_log_notice(job, "ERROR: Could not add op attrs to "
  922. "entry ending at line %d of file \"%s\"",
  923. fi->line, fi->filename);
  924. goto error;
  925. }
  926. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  927. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  928. /*
  929. * Only check for a parent and add to the entry2dn index if
  930. * the entry is not a tombstone.
  931. */
  932. if (job->flags & FLAG_ABORT) {
  933. goto error;
  934. }
  935. if (parent_status == IMPORT_ADD_OP_ATTRS_NO_PARENT) {
  936. /* If this entry is a suffix entry, this is not a problem */
  937. /* However, if it is not, this is an error---it means that
  938. * someone tried to import an entry before importing its parent
  939. * we reject the entry but carry on since we've not stored
  940. * anything related to this entry.
  941. */
  942. if (! slapi_be_issuffix(inst->inst_be, backentry_get_sdn(fi->entry))) {
  943. import_log_notice(job, "WARNING: Skipping entry \"%s\" "
  944. "which has no parent, ending at line %d "
  945. "of file \"%s\"",
  946. slapi_entry_get_dn(fi->entry->ep_entry),
  947. fi->line, fi->filename);
  948. /* skip this one */
  949. fi->bad = 1;
  950. job->skipped++;
  951. goto cont; /* below */
  952. }
  953. }
  954. if (job->flags & FLAG_ABORT) {
  955. goto error;
  956. }
  957. /* insert into the entrydn index */
  958. ret = foreman_do_entrydn(job, fi);
  959. if (ret == -1)
  960. goto cont; /* skip entry */
  961. if (ret != 0)
  962. goto error;
  963. }
  964. if (job->flags & FLAG_ABORT) {
  965. goto error;
  966. }
  967. if (!(job->flags & FLAG_REINDEXING))/* reindex reads data from id2entry */
  968. {
  969. /* insert into the id2entry index
  970. * (that isn't really an index -- it's the storehouse of the entries
  971. * themselves.)
  972. */
  973. if ((ret = id2entry_add_ext(be, fi->entry, NULL, job->encrypt)) != 0) {
  974. /* DB_RUNRECOVERY usually occurs if disk fills */
  975. if (LDBM_OS_ERR_IS_DISKFULL(ret)) {
  976. import_log_notice(job, "ERROR: OUT OF SPACE ON DISK or FILE TOO LARGE -- "
  977. "Could not store the entry ending at line "
  978. "%d of file \"%s\"",
  979. fi->line, fi->filename);
  980. } else if (ret == DB_RUNRECOVERY) {
  981. import_log_notice(job, "FATAL ERROR: (LARGEFILE SUPPORT NOT ENABLED? OUT OF SPACE ON DISK?) -- "
  982. "Could not store the entry ending at line "
  983. "%d of file \"%s\"",
  984. fi->line, fi->filename);
  985. } else {
  986. import_log_notice(job, "ERROR: Could not store the entry "
  987. "ending at line %d of file \"%s\" -- "
  988. "error %d", fi->line, fi->filename, ret);
  989. }
  990. goto error;
  991. }
  992. }
  993. if (job->flags & FLAG_ABORT) {
  994. goto error;
  995. }
  996. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  997. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  998. /* parentid index
  999. * (we have to do this here, because the parentID is dependent on
  1000. * looking up by entrydn.)
  1001. * Only add to the parent index if the entry is not a tombstone.
  1002. */
  1003. ret = foreman_do_parentid(job, fi->entry, parentid_ai);
  1004. if (ret != 0)
  1005. goto error;
  1006. /* Lastly, before we're finished with the entry, pass it to the
  1007. vlv code to see whether it's within the scope a VLV index. */
  1008. vlv_grok_new_import_entry(fi->entry, be);
  1009. }
  1010. if (job->flags & FLAG_ABORT) {
  1011. goto error;
  1012. }
  1013. /* Remove the entry from the cache (caused by id2entry_add) */
  1014. if (!(job->flags & FLAG_REINDEXING))/* reindex reads data from id2entry */
  1015. cache_remove(&inst->inst_cache, fi->entry);
  1016. fi->entry->ep_refcnt = job->number_indexers;
  1017. cont:
  1018. if (job->flags & FLAG_ABORT) {
  1019. goto error;
  1020. }
  1021. job->ready_ID = id;
  1022. info->last_ID_processed = id;
  1023. id++;
  1024. if (job->flags & FLAG_ABORT){
  1025. goto error;
  1026. }
  1027. }
  1028. slapi_pblock_destroy(pb);
  1029. info->state = FINISHED;
  1030. return;
  1031. error:
  1032. slapi_pblock_destroy(pb);
  1033. info->state = ABORTED;
  1034. }
  1035. /* worker thread:
  1036. * given an attribute, this worker plows through the entry FIFO, building
  1037. * up the attribute index.
  1038. */
  1039. void import_worker(void *param)
  1040. {
  1041. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  1042. ImportJob *job = info->job;
  1043. ldbm_instance *inst = job->inst;
  1044. backend *be = inst->inst_be;
  1045. PRIntervalTime sleeptime;
  1046. int finished = 0;
  1047. ID id = info->first_ID;
  1048. int ret = 0;
  1049. int idl_disposition = 0;
  1050. struct vlvIndex* vlv_index = NULL;
  1051. void *substring_key_buffer = NULL;
  1052. FifoItem *fi;
  1053. int is_objectclass_attribute;
  1054. int is_nsuniqueid_attribute;
  1055. void *attrlist_cursor;
  1056. PR_ASSERT(NULL != info);
  1057. PR_ASSERT(NULL != inst);
  1058. if (job->flags & FLAG_ABORT) {
  1059. goto error;
  1060. }
  1061. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1062. vlv_index = vlv_find_indexname(info->index_info->name, be);
  1063. if (NULL == vlv_index) {
  1064. goto error;
  1065. }
  1066. }
  1067. /*
  1068. * If the entry is a Tombstone, then we only add it to the nsuniqeid index
  1069. * and the idlist for (objectclass=tombstone). These two flags are just
  1070. * handy for working out what to do in this case.
  1071. */
  1072. is_objectclass_attribute =
  1073. (strcasecmp(info->index_info->name, "objectclass") == 0);
  1074. is_nsuniqueid_attribute =
  1075. (strcasecmp(info->index_info->name, SLAPI_ATTR_UNIQUEID) == 0);
  1076. if (1 != idl_get_idl_new()) {
  1077. /* Is there substring indexing going on here ? */
  1078. if ( (INDEX_SUB & info->index_info->ai->ai_indexmask) &&
  1079. (info->index_buffer_size > 0) ) {
  1080. /* Then make a key buffer thing */
  1081. ret = index_buffer_init(info->index_buffer_size, 0,
  1082. &substring_key_buffer);
  1083. if (0 != ret) {
  1084. import_log_notice(job, "IMPORT FAIL 1 (error %d)", ret);
  1085. }
  1086. }
  1087. }
  1088. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  1089. info->state = RUNNING;
  1090. info->last_ID_processed = id-1;
  1091. while (! finished) {
  1092. struct backentry *ep = NULL;
  1093. Slapi_Value **svals = NULL;
  1094. Slapi_Attr *attr = NULL;
  1095. if (job->flags & FLAG_ABORT) {
  1096. goto error;
  1097. }
  1098. /* entry can be NULL if it turned out to be bogus */
  1099. while (!finished && !ep) {
  1100. /* This worker thread must wait if the command flag is "PAUSE" or
  1101. * the entry corresponds to the current entry treated by the foreman
  1102. * thread, and the state is neither STOP nor ABORT
  1103. */
  1104. while (((info->command == PAUSE) || (id > job->ready_ID)) &&
  1105. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  1106. /* Check to see if we've been told to stop */
  1107. info->state = WAITING;
  1108. DS_Sleep(sleeptime);
  1109. }
  1110. if (info->command == STOP) {
  1111. finished = 1;
  1112. continue;
  1113. }
  1114. if (job->flags & FLAG_ABORT) {
  1115. goto error;
  1116. }
  1117. info->state = RUNNING;
  1118. /* Read that entry from the cache */
  1119. fi = import_fifo_fetch(job, id, 1, 0);
  1120. ep = fi ? fi->entry : NULL;
  1121. if (!ep) {
  1122. /* skipping an entry that turned out to be bad */
  1123. info->last_ID_processed = id;
  1124. id++;
  1125. }
  1126. }
  1127. if (finished)
  1128. continue;
  1129. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1130. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1131. /* This is not a tombstone entry. */
  1132. /* Is this a VLV index ? */
  1133. if (job->flags & FLAG_ABORT) {
  1134. goto error;
  1135. }
  1136. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1137. /* Yes, call VLV code -- needs pblock to find backend */
  1138. Slapi_PBlock *pb = slapi_pblock_new();
  1139. PR_ASSERT(NULL != vlv_index);
  1140. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  1141. vlv_update_index(vlv_index, NULL, inst->inst_li, pb, NULL, ep);
  1142. slapi_pblock_destroy(pb);
  1143. } else {
  1144. /* No, process regular index */
  1145. /* Look for the attribute we're indexing and its subtypes */
  1146. /* For each attr write to the index */
  1147. attrlist_cursor = NULL;
  1148. while ((attr = attrlist_find_ex(ep->ep_entry->e_attrs,
  1149. info->index_info->name,
  1150. NULL,
  1151. NULL,
  1152. &attrlist_cursor)) != NULL) {
  1153. if (job->flags & FLAG_ABORT) {
  1154. goto error;
  1155. }
  1156. if(valueset_isempty(&(attr->a_present_values))) continue;
  1157. svals = attr_get_present_values(attr);
  1158. ret = index_addordel_values_ext_sv(be, info->index_info->name,
  1159. svals, NULL, ep->ep_id, BE_INDEX_ADD | (job->encrypt ? 0 : BE_INDEX_DONT_ENCRYPT), NULL, &idl_disposition,
  1160. substring_key_buffer);
  1161. if (0 != ret) {
  1162. /* Something went wrong, eg disk filled up */
  1163. goto error;
  1164. }
  1165. }
  1166. }
  1167. } else {
  1168. /* This is a Tombstone entry... we only add it to the nsuniqeid
  1169. * index and the idlist for (objectclass=nstombstone).
  1170. */
  1171. if (job->flags & FLAG_ABORT) {
  1172. goto error;
  1173. }
  1174. if (is_nsuniqueid_attribute) {
  1175. ret = index_addordel_string(be, SLAPI_ATTR_UNIQUEID,
  1176. slapi_entry_get_uniqueid(ep->ep_entry), ep->ep_id,
  1177. BE_INDEX_ADD, NULL);
  1178. if (0 != ret) {
  1179. /* Something went wrong, eg disk filled up */
  1180. goto error;
  1181. }
  1182. }
  1183. if (is_objectclass_attribute) {
  1184. ret = index_addordel_string(be, SLAPI_ATTR_OBJECTCLASS,
  1185. SLAPI_ATTR_VALUE_TOMBSTONE, ep->ep_id, BE_INDEX_ADD, NULL);
  1186. if (0 != ret) {
  1187. /* Something went wrong, eg disk filled up */
  1188. goto error;
  1189. }
  1190. }
  1191. }
  1192. import_decref_entry(ep);
  1193. info->last_ID_processed = id;
  1194. id++;
  1195. if (job->flags & FLAG_ABORT) {
  1196. goto error;
  1197. }
  1198. }
  1199. if (job->flags & FLAG_ABORT) {
  1200. goto error;
  1201. }
  1202. /* If we were buffering index keys, now flush them */
  1203. if (substring_key_buffer) {
  1204. ret = index_buffer_flush(substring_key_buffer,
  1205. inst->inst_be, NULL,
  1206. info->index_info->ai);
  1207. if (0 != ret) {
  1208. goto error;
  1209. }
  1210. index_buffer_terminate(substring_key_buffer);
  1211. }
  1212. info->state = FINISHED;
  1213. return;
  1214. error:
  1215. if (ret == DB_RUNRECOVERY) {
  1216. LDAPDebug(LDAP_DEBUG_ANY,"cannot import; database recovery needed\n",
  1217. 0,0,0);
  1218. } else if (ret == DB_LOCK_DEADLOCK) {
  1219. /* can this occur? */
  1220. }
  1221. info->state = ABORTED;
  1222. }
  1223. /*
  1224. * import entries to a backend, over the wire -- entries will arrive
  1225. * asynchronously, so this method has no "producer" thread. instead, the
  1226. * front-end drops new entries in as they arrive.
  1227. *
  1228. * this is sometimes called "fast replica initialization".
  1229. *
  1230. * some of this code is duplicated from ldif2ldbm, but i don't think we
  1231. * can avoid it.
  1232. */
  1233. static int bulk_import_start(Slapi_PBlock *pb)
  1234. {
  1235. struct ldbminfo *li = NULL;
  1236. ImportJob *job = NULL;
  1237. backend *be = NULL;
  1238. PRThread *thread = NULL;
  1239. int ret = 0;
  1240. job = CALLOC(ImportJob);
  1241. if (job == NULL) {
  1242. LDAPDebug(LDAP_DEBUG_ANY, "not enough memory to do import job\n",
  1243. 0, 0, 0);
  1244. return -1;
  1245. }
  1246. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1247. PR_ASSERT(be != NULL);
  1248. li = (struct ldbminfo *)(be->be_database->plg_private);
  1249. job->inst = (ldbm_instance *)be->be_instance_info;
  1250. /* check if an import/restore is already ongoing... */
  1251. PR_Lock(job->inst->inst_config_mutex);
  1252. if (job->inst->inst_flags & INST_FLAG_BUSY) {
  1253. PR_Unlock(job->inst->inst_config_mutex);
  1254. LDAPDebug(LDAP_DEBUG_ANY, "ldbm: '%s' is already in the middle of "
  1255. "another task and cannot be disturbed.\n",
  1256. job->inst->inst_name, 0, 0);
  1257. FREE(job);
  1258. return SLAPI_BI_ERR_BUSY;
  1259. }
  1260. job->inst->inst_flags |= INST_FLAG_BUSY;
  1261. PR_Unlock(job->inst->inst_config_mutex);
  1262. /* take backend offline */
  1263. slapi_mtn_be_disable(be);
  1264. /* get uniqueid info */
  1265. slapi_pblock_get(pb, SLAPI_LDIF2DB_GENERATE_UNIQUEID, &job->uuid_gen_type);
  1266. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  1267. char *namespaceid;
  1268. slapi_pblock_get(pb, SLAPI_LDIF2DB_NAMESPACEID, &namespaceid);
  1269. job->uuid_namespace = slapi_ch_strdup(namespaceid);
  1270. }
  1271. job->flags = 0; /* don't use files */
  1272. job->flags |= FLAG_INDEX_ATTRS;
  1273. job->flags |= FLAG_ONLINE;
  1274. job->starting_ID = 1;
  1275. job->first_ID = 1;
  1276. job->mothers = CALLOC(import_subcount_stuff);
  1277. /* how much space should we allocate to index buffering? */
  1278. job->job_index_buffer_size = import_get_index_buffer_size();
  1279. if (job->job_index_buffer_size == 0) {
  1280. /* 10% of the allocated cache size + one meg */
  1281. job->job_index_buffer_size = (job->inst->inst_li->li_dbcachesize/10) +
  1282. (1024*1024);
  1283. }
  1284. import_subcount_stuff_init(job->mothers);
  1285. job->wire_lock = PR_NewLock();
  1286. job->wire_cv = PR_NewCondVar(job->wire_lock);
  1287. /* COPIED from ldif2ldbm.c : */
  1288. /* shutdown this instance of the db */
  1289. cache_clear(&job->inst->inst_cache);
  1290. dblayer_instance_close(be);
  1291. /* Delete old database files */
  1292. dblayer_delete_instance_dir(be);
  1293. /* it's okay to fail -- it might already be gone */
  1294. /* dblayer_instance_start will init the id2entry index. */
  1295. /* it also (finally) fills in inst_dir_name */
  1296. ret = dblayer_instance_start(be, DBLAYER_IMPORT_MODE);
  1297. if (ret != 0)
  1298. goto fail;
  1299. /* END OF COPIED SECTION */
  1300. PR_Lock(job->wire_lock);
  1301. vlv_init(job->inst);
  1302. /* create thread for import_main, so we can return */
  1303. thread = PR_CreateThread(PR_USER_THREAD, import_main, (void *)job,
  1304. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1305. PR_JOINABLE_THREAD,
  1306. SLAPD_DEFAULT_THREAD_STACKSIZE);
  1307. if (thread == NULL) {
  1308. PRErrorCode prerr = PR_GetError();
  1309. LDAPDebug(LDAP_DEBUG_ANY, "unable to spawn import thread, "
  1310. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1311. prerr, slapd_pr_strerror(prerr), 0);
  1312. PR_Unlock(job->wire_lock);
  1313. ret = -2;
  1314. goto fail;
  1315. }
  1316. job->main_thread = thread;
  1317. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1318. li->li_bulk_import_handle, job);
  1319. /* wait for the import_main to signal that it's ready for entries */
  1320. /* (don't want to send the success code back to the LDAP client until
  1321. * we're ready for the adds to start rolling in)
  1322. */
  1323. PR_WaitCondVar(job->wire_cv, PR_INTERVAL_NO_TIMEOUT);
  1324. PR_Unlock(job->wire_lock);
  1325. return 0;
  1326. fail:
  1327. PR_Lock(job->inst->inst_config_mutex);
  1328. job->inst->inst_flags &= ~INST_FLAG_BUSY;
  1329. PR_Unlock(job->inst->inst_config_mutex);
  1330. import_free_job(job);
  1331. FREE(job);
  1332. return ret;
  1333. }
  1334. /* returns 0 on success, or < 0 on error
  1335. *
  1336. * on error, the import process is aborted -- so if this returns an error,
  1337. * don't try to queue any more entries or you'll be sorry.
  1338. *
  1339. * flag_block in used to know if this thread should block when
  1340. * the fifo is full or return an error LDAP_BUSY
  1341. * Typically, import done on from the GUI or the command line will
  1342. * block while online import as used by the replication total update
  1343. * will not block
  1344. */
  1345. static int bulk_import_queue(ImportJob *job, Slapi_Entry *entry, int flag_block)
  1346. {
  1347. struct backentry *ep = NULL, *old_ep = NULL;
  1348. int idx;
  1349. ID id = 0;
  1350. Slapi_Attr *attr = NULL;
  1351. size_t newesize = 0;
  1352. PR_Lock(job->wire_lock);
  1353. /* Let's do this inside the lock !*/
  1354. id = job->lead_ID + 1;
  1355. /* generate uniqueid if necessary */
  1356. import_generate_uniqueid(job, entry);
  1357. /* make into backentry */
  1358. ep = import_make_backentry(entry, id);
  1359. if (!ep) {
  1360. import_abort_all(job, 1);
  1361. PR_Unlock(job->wire_lock);
  1362. return -1;
  1363. }
  1364. /* encode the password */
  1365. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  1366. Slapi_Value **va = attr_get_present_values(attr);
  1367. pw_encodevals( (Slapi_Value **)va ); /* jcm - had to cast away const */
  1368. }
  1369. /* Now we have this new entry, all decoded
  1370. * Next thing we need to do is:
  1371. * (1) see if the appropriate fifo location contains an
  1372. * entry which had been processed by the indexers.
  1373. * If so, proceed.
  1374. * If not, spin waiting for it to become free.
  1375. * (2) free the old entry and store the new one there.
  1376. * (3) Update the job progress indicators so the indexers
  1377. * can use the new entry.
  1378. */
  1379. idx = id % job->fifo.size;
  1380. old_ep = job->fifo.item[idx].entry;
  1381. if (old_ep) {
  1382. while ((old_ep->ep_refcnt > 0) && !(job->flags & FLAG_ABORT))
  1383. {
  1384. if (flag_block)
  1385. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1386. else
  1387. {
  1388. /* DBBD: Argh -- why not just block, what's the benefit to this ?? */
  1389. /* I think that to support pipelining in the transport, we need to block here, */
  1390. /* Otherwise evil things could happen where we say we're busy for operation N, but */
  1391. /* Not for operation N+1, but the sender doesn't find out about this until after sending */
  1392. /* Operation N+2 etc. Seems possible to end up with children processed before parents which won't work. */
  1393. PR_Unlock(job->wire_lock);
  1394. return LDAP_BUSY;
  1395. }
  1396. }
  1397. /* the producer could be running thru the fifo while
  1398. * everyone else is cycling to a new pass...
  1399. * double-check that this entry is < ready_ID
  1400. */
  1401. while ((old_ep->ep_id >= job->ready_ID) && !(job->flags & FLAG_ABORT))
  1402. {
  1403. if (flag_block)
  1404. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1405. else
  1406. {
  1407. PR_Unlock(job->wire_lock);
  1408. return LDAP_BUSY;
  1409. }
  1410. }
  1411. if (job->flags & FLAG_ABORT) {
  1412. PR_Unlock(job->wire_lock);
  1413. return -2;
  1414. }
  1415. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  1416. job->fifo.item[idx].entry = NULL;
  1417. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  1418. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  1419. else
  1420. job->fifo.c_bsize = 0;
  1421. backentry_free(&old_ep);
  1422. }
  1423. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  1424. if (newesize > job->fifo.bsize) { /* entry too big */
  1425. char ebuf[BUFSIZ];
  1426. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  1427. escape_string(slapi_entry_get_dn(ep->ep_entry), ebuf));
  1428. import_log_notice(job, "REASON: entry too large (%d bytes) for "
  1429. "the buffer size (%d bytes)", newesize, job->fifo.bsize);
  1430. backentry_free(&ep);
  1431. PR_Unlock(job->wire_lock);
  1432. return -1;
  1433. }
  1434. /* Now check if fifo has enough space for the new entry */
  1435. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  1436. import_wait_for_space_in_fifo( job, newesize );
  1437. }
  1438. /* We have enough space */
  1439. job->fifo.item[idx].filename = "(bulk import)";
  1440. job->fifo.item[idx].line = 0;
  1441. job->fifo.item[idx].entry = ep;
  1442. job->fifo.item[idx].bad = 0;
  1443. job->fifo.item[idx].esize = newesize;
  1444. /* Add the entry size to total fifo size */
  1445. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  1446. /* Update the job to show our progress */
  1447. job->lead_ID = id;
  1448. if ((id - job->starting_ID) <= job->fifo.size) {
  1449. job->trailing_ID = job->starting_ID;
  1450. } else {
  1451. job->trailing_ID = id - job->fifo.size;
  1452. }
  1453. PR_Unlock(job->wire_lock);
  1454. return 0;
  1455. }
  1456. void *factory_constructor(void *object, void *parent)
  1457. {
  1458. return NULL;
  1459. }
  1460. void factory_destructor(void *extension, void *object, void *parent)
  1461. {
  1462. ImportJob *job = (ImportJob *)extension;
  1463. PRThread *thread;
  1464. if (extension == NULL)
  1465. return;
  1466. /* connection was destroyed while we were still storing the extension --
  1467. * this is bad news and means we have a bulk import that needs to be
  1468. * aborted!
  1469. */
  1470. thread = job->main_thread;
  1471. LDAPDebug(LDAP_DEBUG_ANY, "ERROR bulk import abandoned\n",
  1472. 0, 0, 0);
  1473. import_abort_all(job, 1);
  1474. /* wait for import_main to finish... */
  1475. PR_JoinThread(thread);
  1476. /* extension object is free'd by import_main */
  1477. return;
  1478. }
  1479. /* plugin entry function for replica init */
  1480. int ldbm_back_wire_import(Slapi_PBlock *pb)
  1481. {
  1482. struct ldbminfo *li;
  1483. backend *be = NULL;
  1484. ImportJob *job;
  1485. PRThread *thread;
  1486. int state;
  1487. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1488. PR_ASSERT(be != NULL);
  1489. li = (struct ldbminfo *)(be->be_database->plg_private);
  1490. slapi_pblock_get(pb, SLAPI_BULK_IMPORT_STATE, &state);
  1491. if (state == SLAPI_BI_STATE_START) {
  1492. /* starting a new import */
  1493. return bulk_import_start(pb);
  1494. }
  1495. PR_ASSERT(pb->pb_conn != NULL);
  1496. if (pb->pb_conn != NULL) {
  1497. job = (ImportJob *)slapi_get_object_extension(li->li_bulk_import_object, pb->pb_conn, li->li_bulk_import_handle);
  1498. }
  1499. if ((job == NULL) || (pb->pb_conn == NULL)) {
  1500. /* import might be aborting */
  1501. return -1;
  1502. }
  1503. if (state == SLAPI_BI_STATE_ADD) {
  1504. /* continuing previous import */
  1505. if (! import_entry_belongs_here(pb->pb_import_entry,
  1506. job->inst->inst_be)) {
  1507. /* silently skip */
  1508. return 0;
  1509. }
  1510. /* These days, we don't want to return LDAP_BUSY (it makes pipelineing impossible
  1511. and actually doesn't achieve anything anyway). So we pass '1' for the block flag. */
  1512. return bulk_import_queue(job, pb->pb_import_entry,
  1513. 1);
  1514. }
  1515. thread = job->main_thread;
  1516. if (state == SLAPI_BI_STATE_DONE) {
  1517. /* finished with an import */
  1518. job->flags |= FLAG_PRODUCER_DONE;
  1519. /* "job" struct may vanish at any moment after we set the DONE
  1520. * flag, so keep a copy of the thread id in 'thread' for safekeeping.
  1521. */
  1522. /* wait for import_main to finish... */
  1523. PR_JoinThread(thread);
  1524. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1525. li->li_bulk_import_handle, NULL);
  1526. return 0;
  1527. }
  1528. /* ??? unknown state */
  1529. LDAPDebug(LDAP_DEBUG_ANY,
  1530. "ERROR: ldbm_back_wire_import: unknown state %d\n",
  1531. state, 0, 0);
  1532. return -1;
  1533. }
  1534. /*
  1535. * backup index configuration
  1536. * this function is called from dblayer_backup (ldbm2archive)
  1537. * [547427] index config must not change between backup and restore
  1538. */
  1539. #define DSE_INDEX "dse_index.ldif"
  1540. #define DSE_INSTANCE "dse_instance.ldif"
  1541. #define DSE_INDEX_FILTER "(objectclass=nsIndex)"
  1542. #define DSE_INSTANCE_FILTER "(objectclass=nsBackendInstance)"
  1543. static int
  1544. dse_conf_backup_core(struct ldbminfo *li, char *dest_dir, char *file_name, char *filter)
  1545. {
  1546. Slapi_PBlock *srch_pb = NULL;
  1547. Slapi_Entry **entries = NULL;
  1548. Slapi_Entry **ep = NULL;
  1549. Slapi_Attr *attr = NULL;
  1550. char *attr_name;
  1551. char *filename = NULL;
  1552. PRFileDesc *prfd = NULL;
  1553. int rval = 0;
  1554. int dlen = 0;
  1555. PRInt32 prrval;
  1556. char tmpbuf[BUFSIZ];
  1557. char *tp = NULL;
  1558. dlen = strlen(dest_dir);
  1559. if (0 == dlen)
  1560. {
  1561. filename = file_name;
  1562. }
  1563. else
  1564. {
  1565. filename = slapi_ch_smprintf("%s/%s", dest_dir, file_name);
  1566. }
  1567. LDAPDebug(LDAP_DEBUG_TRACE, "dse_conf_backup(%s): backup file %s\n",
  1568. filter, filename, 0);
  1569. /* Open the file to write */
  1570. if ((prfd = PR_Open(filename, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE,
  1571. SLAPD_DEFAULT_FILE_MODE)) == NULL)
  1572. {
  1573. LDAPDebug(LDAP_DEBUG_ANY,
  1574. "dse_conf_backup(%s): open %s failed: (%s)\n",
  1575. filter, filename, slapd_pr_strerror(PR_GetError()));
  1576. rval = -1;
  1577. goto out;
  1578. }
  1579. srch_pb = slapi_pblock_new();
  1580. slapi_search_internal_set_pb(srch_pb, li->li_plugin->plg_dn,
  1581. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  1582. slapi_search_internal_pb(srch_pb);
  1583. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1584. for (ep = entries; ep != NULL && *ep != NULL; ep++)
  1585. {
  1586. size_t l = strlen(slapi_entry_get_dn_const(*ep)) + 5 /* "dn: \n" */;
  1587. LDAPDebug(LDAP_DEBUG_TRACE, "\ndn: %s\n",
  1588. slapi_entry_get_dn_const(*ep), 0, 0);
  1589. if (l <= BUFSIZ)
  1590. tp = tmpbuf;
  1591. else
  1592. tp = (char *)slapi_ch_malloc(l); /* should be very rare ... */
  1593. sprintf(tp, "dn: %s\n", slapi_entry_get_dn_const(*ep));
  1594. prrval = PR_Write(prfd, tp, l);
  1595. if ((size_t)prrval != l)
  1596. {
  1597. LDAPDebug(LDAP_DEBUG_ANY,
  1598. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1599. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1600. rval = -1;
  1601. if (l > BUFSIZ)
  1602. slapi_ch_free_string(&tp);
  1603. goto out;
  1604. }
  1605. if (l > BUFSIZ)
  1606. slapi_ch_free_string(&tp);
  1607. for (slapi_entry_first_attr(*ep, &attr); attr;
  1608. slapi_entry_next_attr(*ep, attr, &attr))
  1609. {
  1610. int i;
  1611. Slapi_Value *sval = NULL;
  1612. const struct berval *attr_val;
  1613. int attr_name_len;
  1614. slapi_attr_get_type(attr, &attr_name);
  1615. /* numsubordinates should not be backed up */
  1616. if (!strcasecmp("numsubordinates", attr_name))
  1617. continue;
  1618. attr_name_len = strlen(attr_name);
  1619. for (i = slapi_attr_first_value(attr, &sval); i != -1;
  1620. i = slapi_attr_next_value(attr, i, &sval))
  1621. {
  1622. attr_val = slapi_value_get_berval(sval);
  1623. l = strlen(attr_val->bv_val) + attr_name_len + 3; /* : \n" */
  1624. LDAPDebug(LDAP_DEBUG_TRACE, "%s: %s\n", attr_name,
  1625. attr_val->bv_val, 0);
  1626. if (l <= BUFSIZ)
  1627. tp = tmpbuf;
  1628. else
  1629. tp = (char *)slapi_ch_malloc(l);
  1630. sprintf(tp, "%s: %s\n", attr_name, attr_val->bv_val);
  1631. prrval = PR_Write(prfd, tp, l);
  1632. if ((size_t)prrval != l)
  1633. {
  1634. LDAPDebug(LDAP_DEBUG_ANY,
  1635. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1636. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1637. rval = -1;
  1638. if (l > BUFSIZ)
  1639. slapi_ch_free_string(&tp);
  1640. goto out;
  1641. }
  1642. if (l > BUFSIZ)
  1643. slapi_ch_free_string(&tp);
  1644. }
  1645. }
  1646. if (ep+1 != NULL && *(ep+1) != NULL)
  1647. {
  1648. prrval = PR_Write(prfd, "\n", 1);
  1649. if ((int)prrval != 1)
  1650. {
  1651. LDAPDebug(LDAP_DEBUG_ANY,
  1652. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1653. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1654. rval = -1;
  1655. goto out;
  1656. }
  1657. }
  1658. }
  1659. out:
  1660. slapi_free_search_results_internal(srch_pb);
  1661. if (srch_pb)
  1662. {
  1663. slapi_pblock_destroy(srch_pb);
  1664. }
  1665. if (0 != dlen)
  1666. {
  1667. slapi_ch_free_string(&filename);
  1668. }
  1669. if (prfd)
  1670. {
  1671. prrval = PR_Close(prfd);
  1672. if (PR_SUCCESS != prrval)
  1673. {
  1674. LDAPDebug( LDAP_DEBUG_ANY,
  1675. "Fatal Error---Failed to back up dse indexes %d (%s)\n",
  1676. PR_GetError(), slapd_pr_strerror(PR_GetError()), 0);
  1677. rval = -1;
  1678. }
  1679. }
  1680. return rval;
  1681. }
  1682. int
  1683. dse_conf_backup(struct ldbminfo *li, char *dest_dir)
  1684. {
  1685. int rval = 0;
  1686. rval = dse_conf_backup_core(li, dest_dir, DSE_INSTANCE, DSE_INSTANCE_FILTER);
  1687. rval += dse_conf_backup_core(li, dest_dir, DSE_INDEX, DSE_INDEX_FILTER);
  1688. return rval;
  1689. }
  1690. /*
  1691. * read the backed up index configuration
  1692. * adjust them if the current configuration is different from it.
  1693. * this function is called from dblayer_restore (archive2ldbm)
  1694. * these functions are placed here to borrow import_get_entry
  1695. * [547427] index config must not change between backup and restore
  1696. */
  1697. int
  1698. dse_conf_verify_core(struct ldbminfo *li, char *src_dir, char *file_name, char *filter, char *log_str, char *entry_filter)
  1699. {
  1700. char *filename = NULL;
  1701. int rval = 0;
  1702. ldif_context c;
  1703. int fd = -1;
  1704. int curr_lineno = 0;
  1705. int finished = 0;
  1706. int backup_entry_len = 256;
  1707. char *search_scope = NULL;
  1708. Slapi_Entry **backup_entries = NULL;
  1709. Slapi_Entry **bep = NULL;
  1710. Slapi_Entry **curr_entries = NULL;
  1711. Slapi_PBlock srch_pb;
  1712. filename = slapi_ch_smprintf("%s/%s", src_dir, file_name);
  1713. if (PR_SUCCESS != PR_Access(filename, PR_ACCESS_READ_OK))
  1714. {
  1715. LDAPDebug(LDAP_DEBUG_ANY,
  1716. "Warning: config backup file %s not found in backup\n",
  1717. file_name, 0, 0);
  1718. rval = 0;
  1719. goto out;
  1720. }
  1721. fd = dblayer_open_huge_file(filename, O_RDONLY, 0);
  1722. if (fd < 0)
  1723. {
  1724. LDAPDebug(LDAP_DEBUG_ANY,
  1725. "Warning: can't open config backup file: %s\n", filename, 0, 0);
  1726. rval = -1;
  1727. goto out;
  1728. }
  1729. import_init_ldif(&c);
  1730. bep = backup_entries = (Slapi_Entry **)slapi_ch_calloc(1,
  1731. backup_entry_len * sizeof(Slapi_Entry *));
  1732. while (!finished)
  1733. {
  1734. char *estr = NULL;
  1735. Slapi_Entry *e = NULL;
  1736. estr = import_get_entry(&c, fd, &curr_lineno);
  1737. if (!estr)
  1738. break;
  1739. if (entry_filter != NULL) /* Single instance restoration */
  1740. {
  1741. if (!(int)strstr(estr, entry_filter))
  1742. continue;
  1743. }
  1744. e = slapi_str2entry(estr, 0);
  1745. slapi_ch_free_string(&estr);
  1746. if (!e) {
  1747. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: skipping bad LDIF entry "
  1748. "ending line %d of file \"%s\"", curr_lineno, filename, 0);
  1749. continue;
  1750. }
  1751. if (bep - backup_entries >= backup_entry_len)
  1752. {
  1753. backup_entries = (Slapi_Entry **)slapi_ch_realloc((char *)backup_entries,
  1754. 2 * backup_entry_len * sizeof(Slapi_Entry *));
  1755. bep = backup_entries + backup_entry_len;
  1756. backup_entry_len *= 2;
  1757. }
  1758. *bep = e;
  1759. bep++;
  1760. }
  1761. /* 623986: terminate the list if we reallocated backup_entries */
  1762. if (backup_entry_len > 256)
  1763. *bep = NULL;
  1764. pblock_init(&srch_pb);
  1765. if (entry_filter != NULL)
  1766. { /* Single instance restoration */
  1767. search_scope = slapi_ch_smprintf("%s,%s", entry_filter, li->li_plugin->plg_dn);
  1768. } else { /* Normal restoration */
  1769. search_scope = slapi_ch_strdup(li->li_plugin->plg_dn);
  1770. }
  1771. slapi_search_internal_set_pb(&srch_pb, search_scope,
  1772. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  1773. slapi_search_internal_pb(&srch_pb);
  1774. slapi_pblock_get(&srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &curr_entries);
  1775. if (0 != slapi_entries_diff(backup_entries, curr_entries, 1 /* test_all */,
  1776. log_str, 1 /* force_update */, li->li_identity))
  1777. {
  1778. LDAPDebug(LDAP_DEBUG_ANY, "WARNING!!: current %s is "
  1779. "different from backed up configuration; "
  1780. "The backup is restored.\n", log_str, 0, 0);
  1781. }
  1782. slapi_free_search_results_internal(&srch_pb);
  1783. pblock_done(&srch_pb);
  1784. import_free_ldif(&c);
  1785. out:
  1786. for (bep = backup_entries; bep && *bep; bep++)
  1787. slapi_entry_free(*bep);
  1788. slapi_ch_free((void **)&backup_entries);
  1789. slapi_ch_free_string(&filename);
  1790. slapi_ch_free_string(&search_scope);
  1791. if (fd > 0)
  1792. close(fd);
  1793. return rval;
  1794. }
  1795. int
  1796. dse_conf_verify(struct ldbminfo *li, char *src_dir, char *bename)
  1797. {
  1798. int rval;
  1799. char *entry_filter = NULL;
  1800. char *instance_entry_filter = NULL;
  1801. if (bename != NULL) /* This was a restore of a single backend */
  1802. {
  1803. /* Entry filter string */
  1804. entry_filter = slapi_ch_smprintf("cn=%s", bename);
  1805. /* Instance search filter */
  1806. instance_entry_filter = slapi_ch_smprintf("(&%s(cn=%s))", DSE_INSTANCE_FILTER, bename);
  1807. } else {
  1808. instance_entry_filter = slapi_ch_strdup(DSE_INSTANCE_FILTER);
  1809. }
  1810. rval = dse_conf_verify_core(li, src_dir, DSE_INSTANCE, instance_entry_filter,
  1811. "Instance Config", entry_filter);
  1812. rval += dse_conf_verify_core(li, src_dir, DSE_INDEX, DSE_INDEX_FILTER,
  1813. "Index Config", entry_filter);
  1814. slapi_ch_free_string(&entry_filter);
  1815. slapi_ch_free_string(&instance_entry_filter);
  1816. return rval;
  1817. }