11 #ifdef HAVE_SYS_PARAM_H
12 #include <sys/param.h>
15 #include "ldap_defaults.h"
17 #include "../back-ldbm/back-ldbm.h"
20 #define INDEXCMD "ldif2index" EXEEXT
21 #define ID2ENTRYCMD "ldif2id2entry" EXEEXT
22 #define ID2CHILDRENCMD "ldif2id2children" EXEEXT
26 static void fork_child( char *prog, char *args[] );
27 static void wait4kids( int nkidval );
29 static char *tailorfile;
30 static char *inputfile;
31 static int maxkids = 1;
37 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
42 main( int argc, char **argv )
45 char *linep, *buf, *sbindir;
47 char buf2[20], buf3[20];
59 struct berval *vals[2];
60 Avlnode *avltypes = NULL;
62 ldbm_ignore_nextid_file = 1;
64 sbindir = LDAP_SBINDIR;
65 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
67 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
69 case 'd': /* turn on debugging */
70 ldap_debug = atoi( optarg );
73 case 's': /* alternate sbindir (index cmd location) */
74 case 'e': /* accept -e for backwards compatibility */
75 sbindir = strdup( optarg );
78 case 'f': /* specify a tailor file */
79 tailorfile = strdup( optarg );
82 case 'i': /* input file */
83 inputfile = strdup( optarg );
86 case 'j': /* number of parallel index procs */
87 cmdkids = atoi( optarg );
90 case 'n': /* which config file db to index */
91 dbnum = atoi( optarg ) - 1;
99 if ( inputfile == NULL ) {
102 if ( freopen( inputfile, "r", stdin ) == NULL ) {
109 * initialize stuff and figure out which backend we're dealing with
112 rc = slap_init(SLAP_TOOL_MODE, "ldif2ldbm");
114 fprintf( stderr, "ldif2ldbm: slap_init failed!\n");
118 read_config( tailorfile );
121 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
122 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
127 if ( dbnum == nbackends ) {
128 fprintf( stderr, "No ldbm database found in config file\n" );
131 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
132 fprintf( stderr, "Database number selected via -n is out of range\n" );
133 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
135 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
136 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
142 be = &backends[dbnum];
144 /* disable write sync'ing */
145 li = (struct ldbminfo *) be->be_private;
146 li->li_dbcachewsync = 0;
149 * generate the id2entry index
153 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
156 args[i++] = inputfile;
158 args[i++] = tailorfile;
160 sprintf( buf2, "%d", dbnum+1 );
163 sprintf( buf3, "%d", ldap_debug );
168 fork_child( cmd, args );
171 * generate the dn2id and id2children indexes
175 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
178 args[i++] = inputfile;
180 args[i++] = tailorfile;
182 sprintf( buf2, "%d", dbnum+1 );
185 sprintf( buf3, "%d", ldap_debug );
190 fork_child( cmd, args );
195 * generate the attribute indexes
199 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
202 args[i++] = inputfile;
204 args[i++] = tailorfile;
206 sprintf( buf2, "%d", dbnum+1 );
209 sprintf( buf3, "%d", ldap_debug );
213 args[i++] = NULL; /* will hold the attribute name */
224 char *type, *val, *s;
225 int vlen, indexmask, syntaxmask;
227 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
231 len = strlen( line );
232 while ( lcur + len + 1 > lmax ) {
234 buf = (char *) ch_realloc( buf, lmax );
236 strcpy( buf + lcur, line );
241 if ( line[0] == '\n' || stop && buf && *buf ) {
245 while ( (linep = ldif_getline( &s )) != NULL ) {
247 if ( ldif_parse_line( linep, &type, &val, &vlen )
249 Debug( LDAP_DEBUG_PARSE,
250 "bad line %d in entry ending at line %d ignored\n",
251 elineno, lineno, 0 );
255 if ( !isascii( *type ) || isdigit( *type ) )
258 type = strdup( type );
259 if ( avl_insert( &avltypes, type, (AVL_CMP) strcasecmp,
260 avl_dup_error ) != 0 ) {
263 attr_masks( be->be_private, type,
264 &indexmask, &syntaxmask );
267 fork_child( cmd, args );
278 slap_shutdown(dbnum);
287 static HANDLE processes[MAXIMUM_WAIT_OBJECTS];
290 fork_child( char *prog, char *args[] )
292 PROCESS_INFORMATION proc_info;
293 PROCESS_INFORMATION *pinfo = &proc_info;
294 STARTUPINFO start_info;
298 wait4kids( maxkids );
301 memset( &start_info, 0, sizeof(STARTUPINFO) );
302 memset( cmdLine, 0, sizeof(cmdLine) );
303 strcpy( cmdLine, prog );
304 while ( args[i] != NULL )
306 strcat( cmdLine, " " );
307 strcat( cmdLine, args[i] );
311 if ( !CreateProcess( NULL, cmdLine, NULL, NULL,
312 TRUE, CREATE_NEW_CONSOLE,
313 NULL, NULL, &start_info, pinfo ) )
315 fprintf( stderr, "Could not create %s: ", prog );
316 perror( "CreateProcess" );
320 processes[nkids] = proc_info.hProcess;
326 wait4kids( int nkidval )
329 while( nkids >= nkidval )
331 wait_index = WaitForMultipleObjects( nkids, processes, FALSE, INFINITE );
333 * processes[wait_index] completed. Move any remaining indexes into its
334 * place in the array so it stays filled.
338 memcpy ( &processes[wait_index], &processes[wait_index+1], sizeof(HANDLE)*(nkids-1) );
339 processes[nkids] = 0;
348 fork_child( char *prog, char *args[] )
352 wait4kids( maxkids );
354 switch ( pid = fork() ) {
356 execvp( prog, args );
357 fprintf( stderr, "%s: ", prog );
362 case -1: /* trouble */
363 fprintf( stderr, "Could not fork to run %s\n", prog );
367 default: /* parent */
374 wait4kids( int nkidval )
379 while ( nkids >= nkidval ) {
381 p = (unsigned char *) &status;
382 if ( p[sizeof(int) - 1] == 0177 ) {
384 "stopping: child stopped with signal %d\n",
385 p[sizeof(int) - 2] );
386 } else if ( p[sizeof(int) - 1] != 0 ) {
388 "stopping: child terminated with signal %d\n",
389 p[sizeof(int) - 1] );
390 exit( p[sizeof(int) - 1] );
391 } else if ( p[sizeof(int) - 2] != 0 ) {
393 "stopping: child exited with status %d\n",
394 p[sizeof(int) - 2] );
395 exit( p[sizeof(int) - 2] );