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 pthread_mutex_t crypt_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 );
61 main( int argc, char **argv )
64 char *linep, *buf, *sbindir;
66 char buf2[20], buf3[20];
76 struct berval *vals[2];
77 Avlnode *avltypes = NULL;
79 sbindir = DEFAULT_SBINDIR;
80 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
82 while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
84 case 'd': /* turn on debugging */
85 ldap_debug = atoi( optarg );
88 case 's': /* alternate sbindir (index cmd location) */
89 case 'e': /* accept -e for backwards compatibility */
90 sbindir = strdup( optarg );
93 case 'f': /* specify a tailor file */
94 tailorfile = strdup( optarg );
97 case 'i': /* input file */
98 inputfile = strdup( optarg );
101 case 'j': /* number of parallel index procs */
102 maxkids = atoi( optarg );
105 case 'n': /* which config file db to index */
106 dbnum = atoi( optarg ) - 1;
114 if ( inputfile == NULL ) {
117 if ( freopen( inputfile, "r", stdin ) == NULL ) {
124 * initialize stuff and figure out which backend we're dealing with
128 read_config( tailorfile, &be, NULL );
131 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
132 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
137 if ( dbnum == nbackends ) {
138 fprintf( stderr, "No ldbm database found in config file\n" );
141 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
142 fprintf( stderr, "Database number selected via -n is out of range\n" );
143 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
145 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
146 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
149 be = &backends[dbnum];
151 /* disable write sync'ing */
152 li = (struct ldbminfo *) be->be_private;
153 li->li_dbcachewsync = 0;
156 * generate the id2entry index
160 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
163 args[i++] = inputfile;
165 args[i++] = tailorfile;
167 sprintf( buf2, "%d", dbnum+1 );
170 sprintf( buf3, "%d", ldap_debug );
175 fork_child( cmd, args );
178 * generate the dn2id and id2children indexes
182 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
185 args[i++] = inputfile;
187 args[i++] = tailorfile;
189 sprintf( buf2, "%d", dbnum+1 );
192 sprintf( buf3, "%d", ldap_debug );
197 fork_child( cmd, args );
200 * generate the attribute indexes
204 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
207 args[i++] = inputfile;
209 args[i++] = tailorfile;
211 sprintf( buf2, "%d", dbnum+1 );
214 sprintf( buf3, "%d", ldap_debug );
218 args[i++] = NULL; /* will hold the attribute name */
229 char *type, *val, *s;
230 int vlen, indexmask, syntaxmask;
233 memset( &key, 0, sizeof( key ));
234 memset( &data, 0, sizeof( data ));
236 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
240 len = strlen( line );
241 while ( lcur + len + 1 > lmax ) {
243 buf = (char *) ch_realloc( buf, lmax );
245 strcpy( buf + lcur, line );
250 if ( line[0] == '\n' || stop && buf && *buf ) {
254 while ( (linep = str_getline( &s )) != NULL ) {
256 if ( str_parse_line( linep, &type, &val, &vlen )
258 Debug( LDAP_DEBUG_PARSE,
259 "bad line %d in entry ending at line %d ignored\n",
260 elineno, lineno, 0 );
264 if ( !isascii( *type ) || isdigit( *type ) )
267 type = strdup( type );
268 if ( avl_insert( &avltypes, type, strcasecmp,
269 avl_dup_error ) != 0 ) {
272 attr_masks( be->be_private, type,
273 &indexmask, &syntaxmask );
276 fork_child( cmd, args );
284 (*be->be_close)( be );
292 fork_child( char *prog, char *args[] )
296 wait4kids( maxkids );
298 switch ( pid = fork() ) {
300 execvp( prog, args );
301 fprintf( stderr, "%s: ", prog );
306 case -1: /* trouble */
307 fprintf( stderr, "Could not fork to run %s\n", prog );
311 default: /* parent */
318 wait4kids( int nkidval )
323 while ( nkids >= nkidval ) {
325 p = (unsigned char *) &status;
326 if ( p[sizeof(int) - 1] == 0177 ) {
328 "stopping: child stopped with signal %d\n",
329 p[sizeof(int) - 2] );
330 } else if ( p[sizeof(int) - 1] != 0 ) {
332 "stopping: child terminated with signal %d\n",
333 p[sizeof(int) - 1] );
334 exit( p[sizeof(int) - 1] );
335 } else if ( p[sizeof(int) - 2] != 0 ) {
337 "stopping: child exited with status %d\n",
338 p[sizeof(int) - 2] );
339 exit( p[sizeof(int) - 2] );