4 #include <sys/socket.h>
7 #include "../back-ldbm/back-ldbm.h"
9 #define DEFAULT_CONFIGFILE "%ETCDIR%/slapd.conf"
10 #define DEFAULT_ETCDIR "%ETCDIR%"
11 #define INDEXCMD "ldif2index"
12 #define ID2ENTRYCMD "ldif2id2entry"
13 #define ID2CHILDRENCMD "ldif2id2children"
16 extern void attr_index_config();
17 extern char *str_getline();
18 extern int strcasecmp();
20 extern Backend *backends;
21 extern int ldap_debug;
25 int ldap_syslog_level;
26 int global_schemacheck;
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();
44 static void wait4kids();
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] [-e etcdir]\n", name );
59 main( int argc, char **argv )
62 char *linep, *buf, *etcdir;
64 char buf2[20], buf3[20];
73 struct berval *vals[2];
74 Avlnode *avltypes = NULL;
77 etcdir = DEFAULT_ETCDIR;
78 tailorfile = DEFAULT_CONFIGFILE;
80 while ( (i = getopt( argc, argv, "d:e:f:i:j:n:" )) != EOF ) {
82 case 'd': /* turn on debugging */
83 ldap_debug = atoi( optarg );
86 case 'e': /* alternate etcdir (index cmd location) */
87 etcdir = 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 ) {
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];
149 * generate the id2entry index
153 sprintf( cmd, "%s/%s", etcdir, ID2ENTRYCMD );
156 args[i++] = inputfile;
158 args[i++] = tailorfile;
160 sprintf( buf2, "%d", dbnum );
163 sprintf( buf3, "%d", ldap_debug );
168 fork_child( cmd, args );
171 * generate the dn2id and id2children indexes
175 sprintf( cmd, "%s/%s", etcdir, ID2CHILDRENCMD );
178 args[i++] = inputfile;
180 args[i++] = tailorfile;
182 sprintf( buf2, "%d", dbnum );
185 sprintf( buf3, "%d", ldap_debug );
190 fork_child( cmd, args );
193 * generate the attribute indexes
197 sprintf( cmd, "%s/%s", etcdir, INDEXCMD );
200 args[i++] = inputfile;
202 args[i++] = tailorfile;
204 sprintf( buf2, "%d", dbnum );
207 sprintf( buf3, "%d", ldap_debug );
211 args[i++] = NULL; /* will hold the attribute name */
222 char *type, *val, *s;
223 int vlen, indexmask, syntaxmask;
226 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
230 len = strlen( line );
231 while ( lcur + len + 1 > lmax ) {
233 buf = (char *) ch_realloc( buf, lmax );
235 strcpy( buf + lcur, line );
240 if ( line[0] == '\n' || stop && buf && *buf ) {
244 while ( (linep = str_getline( &s )) != NULL ) {
246 if ( str_parse_line( linep, &type, &val, &vlen )
248 Debug( LDAP_DEBUG_PARSE,
249 "bad line %d in entry ending at line %d ignored\n",
250 elineno, lineno, 0 );
254 if ( !isascii( *type ) || isdigit( *type ) )
257 type = strdup( type );
258 if ( avl_insert( &avltypes, type, strcasecmp,
259 avl_dup_error ) != 0 ) {
262 attr_masks( be->be_private, type,
263 &indexmask, &syntaxmask );
266 fork_child( cmd, args );
274 (*be->be_close)( be );
282 fork_child( char *prog, char *args[] )
286 wait4kids( maxkids );
288 switch ( pid = fork() ) {
290 execvp( prog, args );
291 fprintf( stderr, "%s: ", prog );
296 case -1: /* trouble */
297 fprintf( stderr, "Could not fork to run %s\n", prog );
301 default: /* parent */
308 wait4kids( int nkidval )
313 while ( nkids >= nkidval ) {
315 p = (unsigned char *) &status;
316 if ( p[sizeof(int) - 1] == 0177 ) {
318 "stopping: child stopped with signal %d\n",
319 p[sizeof(int) - 2] );
320 } else if ( p[sizeof(int) - 1] != 0 ) {
322 "stopping: child terminated with signal %d\n",
323 p[sizeof(int) - 1] );
324 exit( p[sizeof(int) - 1] );
325 } else if ( p[sizeof(int) - 2] != 0 ) {
327 "stopping: child exited with status %d\n",
328 p[sizeof(int) - 2] );
329 exit( p[sizeof(int) - 2] );