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