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];
74 struct berval *vals[2];
75 Avlnode *avltypes = NULL;
77 sbindir = DEFAULT_SBINDIR;
78 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
80 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
82 case 'd': /* turn on debugging */
83 ldap_debug = atoi( optarg );
86 case 's': /* alternate sbindir (index cmd location) */
87 case 'e': /* accept -e for backwards compatibility */
88 sbindir = strdup( optarg );
91 case 'f': /* specify a tailor file */
92 tailorfile = strdup( optarg );
95 case 'i': /* input file */
96 inputfile = strdup( optarg );
99 case 'j': /* number of parallel index procs */
100 maxkids = atoi( optarg );
103 case 'n': /* which config file db to index */
104 dbnum = atoi( optarg ) - 1;
112 if ( inputfile == NULL ) {
115 if ( freopen( inputfile, "r", stdin ) == NULL ) {
122 * initialize stuff and figure out which backend we're dealing with
126 read_config( tailorfile, &be, NULL );
129 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
130 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
135 if ( dbnum == nbackends ) {
136 fprintf( stderr, "No ldbm database found in config file\n" );
139 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
140 fprintf( stderr, "Database number selected via -n is out of range\n" );
141 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
143 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
144 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
147 be = &backends[dbnum];
150 * generate the id2entry index
154 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
157 args[i++] = inputfile;
159 args[i++] = tailorfile;
161 sprintf( buf2, "%d", dbnum+1 );
164 sprintf( buf3, "%d", ldap_debug );
169 fork_child( cmd, args );
172 * generate the dn2id and id2children indexes
176 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
179 args[i++] = inputfile;
181 args[i++] = tailorfile;
183 sprintf( buf2, "%d", dbnum+1 );
186 sprintf( buf3, "%d", ldap_debug );
191 fork_child( cmd, args );
194 * generate the attribute indexes
198 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
201 args[i++] = inputfile;
203 args[i++] = tailorfile;
205 sprintf( buf2, "%d", dbnum+1 );
208 sprintf( buf3, "%d", ldap_debug );
212 args[i++] = NULL; /* will hold the attribute name */
223 char *type, *val, *s;
224 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 = str_getline( &s )) != NULL ) {
247 if ( str_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, strcasecmp,
260 avl_dup_error ) != 0 ) {
263 attr_masks( be->be_private, type,
264 &indexmask, &syntaxmask );
267 fork_child( cmd, args );
275 (*be->be_close)( be );
283 fork_child( char *prog, char *args[] )
287 wait4kids( maxkids );
289 switch ( pid = fork() ) {
291 execvp( prog, args );
292 fprintf( stderr, "%s: ", prog );
297 case -1: /* trouble */
298 fprintf( stderr, "Could not fork to run %s\n", prog );
302 default: /* parent */
309 wait4kids( int nkidval )
314 while ( nkids >= nkidval ) {
316 p = (unsigned char *) &status;
317 if ( p[sizeof(int) - 1] == 0177 ) {
319 "stopping: child stopped with signal %d\n",
320 p[sizeof(int) - 2] );
321 } else if ( p[sizeof(int) - 1] != 0 ) {
323 "stopping: child terminated with signal %d\n",
324 p[sizeof(int) - 1] );
325 exit( p[sizeof(int) - 1] );
326 } else if ( p[sizeof(int) - 2] != 0 ) {
328 "stopping: child exited with status %d\n",
329 p[sizeof(int) - 2] );
330 exit( p[sizeof(int) - 2] );