]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/startup.c
neutralize previous add.c change until bdb2_back_entry_release_rw() is provided
[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 "ldapconfig.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( 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 != *DEFAULT_DIRSEP ) {
49                 char   cwd[MAXPATHLEN];
50
51                 (void) getcwd( cwd, MAXPATHLEN );
52                 sprintf( cwd, "%s%s%s", cwd, DEFAULT_DIRSEP, lty->lty_dbhome );
53                 free( lty->lty_dbhome );
54                 lty->lty_dbhome = ch_strdup( cwd );
55
56         }
57         home = lty->lty_dbhome;
58
59         /*  general initialization of the environment  */
60         memset( &bdb2i_dbEnv, 0, sizeof( DB_ENV ));
61         bdb2i_dbEnv.db_errcall = bdb2i_db_errcall;
62         bdb2i_dbEnv.db_errpfx  = "==>";
63
64         /*  initialize the lock subsystem  */
65         bdb2i_dbEnv.lk_max     = 0;
66
67         /*  remove old locking tables  */
68         remove_old_locks( home );
69
70         /*  initialize the mpool subsystem  */
71         bdb2i_dbEnv.mp_size   = lty->lty_mpsize;
72
73         /*  now do the db_appinit  */
74         if ( ( err = db_appinit( home, NULL, &bdb2i_dbEnv, envFlags )) ) {
75                 char  error[BUFSIZ];
76
77                 if ( err < 0 ) sprintf( error, "%ld\n", (long) err );
78                 else           sprintf( error, "%s\n", strerror( err ));
79
80                 Debug( LDAP_DEBUG_ANY,
81                                 "bdb2i_back_startup(): FATAL error in db_appinit() : %s\n",
82                                 error, 0, 0 );
83                 return( 1 );
84
85         }
86
87         return 0;
88 }
89
90
91 static int
92 bdb2i_back_shutdown_internal(
93     BackendInfo *bi
94 )
95 {
96         struct ldbtype  *lty = (struct ldbtype *) bi->bi_private;
97         int              err;
98
99         /*  remove old locking tables  */
100         bdb2i_dbEnv.db_errpfx  = "bdb2i_back_shutdown(): lock_unlink:";
101         if ( ( err = lock_unlink( NULL, 1, &bdb2i_dbEnv )) != 0 )
102                 Debug( LDAP_DEBUG_ANY, "bdb2i_back_shutdown(): lock_unlink: %s\n",
103                                         strerror( err ), 0, 0);
104
105         /*  remove old memory pool  */
106         bdb2i_dbEnv.db_errpfx  = "bdb2i_back_shutdown(): memp_unlink:";
107         if ( ( err = memp_unlink( NULL, 1, &bdb2i_dbEnv )) != 0 )
108                 Debug( LDAP_DEBUG_ANY, "bdb2i_back_shutdown(): memp_unlink: %s\n",
109                                         strerror( err ), 0, 0);
110
111         (void) db_appexit( &bdb2i_dbEnv );
112
113         return( 0 );
114 }
115
116
117 int
118 bdb2i_back_startup(
119     BackendInfo *bi
120 )
121 {
122         struct timeval  time1;
123         int             ret;
124
125         bdb2i_start_timing( bi, &time1 );
126
127         ret = bdb2i_back_startup_internal( bi );
128         bdb2i_stop_timing( bi, time1, "BE-START", NULL, NULL );
129
130         return( ret );
131 }
132
133
134 int
135 bdb2i_back_shutdown(
136     BackendInfo *bi
137 )
138 {
139         struct timeval  time1;
140         int             ret;
141
142         bdb2i_start_timing( bi, &time1 );
143
144         ret = bdb2i_back_shutdown_internal( bi );
145         bdb2i_stop_timing( bi, time1, "BE-SHUTDOWN", NULL, NULL );
146
147         return( ret );
148 }
149
150
151 /*  startup/shutdown per backend database  */
152
153 static int
154 bdb2i_back_db_startup_internal(
155     BackendDB   *be
156 )
157 {
158         struct ldbminfo  *li = (struct ldbminfo *) be->be_private;
159
160         /*  if the data directory is not an absolute path, have it relative
161         to the current working directory (which should not be configured !)  */
162         if ( *li->li_directory != *DEFAULT_DIRSEP ) {
163                 char   cwd[MAXPATHLEN];
164
165                 (void) getcwd( cwd, MAXPATHLEN );
166                 sprintf( cwd, "%s%s%s", cwd, DEFAULT_DIRSEP, li->li_directory );
167                 free( li->li_directory );
168                 li->li_directory = ch_strdup( cwd );
169
170         }
171
172         /*  if there are more index files, add them to the DB file list  */
173         if ( bdb2i_check_additional_attr_index( li ) != 0 )
174                 return 1;
175
176         /*  now open all DB files  */
177         if ( bdb2i_txn_open_files( be ) != 0 )
178                 return 1;
179
180         return 0;
181 }
182
183
184 static int
185 bdb2i_back_db_shutdown_internal(
186     BackendDB   *be
187 )
188 {
189         return 0;
190 }
191
192
193 int
194 bdb2_back_db_startup(
195     BackendDB   *be
196 )
197 {
198         struct timeval  time1;
199         int             ret;
200
201         bdb2i_start_timing( be->bd_info, &time1 );
202
203         ret = bdb2i_back_db_startup_internal( be );
204         bdb2i_stop_timing( be->bd_info, time1, "DB-START", NULL, NULL );
205
206         return( ret );
207 }
208
209
210 int
211 bdb2_back_db_shutdown(
212     BackendDB   *be
213 )
214 {
215         struct timeval  time1;
216         int             ret;
217
218         bdb2i_start_timing( be->bd_info, &time1 );
219
220         ret = bdb2i_back_db_shutdown_internal( be );
221         bdb2i_stop_timing( be->bd_info, time1, "DB-SHUTDOWN", NULL, NULL );
222
223         return( ret );
224 }
225
226
227 static void
228 remove_old_locks( char *home )
229 {
230         DB_ENV  dbEnv;
231         int     err;
232
233         memset( &dbEnv, 0, sizeof( DB_ENV ));
234         dbEnv.db_errcall = stderr;
235         dbEnv.db_errpfx  = "remove_old_locks(): db_appinit:";
236         dbEnv.lk_max     = 0;
237
238         if ( ( err = db_appinit( home, NULL, &dbEnv, 0 )) != 0 )
239                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): db_appinit: %s\n",
240                                         strerror( err ), 0, 0);
241
242         dbEnv.db_errpfx  = "remove_old_locks(): lock_unlink:";
243         if ( ( err = lock_unlink( NULL, 1, &dbEnv )) != 0 )
244                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): lock_unlink: %s\n",
245                                         strerror( err ), 0, 0);
246
247         dbEnv.db_errpfx  = "remove_old_locks(): db_appexit:";
248         if ( ( err = db_appexit( &dbEnv )) != 0 )
249                 Debug( LDAP_DEBUG_ANY, "remove_old_locks(): db_appexit: %s\n",
250                                         strerror( err ), 0, 0);
251
252 }
253
254