11 #include <sys/param.h>
13 #include "ldapconfig.h"
15 #include "../back-ldbm/back-ldbm.h"
18 #define INDEXCMD "ldif2index"
19 #define ID2ENTRYCMD "ldif2id2entry"
20 #define ID2CHILDRENCMD "ldif2id2children"
25 int ldap_syslog_level;
26 int global_schemacheck;
27 long num_entries_sent;
30 char *default_referral;
31 struct objclass *global_oc;
33 pthread_t listener_tid;
34 pthread_mutex_t num_sent_mutex;
35 pthread_mutex_t entry2str_mutex;
36 pthread_mutex_t active_threads_mutex;
37 pthread_mutex_t new_conn_mutex;
38 pthread_mutex_t currenttime_mutex;
39 pthread_mutex_t replog_mutex;
40 pthread_mutex_t ops_mutex;
41 pthread_mutex_t regex_mutex;
43 static void fork_child( char *prog, char *args[] );
44 static void wait4kids( int nkidval );
46 static char *indexcmd;
47 static char *tailorfile;
48 static char *inputfile;
49 static int maxkids = 1;
55 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
60 main( int argc, char **argv )
63 char *linep, *buf, *sbindir;
65 char buf2[20], buf3[20];
75 struct berval *vals[2];
76 Avlnode *avltypes = NULL;
78 sbindir = DEFAULT_SBINDIR;
79 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
81 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
83 case 'd': /* turn on debugging */
84 ldap_debug = atoi( optarg );
87 case 's': /* alternate sbindir (index cmd location) */
88 case 'e': /* accept -e for backwards compatibility */
89 sbindir = strdup( optarg );
92 case 'f': /* specify a tailor file */
93 tailorfile = strdup( optarg );
96 case 'i': /* input file */
97 inputfile = strdup( optarg );
100 case 'j': /* number of parallel index procs */
101 maxkids = atoi( optarg );
104 case 'n': /* which config file db to index */
105 dbnum = atoi( optarg ) - 1;
113 if ( inputfile == NULL ) {
116 if ( freopen( inputfile, "r", stdin ) == NULL ) {
123 * initialize stuff and figure out which backend we're dealing with
127 read_config( tailorfile, &be, NULL );
130 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
131 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
136 if ( dbnum == nbackends ) {
137 fprintf( stderr, "No ldbm database found in config file\n" );
140 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
141 fprintf( stderr, "Database number selected via -n is out of range\n" );
142 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
144 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
145 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
148 be = &backends[dbnum];
150 /* disable write sync'ing */
151 li = (struct ldbminfo *) be->be_private;
152 li->li_dbcachewsync = 0;
155 * generate the id2entry index
159 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
162 args[i++] = inputfile;
164 args[i++] = tailorfile;
166 sprintf( buf2, "%d", dbnum+1 );
169 sprintf( buf3, "%d", ldap_debug );
174 fork_child( cmd, args );
177 * generate the dn2id and id2children indexes
181 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
184 args[i++] = inputfile;
186 args[i++] = tailorfile;
188 sprintf( buf2, "%d", dbnum+1 );
191 sprintf( buf3, "%d", ldap_debug );
196 fork_child( cmd, args );
199 * generate the attribute indexes
203 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
206 args[i++] = inputfile;
208 args[i++] = tailorfile;
210 sprintf( buf2, "%d", dbnum+1 );
213 sprintf( buf3, "%d", ldap_debug );
217 args[i++] = NULL; /* will hold the attribute name */
228 char *type, *val, *s;
229 int vlen, indexmask, syntaxmask;
232 memset( &key, 0, sizeof( key ));
233 memset( &data, 0, sizeof( data ));
235 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
239 len = strlen( line );
240 while ( lcur + len + 1 > lmax ) {
242 buf = (char *) ch_realloc( buf, lmax );
244 strcpy( buf + lcur, line );
249 if ( line[0] == '\n' || stop && buf && *buf ) {
253 while ( (linep = str_getline( &s )) != NULL ) {
255 if ( str_parse_line( linep, &type, &val, &vlen )
257 Debug( LDAP_DEBUG_PARSE,
258 "bad line %d in entry ending at line %d ignored\n",
259 elineno, lineno, 0 );
263 if ( !isascii( *type ) || isdigit( *type ) )
266 type = strdup( type );
267 if ( avl_insert( &avltypes, type, strcasecmp,
268 avl_dup_error ) != 0 ) {
271 attr_masks( be->be_private, type,
272 &indexmask, &syntaxmask );
275 fork_child( cmd, args );
283 (*be->be_close)( be );
291 fork_child( char *prog, char *args[] )
295 wait4kids( maxkids );
297 switch ( pid = fork() ) {
299 execvp( prog, args );
300 fprintf( stderr, "%s: ", prog );
305 case -1: /* trouble */
306 fprintf( stderr, "Could not fork to run %s\n", prog );
310 default: /* parent */
317 wait4kids( int nkidval )
322 while ( nkids >= nkidval ) {
324 p = (unsigned char *) &status;
325 if ( p[sizeof(int) - 1] == 0177 ) {
327 "stopping: child stopped with signal %d\n",
328 p[sizeof(int) - 2] );
329 } else if ( p[sizeof(int) - 1] != 0 ) {
331 "stopping: child terminated with signal %d\n",
332 p[sizeof(int) - 1] );
333 exit( p[sizeof(int) - 1] );
334 } else if ( p[sizeof(int) - 2] != 0 ) {
336 "stopping: child exited with status %d\n",
337 p[sizeof(int) - 2] );
338 exit( p[sizeof(int) - 2] );