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