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 strtok_mutex;
38 pthread_mutex_t replog_mutex;
39 pthread_mutex_t ops_mutex;
40 pthread_mutex_t regex_mutex;
42 pthread_mutex_t crypt_mutex;
45 static void fork_child( char *prog, char *args[] );
46 static void wait4kids( int nkidval );
48 static char *indexcmd;
49 static char *tailorfile;
50 static char *inputfile;
51 static int maxkids = 1;
57 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];
77 struct berval *vals[2];
78 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];
152 /* disable write sync'ing */
153 li = (struct ldbminfo *) be->be_private;
154 li->li_dbcachewsync = 0;
157 * generate the id2entry index
161 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
164 args[i++] = inputfile;
166 args[i++] = tailorfile;
168 sprintf( buf2, "%d", dbnum+1 );
171 sprintf( buf3, "%d", ldap_debug );
176 fork_child( cmd, args );
179 * generate the dn2id and id2children indexes
183 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
186 args[i++] = inputfile;
188 args[i++] = tailorfile;
190 sprintf( buf2, "%d", dbnum+1 );
193 sprintf( buf3, "%d", ldap_debug );
198 fork_child( cmd, args );
201 * generate the attribute indexes
205 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
208 args[i++] = inputfile;
210 args[i++] = tailorfile;
212 sprintf( buf2, "%d", dbnum+1 );
215 sprintf( buf3, "%d", ldap_debug );
219 args[i++] = NULL; /* will hold the attribute name */
230 char *type, *val, *s;
231 int vlen, indexmask, syntaxmask;
234 ldbm_datum_init( key );
235 ldbm_datum_init( data );
237 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
241 len = strlen( line );
242 while ( lcur + len + 1 > lmax ) {
244 buf = (char *) ch_realloc( buf, lmax );
246 strcpy( buf + lcur, line );
251 if ( line[0] == '\n' || stop && buf && *buf ) {
255 while ( (linep = str_getline( &s )) != NULL ) {
257 if ( str_parse_line( linep, &type, &val, &vlen )
259 Debug( LDAP_DEBUG_PARSE,
260 "bad line %d in entry ending at line %d ignored\n",
261 elineno, lineno, 0 );
265 if ( !isascii( *type ) || isdigit( *type ) )
268 type = strdup( type );
269 if ( avl_insert( &avltypes, type, strcasecmp,
270 avl_dup_error ) != 0 ) {
273 attr_masks( be->be_private, type,
274 &indexmask, &syntaxmask );
277 fork_child( cmd, args );
285 (*be->be_close)( be );
293 fork_child( char *prog, char *args[] )
297 wait4kids( maxkids );
299 switch ( pid = fork() ) {
301 execvp( prog, args );
302 fprintf( stderr, "%s: ", prog );
307 case -1: /* trouble */
308 fprintf( stderr, "Could not fork to run %s\n", prog );
312 default: /* parent */
319 wait4kids( int nkidval )
324 while ( nkids >= nkidval ) {
326 p = (unsigned char *) &status;
327 if ( p[sizeof(int) - 1] == 0177 ) {
329 "stopping: child stopped with signal %d\n",
330 p[sizeof(int) - 2] );
331 } else if ( p[sizeof(int) - 1] != 0 ) {
333 "stopping: child terminated with signal %d\n",
334 p[sizeof(int) - 1] );
335 exit( p[sizeof(int) - 1] );
336 } else if ( p[sizeof(int) - 2] != 0 ) {
338 "stopping: child exited with status %d\n",
339 p[sizeof(int) - 2] );
340 exit( p[sizeof(int) - 2] );