]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
Add CSRIMALLOC support
[openldap] / servers / slapd / tools / slapcommon.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* slapcommon.c - common routine for the slap tools */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
16
17 #include "slapcommon.h"
18 #include "lutil.h"
19
20
21 char    *progname       = NULL;
22 char    *conffile       = SLAPD_DEFAULT_CONFIGFILE;
23 int             truncatemode = 0;
24 int             verbose         = 0;
25 int             noschemacheck = 0;
26 int             continuemode = 0;
27
28 char    *ldiffile       = NULL;
29 FILE    *ldiffp         = NULL;
30
31 #ifdef CSRIMALLOC
32         char *leakfilename;
33         FILE *leakfile;
34 #endif
35
36 Backend *be = NULL;
37
38 static void
39 usage( int tool )
40 {
41         char *options = NULL;
42         fprintf( stderr,
43                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
44                          "\t[-n databasenumber | -b suffix]", progname );
45
46         switch( tool ) {
47         case SLAPADD:
48                 options = "\t[-s] [-l ldiffile]\n";
49                 break;
50
51         case SLAPCAT:
52                 options = "\t[-l ldiffile]\n";
53                 break;
54
55         case SLAPINDEX:
56                 options = "\tattributetype\n";
57                 break;
58         }
59
60         if( options != NULL ) {
61                 fputs( options, stderr );
62         }
63         exit( EXIT_FAILURE );
64 }
65
66
67 /*
68  * slap_tool_init - initialize slap utility, handle program options.
69  * arguments:
70  *      name            program name
71  *      tool            tool code
72  *      argc, argv      command line arguments
73  */
74
75 void
76 slap_tool_init(
77         const char* name,
78         int tool,
79         int argc, char **argv )
80 {
81         char *options;
82         char *base = NULL;
83         int rc, i, dbnum;
84         int mode = SLAP_TOOL_MODE;
85
86         progname = lutil_progname( name, argc, argv );
87
88 #ifdef CSRIMALLOC
89         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
90         sprintf( leakfilename, "%s.leak", progname );
91         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
92                 leakfile = stderr;
93         }
94         free( leakfilename );
95 #endif
96
97         switch( tool ) {
98         case SLAPADD:
99                 options = "b:cd:f:l:n:stv";
100                 break;
101
102         case SLAPINDEX:
103                 options = "b:cd:f:n:v";
104                 break;
105
106         case SLAPCAT:
107                 options = "b:cd:f:l:n:v";
108                 break;
109
110         default:
111                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
112                          progname, tool );
113                 exit( EXIT_FAILURE );
114         }
115
116         ldiffile = NULL;
117         conffile = SLAPD_DEFAULT_CONFIGFILE;
118         dbnum = -1;
119         while ( (i = getopt( argc, argv, options )) != EOF ) {
120                 switch ( i ) {
121                 case 'b':
122                         base = strdup( optarg );
123
124                 case 'c':       /* enable continue mode */
125                         continuemode++;
126                         break;
127
128                 case 'd':       /* turn on debugging */
129                         ldap_debug += atoi( optarg );
130                         break;
131
132                 case 'f':       /* specify a conf file */
133                         conffile = strdup( optarg );
134                         break;
135
136                 case 'l':       /* LDIF file */
137                         ldiffile = strdup( optarg );
138                         break;
139
140                 case 'n':       /* which config file db to index */
141                         dbnum = atoi( optarg ) - 1;
142                         break;
143
144                 case 's':       /* disable schema checking */
145                         noschemacheck++;
146                         break;
147
148                 case 't':       /* turn on truncate */
149                         truncatemode++;
150                         mode |= SLAP_TRUNCATE_MODE;
151                         break;
152
153                 case 'v':       /* turn on verbose */
154                         verbose++;
155                         break;
156
157                 default:
158                         usage( tool );
159                         break;
160                 }
161         }
162
163         if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
164                 || (dbnum >= 0 && base != NULL ) )
165         {
166                 usage( tool );
167         }
168
169         if ( ldiffile == NULL ) {
170                 ldiffp = tool == SLAPCAT ? stdout : stdin;
171
172         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
173                 == NULL )
174         {
175                 perror( ldiffile );
176                 exit( EXIT_FAILURE );
177         }
178
179         /*
180          * initialize stuff and figure out which backend we're dealing with
181          */
182
183         rc = slap_init( mode, progname );
184
185         if (rc != 0 ) {
186                 fprintf( stderr, "%s: slap_init failed!\n", progname );
187                 exit( EXIT_FAILURE );
188         }
189
190         read_config( conffile );
191
192         if ( !nbackends ) {
193                 fprintf( stderr, "No databases found in config file\n" );
194                 exit( EXIT_FAILURE );
195         }
196
197         if( base != NULL ) {
198                 char *tbase = ch_strdup( base );
199
200                 if( dn_normalize_case( tbase ) == NULL ) {
201                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
202                                 progname, base );
203                         exit( EXIT_FAILURE );
204                 }
205
206                 be = select_backend( tbase );
207                 free( tbase );
208
209                 if( be == NULL ) {
210                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
211                                 progname, base );
212                         exit( EXIT_FAILURE );
213                 }
214
215         } else if ( dbnum == -1 ) {
216                 be = &backends[dbnum=0];
217
218         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
219                 fprintf( stderr,
220                         "Database number selected via -n is out of range\n"
221                         "Must be in the range 1 to %d (number of databases in the config file)\n",
222                         nbackends );
223                 exit( EXIT_FAILURE );
224
225         } else {
226                 be = &backends[dbnum];
227         }
228
229 #ifdef CSRIMALLOC
230         mal_leaktrace(1);
231 #endif
232
233         slap_startup( be );
234 }
235
236 void slap_tool_destroy( void )
237 {
238         slap_shutdown( be );
239         slap_destroy();
240
241 #ifdef CSRIMALLOC
242         mal_dumpleaktrace( leakfile );
243 #endif
244 }