2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
12 #include <ac/socket.h>
13 #include <ac/unistd.h>
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
20 #include "ldif2common.h"
21 #include "../back-ldbm/back-ldbm.h"
24 #define INDEXCMD "ldif2index" EXEEXT
25 #define ID2ENTRYCMD "ldif2id2entry" EXEEXT
26 #define ID2CHILDRENCMD "ldif2id2children" EXEEXT
30 static void fork_child( char *prog, char *args[] );
31 static void wait4kids( int nkidval );
33 static int maxkids = 1;
37 main( int argc, char **argv )
42 char buf2[20], buf3[20];
51 struct berval *vals[2];
52 Avlnode *avltypes = NULL;
54 ldbm_ignore_nextid_file = 1;
56 slap_ldif_init( argc, argv, LDIF2LDBM, "ldbm", SLAP_TOOL_MODE );
60 be = &backends[dbnum];
62 /* disable write sync'ing */
63 li = (struct ldbminfo *) be->be_private;
64 li->li_dbcachewsync = 0;
67 * generate the id2entry index
71 sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
74 args[i++] = inputfile;
76 args[i++] = tailorfile;
78 sprintf( buf2, "%d", dbnum+1 );
81 sprintf( buf3, "%d", ldap_debug );
86 fork_child( cmd, args );
89 * generate the dn2id and id2children indexes
93 sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
96 args[i++] = inputfile;
98 args[i++] = tailorfile;
100 sprintf( buf2, "%d", dbnum+1 );
103 sprintf( buf3, "%d", ldap_debug );
108 fork_child( cmd, args );
113 * generate the attribute indexes
117 sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
120 args[i++] = inputfile;
122 args[i++] = tailorfile;
124 sprintf( buf2, "%d", dbnum+1 );
127 sprintf( buf3, "%d", ldap_debug );
131 args[i++] = NULL; /* will hold the attribute name */
142 char *type, *val, *s;
144 int indexmask, syntaxmask;
146 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
150 len = strlen( line );
151 while ( lcur + len + 1 > lmax ) {
153 buf = (char *) ch_realloc( buf, lmax );
155 strcpy( buf + lcur, line );
160 if ( line[0] == '\n' || stop && buf && *buf ) {
164 while ( (linep = ldif_getline( &s )) != NULL ) {
166 if ( ldif_parse_line( linep, &type, &val, &vlen )
168 Debug( LDAP_DEBUG_PARSE,
169 "bad line %d in entry ending at line %d ignored\n",
170 elineno, lineno, 0 );
174 if ( !isascii( *type ) || isdigit( *type ) )
177 type = strdup( type );
178 if ( avl_insert( &avltypes, type, (AVL_CMP) strcasecmp,
179 avl_dup_error ) != 0 ) {
182 attr_masks( be->be_private, type,
183 &indexmask, &syntaxmask );
186 fork_child( cmd, args );
197 slap_shutdown(dbnum);
201 return( EXIT_SUCCESS );
206 static HANDLE processes[MAXIMUM_WAIT_OBJECTS];
209 fork_child( char *prog, char *args[] )
211 PROCESS_INFORMATION proc_info;
212 PROCESS_INFORMATION *pinfo = &proc_info;
213 STARTUPINFO start_info;
217 wait4kids( maxkids );
220 memset( &start_info, 0, sizeof(STARTUPINFO) );
221 memset( cmdLine, 0, sizeof(cmdLine) );
222 strcpy( cmdLine, prog );
223 while ( args[i] != NULL )
225 strcat( cmdLine, " " );
226 strcat( cmdLine, args[i] );
230 if ( !CreateProcess( NULL, cmdLine, NULL, NULL,
231 TRUE, CREATE_NEW_CONSOLE,
232 NULL, NULL, &start_info, pinfo ) )
234 fprintf( stderr, "Could not create %s: ", prog );
235 perror( "CreateProcess" );
239 processes[nkids] = proc_info.hProcess;
245 wait4kids( int nkidval )
248 while( nkids >= nkidval )
250 wait_index = WaitForMultipleObjects( nkids, processes, FALSE, INFINITE );
252 * processes[wait_index] completed. Move any remaining indexes into its
253 * place in the array so it stays filled.
257 memcpy ( &processes[wait_index], &processes[wait_index+1], sizeof(HANDLE)*(nkids-1) );
258 processes[nkids] = 0;
267 fork_child( char *prog, char *args[] )
271 wait4kids( maxkids );
273 switch ( pid = fork() ) {
275 execvp( prog, args );
276 fprintf( stderr, "%s: ", prog );
278 exit( EXIT_FAILURE );
281 case -1: /* trouble */
282 fprintf( stderr, "Could not fork to run %s\n", prog );
286 default: /* parent */
293 wait4kids( int nkidval )
298 while ( nkids >= nkidval ) {
300 p = (unsigned char *) &status;
301 if ( p[sizeof(int) - 1] == 0177 ) {
303 "stopping: child stopped with signal %d\n",
304 p[sizeof(int) - 2] );
305 } else if ( p[sizeof(int) - 1] != 0 ) {
307 "stopping: child terminated with signal %d\n",
308 p[sizeof(int) - 1] );
309 exit( p[sizeof(int) - 1] );
310 } else if ( p[sizeof(int) - 2] != 0 ) {
312 "stopping: child exited with status %d\n",
313 p[sizeof(int) - 2] );
314 exit( p[sizeof(int) - 2] );