+int backend_init(void)
+{
+ int rc = -1;
+
+ if((nBackendInfo != 0) || (backendInfo != NULL)) {
+ /* already initialized */
+ Debug( LDAP_DEBUG_ANY,
+ "backend_init: already initialized.\n", 0, 0, 0 );
+ return -1;
+ }
+
+ for( ;
+ binfo[nBackendInfo].bi_type != NULL;
+ nBackendInfo++ )
+ {
+ rc = binfo[nBackendInfo].bi_init(
+ &binfo[nBackendInfo] );
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_init: initialized for type \"%s\"\n",
+ binfo[nBackendInfo].bi_type, 0, 0 );
+
+ /* destroy those we've already inited */
+ for( nBackendInfo--;
+ nBackendInfo >= 0 ;
+ nBackendInfo-- )
+ {
+ if ( binfo[nBackendInfo].bi_destroy ) {
+ binfo[nBackendInfo].bi_destroy(
+ &binfo[nBackendInfo] );
+ }
+ }
+ return rc;
+ }
+ }
+
+ if ( nBackendInfo > 0) {
+ backendInfo = binfo;
+ return 0;
+ }
+
+ Debug( LDAP_DEBUG_ANY,
+ "backend_init: failed\n",
+ 0, 0, 0 );
+
+ return rc;
+}
+
+int backend_startup(int n)
+{
+ int i;
+ int rc = 0;
+
+ if( ! ( nBackendDB > 0 ) ) {
+ /* no databases */
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: %d databases to startup.\n",
+ nBackendDB, 0, 0 );
+ return 1;
+ }
+
+ if(n >= 0) {
+ /* startup a specific backend database */
+ Debug( LDAP_DEBUG_TRACE,
+ "backend_startup: starting database %d\n",
+ n, 0, 0 );
+
+ /* make sure, n does not exceed the number of backend databases */
+ if ( n >= nbackends ) {
+
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: database number %d exceeding maximum (%d)\n",
+ n, nbackends, 0 );
+ return 1;
+ }
+
+ if ( backendDB[n].bd_info->bi_open ) {
+ rc = backendDB[n].bd_info->bi_open(
+ backendDB[n].bd_info );
+ }
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: bi_open failed!\n",
+ 0, 0, 0 );
+ return rc;
+ }
+
+ if ( backendDB[n].bd_info->bi_db_open ) {
+ rc = backendDB[n].bd_info->bi_db_open(
+ &backendDB[n] );
+ }
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: bi_db_open failed!\n",
+ 0, 0, 0 );
+ return rc;
+ }
+
+ return rc;
+ }
+
+ /* open each backend type */
+ for( i = 0; i < nBackendInfo; i++ ) {
+ if( backendInfo[i].bi_nDB == 0) {
+ /* no database of this type, don't open */
+ continue;
+ }
+
+ if( backendInfo[i].bi_open ) {
+ rc = backendInfo[i].bi_open(
+ &backendInfo[i] );
+ }
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: bi_open %d failed!\n",
+ i, 0, 0 );
+ return rc;
+ }
+ }
+
+ /* open each backend database */
+ for( i = 0; i < nBackendDB; i++ ) {
+ if ( backendDB[i].bd_info->bi_db_open ) {
+ rc = backendDB[i].bd_info->bi_db_open(
+ &backendDB[i] );
+ }
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: bi_db_open %d failed!\n",
+ i, 0, 0 );
+ return rc;
+ }
+ }
+
+ return rc;
+}
+
+int backend_shutdown(int n)
+{
+ int i;
+ int rc = 0;
+
+ if(n >= 0) {
+ /* shutdown a specific backend database */
+
+ /* make sure, n does not exceed the number of backend databases */
+ if ( n >= nbackends ) {
+
+ Debug( LDAP_DEBUG_ANY,
+ "backend_startup: database number %d exceeding maximum (%d)\n",
+ n, nbackends, 0 );
+ return 1;
+ }
+
+ if ( backendDB[n].bd_info->bi_nDB == 0 ) {
+ /* no database of this type, we never opened it */
+ return 0;
+ }
+
+ if ( backendDB[n].bd_info->bi_db_close ) {
+ backendDB[n].bd_info->bi_db_close(
+ &backendDB[n] );
+ }
+
+ if( backendDB[n].bd_info->bi_close ) {
+ backendDB[n].bd_info->bi_close(
+ backendDB[n].bd_info );
+ }
+
+ return 0;
+ }
+
+ /* close each backend database */
+ for( i = 0; i < nBackendDB; i++ ) {
+ BackendInfo *bi;
+
+ if ( backendDB[i].bd_info->bi_db_close ) {
+ backendDB[i].bd_info->bi_db_close(
+ &backendDB[i] );
+ }
+
+ if(rc != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "backend_close: bi_close %s failed!\n",
+ bi->bi_type, 0, 0 );
+ }
+ }
+
+ /* close each backend type */
+ for( i = 0; i < nBackendInfo; i++ ) {
+ if( backendInfo[i].bi_nDB == 0 ) {
+ /* no database of this type */
+ continue;
+ }
+
+ if( backendInfo[i].bi_close ) {
+ backendInfo[i].bi_close(
+ &backendInfo[i] );
+ }
+ }
+
+ return 0;
+}
+
+int backend_destroy(void)
+{
+ int i;
+
+ /* destroy each backend database */
+ for( i = 0; i < nBackendDB; i++ ) {
+ if ( backendDB[i].bd_info->bi_db_destroy ) {
+ backendDB[i].bd_info->bi_db_destroy(
+ &backendDB[i] );
+ }
+ }
+
+ /* destroy each backend type */
+ for( i = 0; i < nBackendInfo; i++ ) {
+ if( backendInfo[i].bi_destroy ) {
+ backendInfo[i].bi_destroy(
+ &backendInfo[i] );
+ }
+ }
+
+ return 0;
+}
+
+BackendInfo* backend_info(char *type)
+{
+ int i;
+
+ /* search for the backend type */
+ for( i = 0; i < nBackendInfo; i++ ) {
+ if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
+ return &backendInfo[i];
+ }
+ }
+
+ return NULL;
+}
+
+
+BackendDB *
+backend_db_init(