]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
Plug memory leak
[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 "slapi-plugin.h"
40 #include "back-monitor.h"
41
42 #if defined(LDAP_SLAPI)
43 static int monitor_back_add_plugin( Backend *be, Entry *e );
44 #endif /* defined(LDAP_SLAPI) */
45
46 int
47 monitor_subsys_database_init(
48         BackendDB       *be
49 )
50 {
51         struct monitorinfo      *mi;
52         Entry                   *e, *e_database, *e_tmp;
53         int                     i;
54         struct monitorentrypriv *mp;
55         AttributeDescription    *ad_nc = slap_schema.si_ad_namingContexts;
56         AttributeDescription    *ad_mc = slap_schema.si_ad_monitorContext;
57         AttributeDescription    *ad_seeAlso = NULL;
58         const char              *text = NULL;
59
60         assert( be != NULL );
61         assert( monitor_ad_desc != NULL );
62
63         mi = ( struct monitorinfo * )be->be_private;
64
65         if ( monitor_cache_get( mi, 
66                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
67                                 &e_database ) ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( OPERATION, CRIT,
70                         "monitor_subsys_database_init: "
71                         "unable to get entry '%s'\n",
72                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
73 #else
74                 Debug( LDAP_DEBUG_ANY,
75                         "monitor_subsys_database_init: "
76                         "unable to get entry '%s'\n%s%s",
77                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
78                         "", "" );
79 #endif
80                 return( -1 );
81         }
82
83         if ( slap_str2ad( "seeAlso", &ad_seeAlso, &text ) != LDAP_SUCCESS ) {
84 #ifdef NEW_LOGGING
85                 LDAP_LOG( OPERATION, CRIT,
86                         "monitor_subsys_database_init: "
87                         "unable to find 'seeAlso' attribute description\n",
88                         0, 0, 0 );
89 #else
90                 Debug( LDAP_DEBUG_ANY,
91                         "monitor_subsys_database_init: "
92                         "unable to find 'seeAlso' attribute description\n",
93                         0, 0, 0 );
94 #endif
95                 return( -1 );
96         }
97
98         e_tmp = NULL;
99         for ( i = nBackendDB; i--; ) {
100                 char buf[1024];
101                 int j;
102
103                 be = &backendDB[i];
104
105                 /* Subordinates are not exposed as their own naming context */
106                 if ( be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE ) {
107                         continue;
108                 }
109
110                 snprintf( buf, sizeof( buf ),
111                                 "dn: cn=Database %d,%s\n"
112                                 SLAPD_MONITOR_OBJECTCLASSES
113                                 "cn: Database %d\n"
114                                 "description: %s",
115                                 i,
116                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
117                                 i,
118                                 be->bd_info->bi_type );
119                 
120                 e = str2entry( buf );
121                 if ( e == NULL ) {
122 #ifdef NEW_LOGGING
123                         LDAP_LOG( OPERATION, CRIT,
124                                 "monitor_subsys_database_init: "
125                                 "unable to create entry 'cn=Database %d,%s'\n",
126                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
127 #else
128                         Debug( LDAP_DEBUG_ANY,
129                                 "monitor_subsys_database_init: "
130                                 "unable to create entry 'cn=Database %d,%s'\n%s",
131                                 i, 
132                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
133                                 "" );
134 #endif
135                         return( -1 );
136                 }
137                 
138                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
139                         attr_merge( e, ad_mc, be->be_suffix );
140                         attr_merge( e_database, ad_mc, be->be_suffix );
141                 } else {
142                         attr_merge( e, ad_nc, be->be_suffix );
143                         attr_merge( e_database, ad_nc, be->be_suffix );
144                 }
145
146                 for ( j = nBackendInfo; j--; ) {
147                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
148                                 struct berval           bv;
149
150                                 snprintf( buf, sizeof( buf ), 
151                                         "cn=Backend %d,%s", 
152                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
153                                 bv.bv_val = buf;
154                                 bv.bv_len = strlen( buf );
155                                 attr_merge_one( e, ad_seeAlso, &bv );
156                                 break;
157                         }
158                 }
159                 /* we must find it! */
160                 assert( j >= 0 );
161
162                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
163                 e->e_private = ( void * )mp;
164                 mp->mp_next = e_tmp;
165                 mp->mp_children = NULL;
166                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
167                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
168                         | MONITOR_F_SUB;
169
170                 if ( monitor_cache_add( mi, e ) ) {
171 #ifdef NEW_LOGGING
172                         LDAP_LOG( OPERATION, CRIT,
173                                 "monitor_subsys_database_init: "
174                                 "unable to add entry 'cn=Database %d,%s'\n",
175                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
176 #else
177                         Debug( LDAP_DEBUG_ANY,
178                                 "monitor_subsys_database_init: "
179                                 "unable to add entry 'cn=Database %d,%s'\n",
180                                 i, 
181                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
182                                 0 );
183 #endif
184                         return( -1 );
185                 }
186
187 #if defined(LDAP_SLAPI)
188                 monitor_back_add_plugin( be, e );
189 #endif /* defined(LDAP_SLAPI) */
190
191                 e_tmp = e;
192         }
193         
194         mp = ( struct monitorentrypriv * )e_database->e_private;
195         mp->mp_children = e_tmp;
196
197         monitor_cache_release( mi, e_database );
198
199         return( 0 );
200 }
201
202 #if defined(LDAP_SLAPI)
203 static int
204 monitor_back_add_plugin( Backend *be, Entry *e_database )
205 {
206         Slapi_PBlock            *pCurrentPB; 
207         int                     i, rc = LDAP_SUCCESS;
208
209         if ( slapi_x_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
210                 /*
211                  * LDAP_OTHER is returned if no plugins are installed
212                  */
213                 rc = LDAP_OTHER;
214                 goto done;
215         }
216
217         i = 0;
218         do {
219                 Slapi_PluginDesc        *srchdesc;
220                 char                    buf[1024];
221                 struct berval           bv;
222
223                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
224                                 &srchdesc );
225                 if ( rc != LDAP_SUCCESS ) {
226                         goto done;
227                 }
228
229                 snprintf( buf, sizeof(buf),
230                                 "plugin %d name: %s; "
231                                 "vendor: %s; "
232                                 "version: %s; "
233                                 "description: %s", 
234                                 i,
235                                 srchdesc->spd_id,
236                                 srchdesc->spd_vendor,
237                                 srchdesc->spd_version,
238                                 srchdesc->spd_description );
239
240                 bv.bv_val = buf;
241                 bv.bv_len = strlen( buf );
242                 attr_merge_one( e_database, monitor_ad_desc, &bv );
243
244                 i++;
245
246         } while ( ( slapi_x_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
247                         && ( pCurrentPB != NULL ) );
248
249 done:
250         return rc;
251 }
252 #endif /* defined(LDAP_SLAPI) */