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