11 #include <sys/param.h>
13 #include "ldapconfig.h"
15 #include "../back-bdb2/back-bdb2.h"
18 #define INDEXCMD "ldif2index-bdb2"
19 #define ID2ENTRYCMD "ldif2id2entry-bdb2"
20 #define ID2CHILDRENCMD "ldif2id2children-bdb2"
23 static void fork_child( char *prog, char *args[] );
24 static void wait4kids( int nkidval );
26 static char *tailorfile;
27 static char *inputfile;
28 static int maxkids = 1;
34 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
39 main( int argc, char **argv )
42 char *linep, *buf, *sbindir;
44 char buf2[20], buf3[20];
55 struct berval *vals[2];
56 Avlnode *avltypes = NULL;
58 sbindir = DEFAULT_SBINDIR;
59 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
61 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
63 case 'd': /* turn on debugging */
64 ldap_debug = atoi( optarg );
67 case 's': /* alternate sbindir (index cmd location) */
68 case 'e': /* accept -e for backwards compatibility */
69 sbindir = strdup( optarg );
72 case 'f': /* specify a tailor file */
73 tailorfile = strdup( optarg );
76 case 'i': /* input file */
77 inputfile = strdup( optarg );
80 case 'j': /* number of parallel index procs */
81 maxkids = atoi( optarg );
84 case 'n': /* which config file db to index */
85 dbnum = atoi( optarg ) - 1;
93 if ( inputfile == NULL ) {
96 if ( freopen( inputfile, "r", stdin ) == NULL ) {
103 * initialize stuff and figure out which backend we're dealing with
106 rc = slap_init(SLAP_TOOL_MODE, "ldif2ldbm");
108 fprintf( stderr, "ldif2ldbm: slap_init failed!\n");
112 read_config( tailorfile );
115 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
116 if ( strcasecmp( backends[dbnum].be_type, "bdb2" )
121 if ( dbnum == nbackends ) {
122 fprintf( stderr, "No bdb2 database found in config file\n" );
125 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
126 fprintf( stderr, "Database number selected via -n is out of range\n" );
127 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
129 } else if ( strcasecmp( backends[dbnum].be_type, "bdb2" ) != 0 ) {
130 fprintf( stderr, "Database number %d selected via -n is not an bdb2 database\n", dbnum );
136 be = &backends[dbnum];
138 /* disable write sync'ing */
139 li = (struct ldbminfo *) be->be_private;
140 li->li_dbcachewsync = 0;
143 * generate the id2entry index
147 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
150 args[i++] = inputfile;
152 args[i++] = tailorfile;
154 sprintf( buf2, "%d", dbnum+1 );
157 sprintf( buf3, "%d", ldap_debug );
162 fork_child( cmd, args );
165 * generate the dn2id and id2children indexes
169 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
172 args[i++] = inputfile;
174 args[i++] = tailorfile;
176 sprintf( buf2, "%d", dbnum+1 );
179 sprintf( buf3, "%d", ldap_debug );
184 fork_child( cmd, args );
187 * generate the attribute indexes
191 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
194 args[i++] = inputfile;
196 args[i++] = tailorfile;
198 sprintf( buf2, "%d", dbnum+1 );
201 sprintf( buf3, "%d", ldap_debug );
205 args[i++] = NULL; /* will hold the attribute name */
216 char *type, *val, *s;
217 int vlen, indexmask, syntaxmask;
219 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
223 len = strlen( line );
224 while ( lcur + len + 1 > lmax ) {
226 buf = (char *) ch_realloc( buf, lmax );
228 strcpy( buf + lcur, line );
233 if ( line[0] == '\n' || stop && buf && *buf ) {
237 while ( (linep = ldif_getline( &s )) != NULL ) {
239 if ( ldif_parse_line( linep, &type, &val, &vlen )
241 Debug( LDAP_DEBUG_PARSE,
242 "bad line %d in entry ending at line %d ignored\n",
243 elineno, lineno, 0 );
247 if ( !isascii( *type ) || isdigit( *type ) )
250 type = strdup( type );
251 if ( avl_insert( &avltypes, type, strcasecmp,
252 avl_dup_error ) != 0 ) {
255 bdb2i_attr_masks( be->be_private, type,
256 &indexmask, &syntaxmask );
259 fork_child( cmd, args );
268 slap_shutdown(dbnum);
278 fork_child( char *prog, char *args[] )
282 wait4kids( maxkids );
284 switch ( pid = fork() ) {
286 execvp( prog, args );
287 fprintf( stderr, "%s: ", prog );
292 case -1: /* trouble */
293 fprintf( stderr, "Could not fork to run %s\n", prog );
297 default: /* parent */
304 wait4kids( int nkidval )
309 while ( nkids >= nkidval ) {
311 p = (unsigned char *) &status;
312 if ( p[sizeof(int) - 1] == 0177 ) {
314 "stopping: child stopped with signal %d\n",
315 p[sizeof(int) - 2] );
316 } else if ( p[sizeof(int) - 1] != 0 ) {
318 "stopping: child terminated with signal %d\n",
319 p[sizeof(int) - 1] );
320 exit( p[sizeof(int) - 1] );
321 } else if ( p[sizeof(int) - 2] != 0 ) {
323 "stopping: child exited with status %d\n",
324 p[sizeof(int) - 2] );
325 exit( p[sizeof(int) - 2] );