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