]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/startup.c
Fix maxDeref directive
[openldap] / servers / slapd / back-bdb2 / startup.c
1 /* startup.c - startup/shutdown bdb2 backend */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9 #include <ac/unistd.h>
10
11 #ifdef HAVE_DIRECT_H
12 #include <direct.h>
13 #endif
14
15 #include "ldap_defaults.h"
16 #include "slap.h"
17 #include "back-bdb2.h"
18
19 #include "db.h"
20
21 static void remove_old_locks( char *home );
22
23
24 static void
25 bdb2i_db_errcall( const char *prefix, char *message )
26 {
27         Debug( LDAP_DEBUG_ANY, "bdb2_db_errcall(): %s %s", prefix, message, 0 );
28 }
29
30
31 /*  startup/shutdown per backend type  */
32
33 static int
34 bdb2i_back_startup_internal(
35     BackendInfo *bi
36 )
37 {
38         struct ldbtype  *lty = (struct ldbtype *) bi->bi_private;
39         int             envFlags;
40         int             err      = 0;
41         char            *home;
42
43         /*  set the flags for a full-feldged transaction schema  */
44         envFlags = ( DB_CREATE | DB_THREAD | DB_INIT_TXN | DB_INIT_LOG |
45                                         DB_INIT_LOCK | DB_INIT_MPOOL );
46
47         /*  make sure, dbhome is an absolute path  */
48         if ( *lty->lty_dbhome != *LDAP_DIRSEP ) {
49                 char   cwd[MAXPATHLEN];
50
51                 (void) getcwd( cwd, MAXPATHLEN );
52                 sprintf( cwd, "%s" LDAP_DIRSEP "%s",
53                         cwd, lty->lty_dbhome );
54                 free( lty->lty_dbhome );
55                 lty->lty_dbhome = ch_strdup( cwd );
56
57         }
58         home = lty->lty_dbhome;
59
60         /*  general initialization of the environment  */
61         memset( &bdb2i_dbEnv, 0, sizeof( DB_ENV ));
62         bdb2i_dbEnv.db_errcall = bdb2i_db_errcall;
63         bdb2i_dbEnv.db_errpfx  = "==>";
64
65         /*  initialize the lock subsystem  */
66         bdb2i_dbEnv.lk_max     = 0;
67
68         /*  remove old locking tables  */
69         remove_old_locks( home );
70
71         /*  initialize the mpool subsystem  */
72         bdb2i_dbEnv.mp_size   = lty->lty_mpsize;
73
74         /*  now do the db_appinit  */
75         if ( ( err = db_appinit( home, NULL, &bdb2i_dbEnv, envFlags )) ) {
76                 char  error[BUFSIZ];
77
78                 if ( err < 0 ) sprintf( error, "%ld\n", (long) err );
79                 else           sprintf( error, "%s\n", strerror( err ));
80
81                 Debug( LDAP_DEBUG_ANY,
82                                 "bdb2i_back_startup(): FATAL error in db_appinit() : %s\n",
83                                 error, 0, 0 );
84                 return( 1 );
85
86         }
87
88         return 0;
89 }
90
91
92 static int
93 bdb2i_back_shutdown_internal(
94     BackendInfo *bi
95 )
96 {
97         struct ldbtype  *lty = (struct ldbtype *) bi->bi_private;
98         int              err;
99
100         /*  remove old locking tables  */
101         bdb2i_dbEnv.db_errpfx  = "bdb2i_back_shutdown(): lock_unlink:";
102         if ( ( err = lock_unlink( NULL, 1, &bdb2i_dbEnv )) != 0 )
103                 Debug( LDAP_DEBUG_ANY, "bdb2i_back_shutdown(): lock_unlink: %s\n",
104                                         strerror( err ), 0, 0);
105
106         /*  remove old memory pool  */
107         bdb2i_dbEnv.db_errpfx  = "bdb2i_back_shutdown(): memp_unlink:";
108         if ( ( err = memp_unlink( NULL, 1, &bdb2i_dbEnv )) != 0 )
109                 Debug( LDAP_DEBUG_ANY, "bdb2i_back_shutdown(): memp_unlink: %s\n",
110                                         strerror( err ), 0, 0);
111
112         (void) db_appexit( &bdb2i_dbEnv );
113
114         return( 0 );
115 }
116
117
118 int
119 bdb2i_back_startup(
120     BackendInfo *bi
121 )
122 {
123         struct timeval  time1;
124         int             ret;
125
126         bdb2i_start_timing( bi, &time1 );
127
128         ret = bdb2i_back_startup_internal( bi );
129         bdb2i_stop_timing( bi, time1, "BE-START", NULL, NULL );
130
131         return( ret );
132 }
133
134
135 int
136 bdb2i_back_shutdown(
137     BackendInfo *bi
138 )
139 {
140         struct timeval  time1;
141         int             ret;
142
143         bdb2i_start_timing( bi, &time1 );
144
145         ret = bdb2i_back_shutdown_internal( bi );
146         bdb2i_stop_timing( bi, time1, "BE-SHUTDOWN", NULL, NULL );
147
148         return( ret );
149 }
150
151
152 /*  startup/shutdown per backend database  */
153
154 static int
155 bdb2i_back_db_startup_internal(
156     BackendDB   *be
157 )
158 {
159         struct ldbminfo  *li = (struct ldbminfo *) be->be_private;
160
161         /*  if the data directory is not an absolute path, have it relative
162         to the current working directory (which should not be configured !)  */
163         if ( *li->li_directory != *LDAP_DIRSEP ) {
164                 char   cwd[MAXPATHLEN];
165
166                 (void) getcwd( cwd, MAXPATHLEN );
167                 sprintf( cwd, "%s" LDAP_DIRSEP "%s",
168                         cwd, li->li_directory );
169
170                 free( li->li_directory );
171                 li->li_directory = ch_strdup( cwd );
172
173         }
174
175         /*  if there are more index files, add them to the DB file list  */
176         if ( bdb2i_check_additional_attr_index( li ) != 0 )
177                 return 1;
178
179         /*  now open all DB files  */
180         if ( bdb2i_txn_open_files( be ) != 0 )
181                 return 1;
182
183         return 0;
184 }
185
186
187 static int
188 bdb2i_back_db_shutdown_internal(
189     BackendDB   *be
190 )
191 {
192         return 0;
193 }
194
195
196 int
197 bdb2_back_db_startup(
198     BackendDB   *be
199 )
200 {
201         struct timeval  time1;
202         int             ret;
203
204         bdb2i_start_timing( be->bd_info, &time1 );
205
206         ret = bdb2i_back_db_startup_internal( be );
207         bdb2i_stop_timing( be->bd_info, time1, "DB-START", NULL, NULL );
208
209         return( ret );
210 }
211
212
213 int
214 bdb2_back_db_shutdown(
215     BackendDB   *be
216 )
217 {
218         struct timeval  time1;
219         int             ret;
220
221         bdb2i_start_timing( be->bd_info, &time1 );
222
223         ret = bdb2i_back_db_shutdown_internal( be );
224         bdb2i_stop_timing( be->bd_info, time1, "DB-SHUTDOWN", NULL, NULL );
225
226         return( ret );
227 }
228
229
230 static void
231 remove_old_locks( char *home )
232 {
233         DB_ENV  dbEnv;
234         int     err;
235
236         memset( &dbEnv, 0, sizeof( DB_ENV ));
237         dbEnv.db_errcall = bdb2i_db_errcall;
238         dbEnv.db_errpfx  = "remove_old_locks(): db_appinit:";
239         dbEnv.lk_max     = 0;
240
241         if ( ( err = db_appinit( home, NULL, &dbEnv, 0 )) != 0 )
242                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): db_appinit: %s\n",
243                                         strerror( err ), 0, 0);
244
245         dbEnv.db_errpfx  = "remove_old_locks(): lock_unlink:";
246         if ( ( err = lock_unlink( NULL, 1, &dbEnv )) != 0 )
247                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): lock_unlink: %s\n",
248                                         strerror( err ), 0, 0);
249
250         dbEnv.db_errpfx  = "remove_old_locks(): db_appexit:";
251         if ( ( err = db_appexit( &dbEnv )) != 0 )
252                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): db_appexit: %s\n",
253                                         strerror( err ), 0, 0);
254
255 }
256
257