]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
96439a715d97c8da3c02f5dc074e4e38be6bdd83
[openldap] / servers / slapd / back-monitor / database.c
1 /* database.c - deals with database subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "back-monitor.h"
40
41 int
42 monitor_subsys_database_init(
43         BackendDB       *be
44 )
45 {
46         struct monitorinfo      *mi;
47         Entry                   *e, *e_database, *e_tmp;
48         int                     i;
49         struct monitorentrypriv *mp;
50         AttributeDescription    *ad_nc = slap_schema.si_ad_namingContexts;
51         AttributeDescription    *ad_mc = slap_schema.si_ad_monitorContext;
52         AttributeDescription    *ad_seeAlso = NULL;
53         const char              *text = NULL;
54
55         assert( be != NULL );
56         assert( monitor_ad_desc != NULL );
57
58         mi = ( struct monitorinfo * )be->be_private;
59
60         if ( monitor_cache_get( mi, 
61                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
62                                 &e_database ) ) {
63 #ifdef NEW_LOGGING
64                 LDAP_LOG( OPERATION, CRIT,
65                         "monitor_subsys_database_init: "
66                         "unable to get entry '%s'\n",
67                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
68 #else
69                 Debug( LDAP_DEBUG_ANY,
70                         "monitor_subsys_database_init: "
71                         "unable to get entry '%s'\n%s%s",
72                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
73                         "", "" );
74 #endif
75                 return( -1 );
76         }
77
78         if ( slap_str2ad( "seeAlso", &ad_seeAlso, &text ) != LDAP_SUCCESS ) {
79                 return( -1 );
80         }
81
82         e_tmp = NULL;
83         for ( i = nBackendDB; i--; ) {
84                 char buf[1024];
85                 int j;
86
87                 be = &backendDB[i];
88
89                 /* Subordinates are not exposed as their own naming context */
90                 if ( be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE ) {
91                         continue;
92                 }
93
94                 snprintf( buf, sizeof( buf ),
95                                 "dn: cn=Database %d,%s\n"
96                                 SLAPD_MONITOR_OBJECTCLASSES
97                                 "cn: Database %d\n"
98                                 "description: %s",
99                                 i,
100                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
101                                 i,
102                                 be->bd_info->bi_type );
103                 
104                 e = str2entry( buf );
105                 if ( e == NULL ) {
106 #ifdef NEW_LOGGING
107                         LDAP_LOG( OPERATION, CRIT,
108                                 "monitor_subsys_database_init: "
109                                 "unable to create entry 'cn=Database %d,%s'\n",
110                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
111 #else
112                         Debug( LDAP_DEBUG_ANY,
113                                 "monitor_subsys_database_init: "
114                                 "unable to create entry 'cn=Database %d,%s'\n%s",
115                                 i, 
116                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
117                                 "" );
118 #endif
119                         return( -1 );
120                 }
121                 
122                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
123                         attr_merge( e, ad_mc, be->be_suffix );
124                         attr_merge( e_database, ad_mc, be->be_suffix );
125                 } else {
126                         attr_merge( e, ad_nc, be->be_suffix );
127                         attr_merge( e_database, ad_nc, be->be_suffix );
128                 }
129
130                 for ( j = nBackendInfo; j--; ) {
131                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
132                                 struct berval           bv;
133
134                                 snprintf( buf, sizeof( buf ), 
135                                         "cn=Backend %d,%s", 
136                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
137                                 bv.bv_val = buf;
138                                 bv.bv_len = strlen( buf );
139                                 attr_merge_one( e, ad_seeAlso, &bv );
140                                 break;
141                         }
142                 }
143                 /* we must find it! */
144                 assert( j >= 0 );
145
146                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
147                 e->e_private = ( void * )mp;
148                 mp->mp_next = e_tmp;
149                 mp->mp_children = NULL;
150                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
151                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
152                         | MONITOR_F_SUB;
153
154                 if ( monitor_cache_add( mi, e ) ) {
155 #ifdef NEW_LOGGING
156                         LDAP_LOG( OPERATION, CRIT,
157                                 "monitor_subsys_database_init: "
158                                 "unable to add entry 'cn=Database %d,%s'\n",
159                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
160 #else
161                         Debug( LDAP_DEBUG_ANY,
162                                 "monitor_subsys_database_init: "
163                                 "unable to add entry 'cn=Database %d,%s'\n",
164                                 i, 
165                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
166                                 0 );
167 #endif
168                         return( -1 );
169                 }
170
171                 e_tmp = e;
172         }
173         
174         mp = ( struct monitorentrypriv * )e_database->e_private;
175         mp->mp_children = e_tmp;
176
177         monitor_cache_release( mi, e_database );
178
179         return( 0 );
180 }
181