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 long num_entries_sent;
29 char *default_referral;
31 pthread_t listener_tid;
32 pthread_mutex_t num_sent_mutex;
33 pthread_mutex_t entry2str_mutex;
34 pthread_mutex_t active_threads_mutex;
35 pthread_mutex_t new_conn_mutex;
36 pthread_mutex_t currenttime_mutex;
37 pthread_mutex_t replog_mutex;
38 pthread_mutex_t ops_mutex;
39 pthread_mutex_t regex_mutex;
41 static void fork_child( char *prog, char *args[] );
42 static void wait4kids( int nkidval );
44 static char *indexcmd;
45 static char *tailorfile;
46 static char *inputfile;
47 static int maxkids = 1;
53 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
58 main( int argc, char **argv )
61 char *linep, *buf, *sbindir;
63 char buf2[20], buf3[20];
73 struct berval *vals[2];
74 Avlnode *avltypes = NULL;
76 sbindir = DEFAULT_SBINDIR;
77 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
79 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
81 case 'd': /* turn on debugging */
82 ldap_debug = atoi( optarg );
85 case 's': /* alternate sbindir (index cmd location) */
86 case 'e': /* accept -e for backwards compatibility */
87 sbindir = strdup( optarg );
90 case 'f': /* specify a tailor file */
91 tailorfile = strdup( optarg );
94 case 'i': /* input file */
95 inputfile = strdup( optarg );
98 case 'j': /* number of parallel index procs */
99 maxkids = atoi( optarg );
102 case 'n': /* which config file db to index */
103 dbnum = atoi( optarg ) - 1;
111 if ( inputfile == NULL ) {
114 if ( freopen( inputfile, "r", stdin ) == NULL ) {
121 * initialize stuff and figure out which backend we're dealing with
125 read_config( tailorfile, &be, NULL );
128 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
129 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
134 if ( dbnum == nbackends ) {
135 fprintf( stderr, "No ldbm database found in config file\n" );
138 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
139 fprintf( stderr, "Database number selected via -n is out of range\n" );
140 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
142 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
143 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
146 be = &backends[dbnum];
148 /* disable write sync'ing */
149 li = (struct ldbminfo *) be->be_private;
150 li->li_dbcachewsync = 0;
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 memset( &key, 0, sizeof( key ));
231 memset( &data, 0, sizeof( data ));
233 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
237 len = strlen( line );
238 while ( lcur + len + 1 > lmax ) {
240 buf = (char *) ch_realloc( buf, lmax );
242 strcpy( buf + lcur, line );
247 if ( line[0] == '\n' || stop && buf && *buf ) {
251 while ( (linep = str_getline( &s )) != NULL ) {
253 if ( str_parse_line( linep, &type, &val, &vlen )
255 Debug( LDAP_DEBUG_PARSE,
256 "bad line %d in entry ending at line %d ignored\n",
257 elineno, lineno, 0 );
261 if ( !isascii( *type ) || isdigit( *type ) )
264 type = strdup( type );
265 if ( avl_insert( &avltypes, type, strcasecmp,
266 avl_dup_error ) != 0 ) {
269 attr_masks( be->be_private, type,
270 &indexmask, &syntaxmask );
273 fork_child( cmd, args );
281 (*be->be_close)( be );
289 fork_child( char *prog, char *args[] )
293 wait4kids( maxkids );
295 switch ( pid = fork() ) {
297 execvp( prog, args );
298 fprintf( stderr, "%s: ", prog );
303 case -1: /* trouble */
304 fprintf( stderr, "Could not fork to run %s\n", prog );
308 default: /* parent */
315 wait4kids( int nkidval )
320 while ( nkids >= nkidval ) {
322 p = (unsigned char *) &status;
323 if ( p[sizeof(int) - 1] == 0177 ) {
325 "stopping: child stopped with signal %d\n",
326 p[sizeof(int) - 2] );
327 } else if ( p[sizeof(int) - 1] != 0 ) {
329 "stopping: child terminated with signal %d\n",
330 p[sizeof(int) - 1] );
331 exit( p[sizeof(int) - 1] );
332 } else if ( p[sizeof(int) - 2] != 0 ) {
334 "stopping: child exited with status %d\n",
335 p[sizeof(int) - 2] );
336 exit( p[sizeof(int) - 2] );