11 #include "../back-ldbm/back-ldbm.h"
12 #include "ldapconfig.h"
14 #define INDEXCMD "ldif2index"
15 #define ID2ENTRYCMD "ldif2id2entry"
16 #define ID2CHILDRENCMD "ldif2id2children"
19 extern void attr_index_config();
20 extern char *str_getline();
21 extern int strcasecmp();
23 extern Backend *backends;
24 extern int ldap_debug;
28 int ldap_syslog_level;
29 int global_schemacheck;
33 char *default_referral;
34 struct objclass *global_oc;
36 pthread_t listener_tid;
37 pthread_mutex_t num_sent_mutex;
38 pthread_mutex_t entry2str_mutex;
39 pthread_mutex_t active_threads_mutex;
40 pthread_mutex_t new_conn_mutex;
41 pthread_mutex_t currenttime_mutex;
42 pthread_mutex_t replog_mutex;
43 pthread_mutex_t ops_mutex;
44 pthread_mutex_t regex_mutex;
46 static void fork_child();
47 static void wait4kids();
49 static char *indexcmd;
50 static char *tailorfile;
51 static char *inputfile;
52 static int maxkids = 1;
58 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
62 main( int argc, char **argv )
65 char *linep, *buf, *sbindir;
67 char buf2[20], buf3[20];
76 struct berval *vals[2];
77 Avlnode *avltypes = NULL;
80 sbindir = DEFAULT_SBINDIR;
81 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
83 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
85 case 'd': /* turn on debugging */
86 ldap_debug = atoi( optarg );
89 case 's': /* alternate sbindir (index cmd location) */
90 case 'e': /* accept -e for backwards compatibility */
91 sbindir = strdup( optarg );
94 case 'f': /* specify a tailor file */
95 tailorfile = strdup( optarg );
98 case 'i': /* input file */
99 inputfile = strdup( optarg );
102 case 'j': /* number of parallel index procs */
103 maxkids = atoi( optarg );
106 case 'n': /* which config file db to index */
107 dbnum = atoi( optarg ) - 1;
115 if ( inputfile == NULL ) {
118 if ( freopen( inputfile, "r", stdin ) == NULL ) {
125 * initialize stuff and figure out which backend we're dealing with
129 read_config( tailorfile, &be, NULL );
132 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
133 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
138 if ( dbnum == nbackends ) {
139 fprintf( stderr, "No ldbm database found in config file\n" );
142 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
143 fprintf( stderr, "Database number selected via -n is out of range\n" );
144 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
146 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
147 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
150 be = &backends[dbnum];
153 * generate the id2entry index
157 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
160 args[i++] = inputfile;
162 args[i++] = tailorfile;
164 sprintf( buf2, "%d", dbnum+1 );
167 sprintf( buf3, "%d", ldap_debug );
172 fork_child( cmd, args );
175 * generate the dn2id and id2children indexes
179 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
182 args[i++] = inputfile;
184 args[i++] = tailorfile;
186 sprintf( buf2, "%d", dbnum+1 );
189 sprintf( buf3, "%d", ldap_debug );
194 fork_child( cmd, args );
197 * generate the attribute indexes
201 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
204 args[i++] = inputfile;
206 args[i++] = tailorfile;
208 sprintf( buf2, "%d", dbnum+1 );
211 sprintf( buf3, "%d", ldap_debug );
215 args[i++] = NULL; /* will hold the attribute name */
226 char *type, *val, *s;
227 int vlen, indexmask, syntaxmask;
230 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
234 len = strlen( line );
235 while ( lcur + len + 1 > lmax ) {
237 buf = (char *) ch_realloc( buf, lmax );
239 strcpy( buf + lcur, line );
244 if ( line[0] == '\n' || stop && buf && *buf ) {
248 while ( (linep = str_getline( &s )) != NULL ) {
250 if ( str_parse_line( linep, &type, &val, &vlen )
252 Debug( LDAP_DEBUG_PARSE,
253 "bad line %d in entry ending at line %d ignored\n",
254 elineno, lineno, 0 );
258 if ( !isascii( *type ) || isdigit( *type ) )
261 type = strdup( type );
262 if ( avl_insert( &avltypes, type, strcasecmp,
263 avl_dup_error ) != 0 ) {
266 attr_masks( be->be_private, type,
267 &indexmask, &syntaxmask );
270 fork_child( cmd, args );
278 (*be->be_close)( be );
286 fork_child( char *prog, char *args[] )
290 wait4kids( maxkids );
292 switch ( pid = fork() ) {
294 execvp( prog, args );
295 fprintf( stderr, "%s: ", prog );
300 case -1: /* trouble */
301 fprintf( stderr, "Could not fork to run %s\n", prog );
305 default: /* parent */
312 wait4kids( int nkidval )
317 while ( nkids >= nkidval ) {
319 p = (unsigned char *) &status;
320 if ( p[sizeof(int) - 1] == 0177 ) {
322 "stopping: child stopped with signal %d\n",
323 p[sizeof(int) - 2] );
324 } else if ( p[sizeof(int) - 1] != 0 ) {
326 "stopping: child terminated with signal %d\n",
327 p[sizeof(int) - 1] );
328 exit( p[sizeof(int) - 1] );
329 } else if ( p[sizeof(int) - 2] != 0 ) {
331 "stopping: child exited with status %d\n",
332 p[sizeof(int) - 2] );
333 exit( p[sizeof(int) - 2] );