This source file includes following definitions.
- cib_native_perform_op_delegate
- cib_native_dispatch_internal
- cib_native_destroy
- cib_native_signoff
- cib_native_signon
- cib_native_free
- cib_native_register_notification
- cib_native_set_connection_dnotify
- cib_native_client_id
- cib_native_new
1
2
3
4
5
6
7
8
9
10
11 #include <crm_internal.h>
12
13 #include <errno.h>
14 #include <crm_internal.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <string.h>
20
21 #include <glib.h>
22
23 #include <crm/crm.h>
24 #include <crm/cib/internal.h>
25
26 #include <crm/common/mainloop.h>
27 #include <crm/common/xml.h>
28
29 typedef struct cib_native_opaque_s {
30 char *token;
31 crm_ipc_t *ipc;
32 void (*dnotify_fn) (gpointer user_data);
33 mainloop_io_t *source;
34 } cib_native_opaque_t;
35
36 static int
37 cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
38 const char *section, xmlNode *data,
39 xmlNode **output_data, int call_options,
40 const char *user_name)
41 {
42 int rc = pcmk_ok;
43 int reply_id = 0;
44 enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
45
46 xmlNode *op_msg = NULL;
47 xmlNode *op_reply = NULL;
48
49 cib_native_opaque_t *native = cib->variant_opaque;
50
51 if (cib->state == cib_disconnected) {
52 return -ENOTCONN;
53 }
54
55 if (output_data != NULL) {
56 *output_data = NULL;
57 }
58
59 if (op == NULL) {
60 crm_err("No operation specified");
61 return -EINVAL;
62 }
63
64 if (call_options & cib_sync_call) {
65 pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
66 }
67
68 rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
69 NULL, &op_msg);
70 if (rc != pcmk_ok) {
71 return rc;
72 }
73
74 if (pcmk_is_set(call_options, cib_transaction)) {
75 rc = cib__extend_transaction(cib, op_msg);
76 goto done;
77 }
78
79 crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
80 rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
81
82 if (rc < 0) {
83 crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
84 cib->call_timeout, pcmk_strerror(rc), rc);
85 rc = -ECOMM;
86 goto done;
87 }
88
89 crm_log_xml_trace(op_reply, "Reply");
90
91 if (!(call_options & cib_sync_call)) {
92 crm_trace("Async call, returning %d", cib->call_id);
93 CRM_CHECK(cib->call_id != 0,
94 rc = -ENOMSG; goto done);
95 rc = cib->call_id;
96 goto done;
97 }
98
99 rc = pcmk_ok;
100 crm_element_value_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
101 if (reply_id == cib->call_id) {
102 xmlNode *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
103 NULL, NULL);
104 xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
105
106 crm_trace("Synchronous reply %d received", reply_id);
107 if (crm_element_value_int(op_reply, PCMK__XA_CIB_RC, &rc) != 0) {
108 rc = -EPROTO;
109 }
110
111 if (output_data == NULL || (call_options & cib_discard_reply)) {
112 crm_trace("Discarding reply");
113 } else {
114 *output_data = pcmk__xml_copy(NULL, tmp);
115 }
116
117 } else if (reply_id <= 0) {
118 crm_err("Received bad reply: No id set");
119 crm_log_xml_err(op_reply, "Bad reply");
120 rc = -ENOMSG;
121 goto done;
122
123 } else {
124 crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
125 crm_log_xml_err(op_reply, "Old reply");
126 rc = -ENOMSG;
127 goto done;
128 }
129
130 if (op_reply == NULL && cib->state == cib_disconnected) {
131 rc = -ENOTCONN;
132
133 } else if (rc == pcmk_ok && op_reply == NULL) {
134 rc = -ETIME;
135 }
136
137 switch (rc) {
138 case pcmk_ok:
139 case -EPERM:
140 break;
141
142
143 case -pcmk_err_diff_resync:
144 rc = pcmk_ok;
145 break;
146
147
148 case -EPROTO:
149 case -ENOMSG:
150 crm_err("Call failed: %s", pcmk_strerror(rc));
151 if (op_reply) {
152 crm_log_xml_err(op_reply, "Invalid reply");
153 }
154 break;
155
156 default:
157 if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
158 crm_warn("Call failed: %s", pcmk_strerror(rc));
159 }
160 }
161
162 done:
163 if (!crm_ipc_connected(native->ipc)) {
164 crm_err("The CIB manager disconnected");
165 cib->state = cib_disconnected;
166 }
167
168 pcmk__xml_free(op_msg);
169 pcmk__xml_free(op_reply);
170 return rc;
171 }
172
173 static int
174 cib_native_dispatch_internal(const char *buffer, ssize_t length,
175 gpointer userdata)
176 {
177 const char *type = NULL;
178 xmlNode *msg = NULL;
179
180 cib_t *cib = userdata;
181
182 crm_trace("dispatching %p", userdata);
183
184 if (cib == NULL) {
185 crm_err("No CIB!");
186 return 0;
187 }
188
189 msg = pcmk__xml_parse(buffer);
190
191 if (msg == NULL) {
192 crm_warn("Received a NULL message from the CIB manager");
193 return 0;
194 }
195
196
197 type = crm_element_value(msg, PCMK__XA_T);
198 crm_trace("Activating %s callbacks...", type);
199 crm_log_xml_explicit(msg, "cib-reply");
200
201 if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
202 cib_native_callback(cib, msg, 0, 0);
203
204 } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
205 g_list_foreach(cib->notify_list, cib_native_notify, msg);
206
207 } else {
208 crm_err("Unknown message type: %s", type);
209 }
210
211 pcmk__xml_free(msg);
212 return 0;
213 }
214
215 static void
216 cib_native_destroy(void *userdata)
217 {
218 cib_t *cib = userdata;
219 cib_native_opaque_t *native = cib->variant_opaque;
220
221 crm_trace("destroying %p", userdata);
222 cib->state = cib_disconnected;
223 native->source = NULL;
224 native->ipc = NULL;
225
226 if (native->dnotify_fn) {
227 native->dnotify_fn(userdata);
228 }
229 }
230
231 static int
232 cib_native_signoff(cib_t *cib)
233 {
234 cib_native_opaque_t *native = cib->variant_opaque;
235
236 crm_debug("Disconnecting from the CIB manager");
237
238 cib_free_notify(cib);
239 remove_cib_op_callback(0, TRUE);
240
241 if (native->source != NULL) {
242
243 mainloop_del_ipc_client(native->source);
244 native->source = NULL;
245 native->ipc = NULL;
246
247 } else if (native->ipc) {
248
249 crm_ipc_t *ipc = native->ipc;
250
251 native->ipc = NULL;
252 crm_ipc_close(ipc);
253 crm_ipc_destroy(ipc);
254 }
255
256 cib->cmds->end_transaction(cib, false, cib_none);
257 cib->state = cib_disconnected;
258 cib->type = cib_no_connection;
259
260 return pcmk_ok;
261 }
262
263 static int
264 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
265 {
266 int rc = pcmk_ok;
267 const char *channel = NULL;
268 cib_native_opaque_t *native = cib->variant_opaque;
269 xmlNode *hello = NULL;
270
271 struct ipc_client_callbacks cib_callbacks = {
272 .dispatch = cib_native_dispatch_internal,
273 .destroy = cib_native_destroy
274 };
275
276 if (name == NULL) {
277 name = pcmk__s(crm_system_name, "client");
278 }
279
280 cib->call_timeout = PCMK__IPC_TIMEOUT;
281
282 if (type == cib_command) {
283 cib->state = cib_connected_command;
284 channel = PCMK__SERVER_BASED_RW;
285
286 } else if (type == cib_command_nonblocking) {
287 cib->state = cib_connected_command;
288 channel = PCMK__SERVER_BASED_SHM;
289
290 } else if (type == cib_query) {
291 cib->state = cib_connected_query;
292 channel = PCMK__SERVER_BASED_RO;
293
294 } else {
295 return -ENOTCONN;
296 }
297
298 crm_trace("Connecting %s channel", channel);
299
300 native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH,
301 512 * 1024, cib, &cib_callbacks);
302 native->ipc = mainloop_get_ipc_client(native->source);
303
304 if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
305 crm_info("Could not connect to CIB manager for %s", name);
306 rc = -ENOTCONN;
307 }
308
309 if (rc == pcmk_ok) {
310 rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
311 cib_sync_call, NULL, name, &hello);
312 }
313
314 if (rc == pcmk_ok) {
315 xmlNode *reply = NULL;
316
317 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
318 &reply) > 0) {
319 const char *msg_type = crm_element_value(reply, PCMK__XA_CIB_OP);
320
321 crm_log_xml_trace(reply, "reg-reply");
322
323 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
324 crm_info("Reply to CIB registration message has unknown type "
325 "'%s'",
326 msg_type);
327 rc = -EPROTO;
328
329 } else {
330 native->token = crm_element_value_copy(reply,
331 PCMK__XA_CIB_CLIENTID);
332 if (native->token == NULL) {
333 rc = -EPROTO;
334 }
335 }
336 pcmk__xml_free(reply);
337
338 } else {
339 rc = -ECOMM;
340 }
341 pcmk__xml_free(hello);
342 }
343
344 if (rc == pcmk_ok) {
345 crm_info("Successfully connected to CIB manager for %s", name);
346 return pcmk_ok;
347 }
348
349 crm_info("Connection to CIB manager for %s failed: %s",
350 name, pcmk_strerror(rc));
351 cib_native_signoff(cib);
352 return rc;
353 }
354
355 static int
356 cib_native_free(cib_t *cib)
357 {
358 int rc = pcmk_ok;
359
360 if (cib->state != cib_disconnected) {
361 rc = cib_native_signoff(cib);
362 }
363
364 if (cib->state == cib_disconnected) {
365 cib_native_opaque_t *native = cib->variant_opaque;
366
367 free(native->token);
368 free(cib->variant_opaque);
369 free(cib->cmds);
370 free(cib->user);
371 free(cib);
372 }
373
374 return rc;
375 }
376
377 static int
378 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
379 {
380 int rc = pcmk_ok;
381 xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
382 cib_native_opaque_t *native = cib->variant_opaque;
383
384 if (cib->state != cib_disconnected) {
385 crm_xml_add(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
386 crm_xml_add(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
387 crm_xml_add_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
388 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
389 1000 * cib->call_timeout, NULL);
390 if (rc <= 0) {
391 crm_trace("Notification not registered: %d", rc);
392 rc = -ECOMM;
393 }
394 }
395
396 pcmk__xml_free(notify_msg);
397 return rc;
398 }
399
400 static int
401 cib_native_set_connection_dnotify(cib_t *cib,
402 void (*dnotify) (gpointer user_data))
403 {
404 cib_native_opaque_t *native = NULL;
405
406 if (cib == NULL) {
407 crm_err("No CIB!");
408 return FALSE;
409 }
410
411 native = cib->variant_opaque;
412 native->dnotify_fn = dnotify;
413
414 return pcmk_ok;
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 static int
436 cib_native_client_id(const cib_t *cib, const char **async_id,
437 const char **sync_id)
438 {
439 cib_native_opaque_t *native = cib->variant_opaque;
440
441 if (async_id != NULL) {
442 *async_id = native->token;
443 }
444 if (sync_id != NULL) {
445 *sync_id = native->token;
446 }
447 return pcmk_ok;
448 }
449
450 cib_t *
451 cib_native_new(void)
452 {
453 cib_native_opaque_t *native = NULL;
454 cib_t *cib = cib_new_variant();
455
456 if (cib == NULL) {
457 return NULL;
458 }
459
460 native = calloc(1, sizeof(cib_native_opaque_t));
461
462 if (native == NULL) {
463 free(cib);
464 return NULL;
465 }
466
467 cib->variant = cib_native;
468 cib->variant_opaque = native;
469
470 native->ipc = NULL;
471 native->source = NULL;
472 native->dnotify_fn = NULL;
473
474
475 cib->delegate_fn = cib_native_perform_op_delegate;
476 cib->cmds->signon = cib_native_signon;
477 cib->cmds->signoff = cib_native_signoff;
478 cib->cmds->free = cib_native_free;
479
480 cib->cmds->register_notification = cib_native_register_notification;
481 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
482
483 cib->cmds->client_id = cib_native_client_id;
484
485 return cib;
486 }