This source file includes following definitions.
- cib_notify_send_one
- cib_notify_send
- attach_cib_generation
- cib_diff_notify
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <sys/param.h>
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <inttypes.h>
17
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <fcntl.h>
21
22 #include <time.h>
23
24 #include <glib.h>
25 #include <libxml/tree.h>
26
27 #include <crm/crm.h>
28 #include <crm/cib/internal.h>
29
30 #include <crm/common/xml.h>
31 #include <crm/common/remote_internal.h>
32 #include <pacemaker-based.h>
33
34 struct cib_notification_s {
35 const xmlNode *msg;
36 struct iovec *iov;
37 int32_t iov_size;
38 };
39
40 static void
41 cib_notify_send_one(gpointer key, gpointer value, gpointer user_data)
42 {
43 const char *type = NULL;
44 gboolean do_send = FALSE;
45 int rc = pcmk_rc_ok;
46
47 pcmk__client_t *client = value;
48 struct cib_notification_s *update = user_data;
49
50 if (client->ipcs == NULL && client->remote == NULL) {
51 crm_warn("Skipping client with NULL channel");
52 return;
53 }
54
55 type = crm_element_value(update->msg, PCMK__XA_SUBT);
56 CRM_LOG_ASSERT(type != NULL);
57
58 if (pcmk_is_set(client->flags, cib_notify_diff)
59 && pcmk__str_eq(type, PCMK__VALUE_CIB_DIFF_NOTIFY, pcmk__str_none)) {
60
61 do_send = TRUE;
62
63 } else if (pcmk_is_set(client->flags, cib_notify_confirm)
64 && pcmk__str_eq(type, PCMK__VALUE_CIB_UPDATE_CONFIRMATION,
65 pcmk__str_none)) {
66 do_send = TRUE;
67
68 } else if (pcmk_is_set(client->flags, cib_notify_pre)
69 && pcmk__str_eq(type, PCMK__VALUE_CIB_PRE_NOTIFY,
70 pcmk__str_none)) {
71 do_send = TRUE;
72
73 } else if (pcmk_is_set(client->flags, cib_notify_post)
74 && pcmk__str_eq(type, PCMK__VALUE_CIB_POST_NOTIFY,
75 pcmk__str_none)) {
76 do_send = TRUE;
77 }
78
79 if (do_send) {
80 switch (PCMK__CLIENT_TYPE(client)) {
81 case pcmk__client_ipc:
82 rc = pcmk__ipc_send_iov(client, update->iov,
83 crm_ipc_server_event);
84 if (rc != pcmk_rc_ok) {
85 crm_warn("Could not notify client %s: %s " CRM_XS " id=%s",
86 pcmk__client_name(client), pcmk_rc_str(rc),
87 client->id);
88 }
89 break;
90 #ifdef HAVE_GNUTLS_GNUTLS_H
91 case pcmk__client_tls:
92 #endif
93 case pcmk__client_tcp:
94 crm_debug("Sent %s notification to client %s (id %s)",
95 type, pcmk__client_name(client), client->id);
96 pcmk__remote_send_xml(client->remote, update->msg);
97 break;
98 default:
99 crm_err("Unknown transport for client %s "
100 CRM_XS " flags=%#016" PRIx64,
101 pcmk__client_name(client), client->flags);
102 }
103 }
104 }
105
106 static void
107 cib_notify_send(const xmlNode *xml)
108 {
109 struct iovec *iov;
110 struct cib_notification_s update;
111
112 ssize_t bytes = 0;
113 int rc = pcmk__ipc_prepare_iov(0, xml, 0, &iov, &bytes);
114
115 if (rc == pcmk_rc_ok) {
116 update.msg = xml;
117 update.iov = iov;
118 update.iov_size = bytes;
119 pcmk__foreach_ipc_client(cib_notify_send_one, &update);
120
121 } else {
122 crm_notice("Could not notify clients: %s " CRM_XS " rc=%d",
123 pcmk_rc_str(rc), rc);
124 }
125 pcmk_free_ipc_event(iov);
126 }
127
128 static void
129 attach_cib_generation(xmlNode *msg)
130 {
131 xmlNode *wrapper = pcmk__xe_create(msg, PCMK__XE_CIB_GENERATION);
132 xmlNode *generation = pcmk__xe_create(wrapper, PCMK__XE_GENERATION_TUPLE);
133
134 if (the_cib != NULL) {
135 pcmk__xe_copy_attrs(generation, the_cib, pcmk__xaf_none);
136 }
137 }
138
139 void
140 cib_diff_notify(const char *op, int result, const char *call_id,
141 const char *client_id, const char *client_name,
142 const char *origin, xmlNode *update, xmlNode *diff)
143 {
144 int add_updates = 0;
145 int add_epoch = 0;
146 int add_admin_epoch = 0;
147
148 int del_updates = 0;
149 int del_epoch = 0;
150 int del_admin_epoch = 0;
151
152 uint8_t log_level = LOG_TRACE;
153
154 xmlNode *update_msg = NULL;
155 xmlNode *wrapper = NULL;
156 const char *type = NULL;
157
158 if (diff == NULL) {
159 return;
160 }
161
162 if (result != pcmk_ok) {
163 log_level = LOG_WARNING;
164 }
165
166 cib_diff_version_details(diff, &add_admin_epoch, &add_epoch, &add_updates,
167 &del_admin_epoch, &del_epoch, &del_updates);
168
169 if ((add_admin_epoch != del_admin_epoch)
170 || (add_epoch != del_epoch)
171 || (add_updates != del_updates)) {
172
173 do_crm_log(log_level,
174 "Updated CIB generation %d.%d.%d to %d.%d.%d from client "
175 "%s%s%s (%s) (%s)",
176 del_admin_epoch, del_epoch, del_updates,
177 add_admin_epoch, add_epoch, add_updates,
178 client_name,
179 ((call_id != NULL)? " call " : ""), pcmk__s(call_id, ""),
180 pcmk__s(origin, "unspecified peer"), pcmk_strerror(result));
181
182 } else if ((add_admin_epoch != 0)
183 || (add_epoch != 0)
184 || (add_updates != 0)) {
185
186 do_crm_log(log_level,
187 "Local-only change to CIB generation %d.%d.%d from client "
188 "%s%s%s (%s) (%s)",
189 add_admin_epoch, add_epoch, add_updates,
190 client_name,
191 ((call_id != NULL)? " call " : ""), pcmk__s(call_id, ""),
192 pcmk__s(origin, "unspecified peer"), pcmk_strerror(result));
193 }
194
195 update_msg = pcmk__xe_create(NULL, PCMK__XE_NOTIFY);
196
197 crm_xml_add(update_msg, PCMK__XA_T, PCMK__VALUE_CIB_NOTIFY);
198 crm_xml_add(update_msg, PCMK__XA_SUBT, PCMK__VALUE_CIB_DIFF_NOTIFY);
199 crm_xml_add(update_msg, PCMK__XA_CIB_OP, op);
200 crm_xml_add(update_msg, PCMK__XA_CIB_CLIENTID, client_id);
201 crm_xml_add(update_msg, PCMK__XA_CIB_CLIENTNAME, client_name);
202 crm_xml_add(update_msg, PCMK__XA_CIB_CALLID, call_id);
203 crm_xml_add(update_msg, PCMK__XA_SRC, origin);
204 crm_xml_add_int(update_msg, PCMK__XA_CIB_RC, result);
205
206
207 if (update != NULL) {
208 type = (const char *) update->name;
209 crm_trace("Setting type to update->name: %s", type);
210 } else {
211 type = (const char *) diff->name;
212 crm_trace("Setting type to new_obj->name: %s", type);
213 }
214
215
216 crm_xml_add(update_msg, PCMK__XA_CIB_OBJECT, pcmk__xe_id(diff));
217 crm_xml_add(update_msg, PCMK__XA_CIB_OBJECT_TYPE, type);
218 attach_cib_generation(update_msg);
219
220
221 if (update != NULL) {
222 wrapper = pcmk__xe_create(update_msg, PCMK__XE_CIB_UPDATE);
223 pcmk__xml_copy(wrapper, update);
224 }
225
226 wrapper = pcmk__xe_create(update_msg, PCMK__XE_CIB_UPDATE_RESULT);
227 pcmk__xml_copy(wrapper, diff);
228
229 crm_log_xml_trace(update_msg, "diff-notify");
230 cib_notify_send(update_msg);
231 free_xml(update_msg);
232 }