Debian Codegraph @

servers/slapd/back-monitor/backend.c

raw
1 /* backend.c - deals with backend subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2001-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
20 */
21
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <ac/string.h>
27
28 #include "slap.h"
29 #include "back-monitor.h"
30
31 /*
32 * initializes backend subentries
33 */
34 int
35 monitor_subsys_backend_init(
36 BackendDB *be,
37 monitor_subsys_t *ms
38 )
39 {
40 monitor_info_t *mi;
41 Entry *e_backend;
42 int i;
43 monitor_entry_t *mp;
44 monitor_subsys_t *ms_database;
45 BackendInfo *bi;
46
47 mi = ( monitor_info_t * )be->be_private;
48
49 ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
50 if ( ms_database == NULL ) {
51 Debug( LDAP_DEBUG_ANY,
52 "monitor_subsys_backend_init: "
53 "unable to get "
54 "\"" SLAPD_MONITOR_DATABASE_NAME "\" "
55 "subsystem\n" );
56 return -1;
57 }
58
59 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_backend ) ) {
60 Debug( LDAP_DEBUG_ANY,
61 "monitor_subsys_backend_init: "
62 "unable to get entry \"%s\"\n",
63 ms->mss_ndn.bv_val );
64 return( -1 );
65 }
66
67 i = -1;
68 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
69 char buf[ BACKMONITOR_BUFSIZE ];
70 BackendDB *be;
71 struct berval bv;
72 int j;
73 Entry *e;
74
75 i++;
76
77 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Backend %d", i );
78 bv.bv_val = buf;
79
80 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
81 mi->mi_oc_monitoredObject, NULL, NULL );
82
83 if ( e == NULL ) {
84 Debug( LDAP_DEBUG_ANY,
85 "monitor_subsys_backend_init: "
86 "unable to create entry \"cn=Backend %d,%s\"\n",
87 i, ms->mss_ndn.bv_val );
88 return( -1 );
89 }
90
91 ber_str2bv( bi->bi_type, 0, 0, &bv );
92 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
93 &bv, NULL );
94 attr_merge_normalize_one( e_backend, mi->mi_ad_monitoredInfo,
95 &bv, NULL );
96
97 attr_merge_normalize_one( e, mi->mi_ad_monitorRuntimeConfig,
98 bi->bi_cf_ocs == NULL ? (struct berval *)&slap_false_bv :
99 (struct berval *)&slap_true_bv, NULL );
100
101 if ( bi->bi_controls ) {
102 int j;
103
104 for ( j = 0; bi->bi_controls[ j ]; j++ ) {
105 ber_str2bv( bi->bi_controls[ j ], 0, 0, &bv );
106 attr_merge_one( e, slap_schema.si_ad_supportedControl,
107 &bv, &bv );
108 }
109 }
110
111 j = -1;
112 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
113 char buf[ SLAP_LDAPDN_MAXLEN ];
114 struct berval dn;
115
116 j++;
117
118 if ( be->bd_info != bi ) {
119 continue;
120 }
121
122 snprintf( buf, sizeof( buf ), "cn=Database %d,%s",
123 j, ms_database->mss_dn.bv_val );
124
125 ber_str2bv( buf, 0, 0, &dn );
126 attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso,
127 &dn, NULL );
128 }
129
130 mp = monitor_entrypriv_create();
131 if ( mp == NULL ) {
132 return -1;
133 }
134 e->e_private = ( void * )mp;
135 mp->mp_info = ms;
136 mp->mp_flags = ms->mss_flags | MONITOR_F_SUB;
137
138 if ( monitor_cache_add( mi, e, e_backend ) ) {
139 Debug( LDAP_DEBUG_ANY,
140 "monitor_subsys_backend_init: "
141 "unable to add entry \"cn=Backend %d,%s\"\n",
142 i,
143 ms->mss_ndn.bv_val );
144 return( -1 );
145 }
146 }
147
148 monitor_cache_release( mi, e_backend );
149
150 return( 0 );
151 }
152