11 #include "../back-ldbm/back-ldbm.h"
12 #include "ldapconfig.h"
15 #define INDEXCMD "ldif2index"
16 #define ID2ENTRYCMD "ldif2id2entry"
17 #define ID2CHILDRENCMD "ldif2id2children"
21 extern Backend *backends;
22 extern int ldap_debug;
26 int ldap_syslog_level;
27 int global_schemacheck;
31 char *default_referral;
32 struct objclass *global_oc;
34 pthread_t listener_tid;
35 pthread_mutex_t num_sent_mutex;
36 pthread_mutex_t entry2str_mutex;
37 pthread_mutex_t active_threads_mutex;
38 pthread_mutex_t new_conn_mutex;
39 pthread_mutex_t currenttime_mutex;
40 pthread_mutex_t replog_mutex;
41 pthread_mutex_t ops_mutex;
42 pthread_mutex_t regex_mutex;
44 static void fork_child( char *prog, char *args[] );
45 static void wait4kids( int nkidval );
47 static char *indexcmd;
48 static char *tailorfile;
49 static char *inputfile;
50 static int maxkids = 1;
56 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];
74 struct berval *vals[2];
75 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];
151 * generate the id2entry index
155 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
158 args[i++] = inputfile;
160 args[i++] = tailorfile;
162 sprintf( buf2, "%d", dbnum+1 );
165 sprintf( buf3, "%d", ldap_debug );
170 fork_child( cmd, args );
173 * generate the dn2id and id2children indexes
177 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
180 args[i++] = inputfile;
182 args[i++] = tailorfile;
184 sprintf( buf2, "%d", dbnum+1 );
187 sprintf( buf3, "%d", ldap_debug );
192 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;
228 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
232 len = strlen( line );
233 while ( lcur + len + 1 > lmax ) {
235 buf = (char *) ch_realloc( buf, lmax );
237 strcpy( buf + lcur, line );
242 if ( line[0] == '\n' || stop && buf && *buf ) {
246 while ( (linep = str_getline( &s )) != NULL ) {
248 if ( str_parse_line( linep, &type, &val, &vlen )
250 Debug( LDAP_DEBUG_PARSE,
251 "bad line %d in entry ending at line %d ignored\n",
252 elineno, lineno, 0 );
256 if ( !isascii( *type ) || isdigit( *type ) )
259 type = strdup( type );
260 if ( avl_insert( &avltypes, type, strcasecmp,
261 avl_dup_error ) != 0 ) {
264 attr_masks( be->be_private, type,
265 &indexmask, &syntaxmask );
268 fork_child( cmd, args );
276 (*be->be_close)( be );
284 fork_child( char *prog, char *args[] )
288 wait4kids( maxkids );
290 switch ( pid = fork() ) {
292 execvp( prog, args );
293 fprintf( stderr, "%s: ", prog );
298 case -1: /* trouble */
299 fprintf( stderr, "Could not fork to run %s\n", prog );
303 default: /* parent */
310 wait4kids( int nkidval )
315 while ( nkids >= nkidval ) {
317 p = (unsigned char *) &status;
318 if ( p[sizeof(int) - 1] == 0177 ) {
320 "stopping: child stopped with signal %d\n",
321 p[sizeof(int) - 2] );
322 } else if ( p[sizeof(int) - 1] != 0 ) {
324 "stopping: child terminated with signal %d\n",
325 p[sizeof(int) - 1] );
326 exit( p[sizeof(int) - 1] );
327 } else if ( p[sizeof(int) - 2] != 0 ) {
329 "stopping: child exited with status %d\n",
330 p[sizeof(int) - 2] );
331 exit( p[sizeof(int) - 2] );