This source file includes following definitions.
- build_query_reply
- attrd_client_clear_failure
- attrd_client_peer_remove
- attrd_client_query
- attrd_client_refresh
- handle_missing_host
- expand_regexes
- handle_regexes
- handle_value_expansion
- send_update_msg_to_cluster
- send_child_update
- attrd_client_update
- attrd_ipc_accept
- attrd_ipc_closed
- attrd_ipc_destroy
- attrd_ipc_dispatch
- attrd_ipc_fini
- attrd_init_ipc
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <errno.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16
17 #include <crm/cluster.h>
18 #include <crm/cluster/internal.h>
19 #include <crm/common/acl_internal.h>
20 #include <crm/common/ipc_internal.h>
21 #include <crm/common/logging.h>
22 #include <crm/common/results.h>
23 #include <crm/common/strings_internal.h>
24 #include <crm/common/util.h>
25 #include <crm/common/xml.h>
26
27 #include "pacemaker-attrd.h"
28
29 static qb_ipcs_service_t *ipcs = NULL;
30
31
32
33
34
35
36
37
38
39
40
41 static xmlNode *build_query_reply(const char *attr, const char *host)
42 {
43 xmlNode *reply = pcmk__xe_create(NULL, __func__);
44 attribute_t *a;
45
46 crm_xml_add(reply, PCMK__XA_T, PCMK__VALUE_ATTRD);
47 crm_xml_add(reply, PCMK__XA_SUBT, PCMK__ATTRD_CMD_QUERY);
48 crm_xml_add(reply, PCMK__XA_ATTR_VERSION, ATTRD_PROTOCOL_VERSION);
49
50
51 a = g_hash_table_lookup(attributes, attr);
52 if (a) {
53 attribute_value_t *v;
54 xmlNode *host_value;
55
56 crm_xml_add(reply, PCMK__XA_ATTR_NAME, attr);
57
58
59 if (pcmk__str_eq(host, "localhost", pcmk__str_casei)) {
60 host = attrd_cluster->uname;
61 crm_trace("Mapped localhost to %s", host);
62 }
63
64
65 if (host) {
66 v = g_hash_table_lookup(a->values, host);
67 host_value = pcmk__xe_create(reply, PCMK_XE_NODE);
68 pcmk__xe_add_node(host_value, host, 0);
69 crm_xml_add(host_value, PCMK__XA_ATTR_VALUE,
70 (v? v->current : NULL));
71
72
73 } else {
74 GHashTableIter iter;
75
76 g_hash_table_iter_init(&iter, a->values);
77 while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &v)) {
78 host_value = pcmk__xe_create(reply, PCMK_XE_NODE);
79 pcmk__xe_add_node(host_value, v->nodename, 0);
80 crm_xml_add(host_value, PCMK__XA_ATTR_VALUE, v->current);
81 }
82 }
83 }
84 return reply;
85 }
86
87 xmlNode *
88 attrd_client_clear_failure(pcmk__request_t *request)
89 {
90 xmlNode *xml = request->xml;
91 const char *rsc, *op, *interval_spec;
92
93 if (minimum_protocol_version >= 2) {
94
95
96
97 attrd_send_message(NULL, xml, false);
98 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
99 return NULL;
100 }
101
102 rsc = crm_element_value(xml, PCMK__XA_ATTR_RESOURCE);
103 op = crm_element_value(xml, PCMK__XA_ATTR_CLEAR_OPERATION);
104 interval_spec = crm_element_value(xml, PCMK__XA_ATTR_CLEAR_INTERVAL);
105
106
107 crm_xml_add(xml, PCMK_XA_TASK, PCMK__ATTRD_CMD_UPDATE);
108
109
110
111 if (rsc) {
112 char *pattern;
113
114 if (op == NULL) {
115 pattern = crm_strdup_printf(ATTRD_RE_CLEAR_ONE, rsc);
116
117 } else {
118 guint interval_ms = 0U;
119
120 pcmk_parse_interval_spec(interval_spec, &interval_ms);
121 pattern = crm_strdup_printf(ATTRD_RE_CLEAR_OP,
122 rsc, op, interval_ms);
123 }
124
125 crm_xml_add(xml, PCMK__XA_ATTR_REGEX, pattern);
126 free(pattern);
127
128 } else {
129 crm_xml_add(xml, PCMK__XA_ATTR_REGEX, ATTRD_RE_CLEAR_ALL);
130 }
131
132
133 pcmk__xe_remove_attr(xml, PCMK__XA_ATTR_NAME);
134 pcmk__xe_remove_attr(xml, PCMK__XA_ATTR_VALUE);
135
136 return attrd_client_update(request);
137 }
138
139 xmlNode *
140 attrd_client_peer_remove(pcmk__request_t *request)
141 {
142 xmlNode *xml = request->xml;
143
144
145 const char *host = crm_element_value(xml, PCMK__XA_ATTR_HOST);
146 char *host_alloc = NULL;
147
148 attrd_send_ack(request->ipc_client, request->ipc_id, request->ipc_flags);
149
150 if (host == NULL) {
151 int nodeid = 0;
152
153 crm_element_value_int(xml, PCMK__XA_ATTR_HOST_ID, &nodeid);
154 if (nodeid > 0) {
155 crm_node_t *node = NULL;
156 char *host_alloc = NULL;
157
158 node = pcmk__search_node_caches(nodeid, NULL,
159 pcmk__node_search_cluster_member);
160 if (node && node->uname) {
161
162 host = node->uname;
163 } else {
164
165 host_alloc = pcmk__cluster_node_name(nodeid);
166 host = host_alloc;
167 }
168 pcmk__xe_add_node(xml, host, 0);
169 }
170 }
171
172 if (host) {
173 crm_info("Client %s is requesting all values for %s be removed",
174 pcmk__client_name(request->ipc_client), host);
175 attrd_send_message(NULL, xml, false);
176 free(host_alloc);
177 } else {
178 crm_info("Ignoring request by client %s to remove all peer values without specifying peer",
179 pcmk__client_name(request->ipc_client));
180 }
181
182 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
183 return NULL;
184 }
185
186 xmlNode *
187 attrd_client_query(pcmk__request_t *request)
188 {
189 xmlNode *query = request->xml;
190 xmlNode *reply = NULL;
191 const char *attr = NULL;
192
193 crm_debug("Query arrived from %s", pcmk__client_name(request->ipc_client));
194
195
196 attr = crm_element_value(query, PCMK__XA_ATTR_NAME);
197 if (attr == NULL) {
198 pcmk__format_result(&request->result, CRM_EX_ERROR, PCMK_EXEC_ERROR,
199 "Ignoring malformed query from %s (no attribute name given)",
200 pcmk__client_name(request->ipc_client));
201 return NULL;
202 }
203
204
205 reply = build_query_reply(attr,
206 crm_element_value(query, PCMK__XA_ATTR_HOST));
207 if (reply == NULL) {
208 pcmk__format_result(&request->result, CRM_EX_ERROR, PCMK_EXEC_ERROR,
209 "Could not respond to query from %s: could not create XML reply",
210 pcmk__client_name(request->ipc_client));
211 return NULL;
212 } else {
213 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
214 }
215
216 request->ipc_client->request_id = 0;
217 return reply;
218 }
219
220 xmlNode *
221 attrd_client_refresh(pcmk__request_t *request)
222 {
223 crm_info("Updating all attributes");
224
225 attrd_send_ack(request->ipc_client, request->ipc_id, request->ipc_flags);
226 attrd_write_attributes(attrd_write_all|attrd_write_no_delay);
227
228 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
229 return NULL;
230 }
231
232 static void
233 handle_missing_host(xmlNode *xml)
234 {
235 const char *host = crm_element_value(xml, PCMK__XA_ATTR_HOST);
236
237 if (host == NULL) {
238 crm_trace("Inferring host");
239 pcmk__xe_add_node(xml, attrd_cluster->uname, attrd_cluster->nodeid);
240 }
241 }
242
243
244
245
246 static int
247 expand_regexes(xmlNode *xml, const char *attr, const char *value, const char *regex)
248 {
249 if (attr == NULL && regex) {
250 bool matched = false;
251 GHashTableIter aIter;
252 regex_t r_patt;
253
254 crm_debug("Setting %s to %s", regex, value);
255 if (regcomp(&r_patt, regex, REG_EXTENDED|REG_NOSUB)) {
256 return EINVAL;
257 }
258
259 g_hash_table_iter_init(&aIter, attributes);
260 while (g_hash_table_iter_next(&aIter, (gpointer *) & attr, NULL)) {
261 int status = regexec(&r_patt, attr, 0, NULL, 0);
262
263 if (status == 0) {
264 xmlNode *child = pcmk__xe_create(xml, PCMK_XE_OP);
265
266 crm_trace("Matched %s with %s", attr, regex);
267 matched = true;
268
269
270
271
272 pcmk__xe_copy_attrs(child, xml, pcmk__xaf_no_overwrite);
273 pcmk__xe_remove_attr(child, PCMK__XA_ATTR_REGEX);
274 crm_xml_add(child, PCMK__XA_ATTR_NAME, attr);
275 }
276 }
277
278 regfree(&r_patt);
279
280
281
282
283
284
285 if (!matched) {
286 return pcmk_rc_op_unsatisfied;
287 }
288
289 } else if (attr == NULL) {
290 return pcmk_rc_bad_nvpair;
291 }
292
293 return pcmk_rc_ok;
294 }
295
296 static int
297 handle_regexes(pcmk__request_t *request)
298 {
299 xmlNode *xml = request->xml;
300 int rc = pcmk_rc_ok;
301
302 const char *attr = crm_element_value(xml, PCMK__XA_ATTR_NAME);
303 const char *value = crm_element_value(xml, PCMK__XA_ATTR_VALUE);
304 const char *regex = crm_element_value(xml, PCMK__XA_ATTR_REGEX);
305
306 rc = expand_regexes(xml, attr, value, regex);
307
308 if (rc == EINVAL) {
309 pcmk__format_result(&request->result, CRM_EX_ERROR, PCMK_EXEC_ERROR,
310 "Bad regex '%s' for update from client %s", regex,
311 pcmk__client_name(request->ipc_client));
312
313 } else if (rc == pcmk_rc_bad_nvpair) {
314 crm_err("Update request did not specify attribute or regular expression");
315 pcmk__format_result(&request->result, CRM_EX_ERROR, PCMK_EXEC_ERROR,
316 "Client %s update request did not specify attribute or regular expression",
317 pcmk__client_name(request->ipc_client));
318 }
319
320 return rc;
321 }
322
323 static int
324 handle_value_expansion(const char **value, xmlNode *xml, const char *op,
325 const char *attr)
326 {
327 attribute_t *a = g_hash_table_lookup(attributes, attr);
328
329 if (a == NULL && pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE_DELAY, pcmk__str_none)) {
330 return EINVAL;
331 }
332
333 if (*value && attrd_value_needs_expansion(*value)) {
334 int int_value;
335 attribute_value_t *v = NULL;
336
337 if (a) {
338 const char *host = crm_element_value(xml, PCMK__XA_ATTR_HOST);
339 v = g_hash_table_lookup(a->values, host);
340 }
341
342 int_value = attrd_expand_value(*value, (v? v->current : NULL));
343
344 crm_info("Expanded %s=%s to %d", attr, *value, int_value);
345 crm_xml_add_int(xml, PCMK__XA_ATTR_VALUE, int_value);
346
347
348 *value = crm_element_value(xml, PCMK__XA_ATTR_VALUE);
349 }
350
351 return pcmk_rc_ok;
352 }
353
354 static void
355 send_update_msg_to_cluster(pcmk__request_t *request, xmlNode *xml)
356 {
357 if (pcmk__str_eq(attrd_request_sync_point(xml), PCMK__VALUE_CLUSTER, pcmk__str_none)) {
358
359
360
361
362 attrd_expect_confirmations(request, attrd_cluster_sync_point_update);
363 attrd_send_message(NULL, xml, true);
364
365 } else {
366
367
368
369
370
371
372 attrd_send_message(NULL, xml, false);
373 }
374 }
375
376 static int
377 send_child_update(xmlNode *child, void *data)
378 {
379 pcmk__request_t *request = (pcmk__request_t *) data;
380
381
382
383
384 request->xml = child;
385 attrd_client_update(request);
386 return pcmk_rc_ok;
387 }
388
389 xmlNode *
390 attrd_client_update(pcmk__request_t *request)
391 {
392 xmlNode *xml = NULL;
393 const char *attr, *value, *regex;
394
395 CRM_CHECK((request != NULL) && (request->xml != NULL), return NULL);
396
397 xml = request->xml;
398
399
400
401
402
403 if (xml->children != NULL) {
404 if (ATTRD_SUPPORTS_MULTI_MESSAGE(minimum_protocol_version)) {
405
406
407
408
409
410 for (xmlNode *child = pcmk__xe_first_child(xml, PCMK_XE_OP, NULL,
411 NULL);
412 child != NULL; child = pcmk__xe_next_same(child)) {
413
414 attr = crm_element_value(child, PCMK__XA_ATTR_NAME);
415 value = crm_element_value(child, PCMK__XA_ATTR_VALUE);
416
417 handle_missing_host(child);
418
419 if (handle_value_expansion(&value, child, request->op, attr) == EINVAL) {
420 pcmk__format_result(&request->result, CRM_EX_NOSUCH, PCMK_EXEC_ERROR,
421 "Attribute %s does not exist", attr);
422 return NULL;
423 }
424 }
425
426 send_update_msg_to_cluster(request, xml);
427 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
428
429 } else {
430
431
432
433 xmlNode *orig_xml = request->xml;
434
435
436
437
438
439 pcmk__xe_foreach_child(xml, PCMK_XE_OP, send_child_update, request);
440 request->xml = orig_xml;
441 }
442
443 return NULL;
444 }
445
446 attr = crm_element_value(xml, PCMK__XA_ATTR_NAME);
447 value = crm_element_value(xml, PCMK__XA_ATTR_VALUE);
448 regex = crm_element_value(xml, PCMK__XA_ATTR_REGEX);
449
450 if (handle_regexes(request) != pcmk_rc_ok) {
451
452 return NULL;
453 } else if (regex) {
454
455
456
457
458 return attrd_client_update(request);
459 }
460
461 handle_missing_host(xml);
462
463 if (handle_value_expansion(&value, xml, request->op, attr) == EINVAL) {
464 pcmk__format_result(&request->result, CRM_EX_NOSUCH, PCMK_EXEC_ERROR,
465 "Attribute %s does not exist", attr);
466 return NULL;
467 }
468
469 crm_debug("Broadcasting %s[%s]=%s%s",
470 attr, crm_element_value(xml, PCMK__XA_ATTR_HOST),
471 value, (attrd_election_won()? " (writer)" : ""));
472
473 send_update_msg_to_cluster(request, xml);
474 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
475 return NULL;
476 }
477
478
479
480
481
482
483
484
485
486
487
488 static int32_t
489 attrd_ipc_accept(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
490 {
491 crm_trace("New client connection %p", c);
492 if (attrd_shutting_down(false)) {
493 crm_info("Ignoring new connection from pid %d during shutdown",
494 pcmk__client_pid(c));
495 return -ECONNREFUSED;
496 }
497
498 if (pcmk__new_client(c, uid, gid) == NULL) {
499 return -ENOMEM;
500 }
501 return pcmk_ok;
502 }
503
504
505
506
507
508
509
510
511
512 static int32_t
513 attrd_ipc_closed(qb_ipcs_connection_t *c)
514 {
515 pcmk__client_t *client = pcmk__find_client(c);
516
517 if (client == NULL) {
518 crm_trace("Ignoring request to clean up unknown connection %p", c);
519 } else {
520 crm_trace("Cleaning up closed client connection %p", c);
521
522
523 attrd_remove_client_from_waitlist(client);
524
525
526 attrd_do_not_wait_for_client(client);
527
528 pcmk__free_client(client);
529 }
530
531 return FALSE;
532 }
533
534
535
536
537
538
539
540
541
542
543 static void
544 attrd_ipc_destroy(qb_ipcs_connection_t *c)
545 {
546 crm_trace("Destroying client connection %p", c);
547 attrd_ipc_closed(c);
548 }
549
550 static int32_t
551 attrd_ipc_dispatch(qb_ipcs_connection_t * c, void *data, size_t size)
552 {
553 uint32_t id = 0;
554 uint32_t flags = 0;
555 pcmk__client_t *client = pcmk__find_client(c);
556 xmlNode *xml = NULL;
557
558
559 CRM_CHECK((c != NULL) && (client != NULL), return 0);
560 if (data == NULL) {
561 crm_debug("No IPC data from PID %d", pcmk__client_pid(c));
562 return 0;
563 }
564
565 xml = pcmk__client_data2xml(client, data, &id, &flags);
566
567 if (xml == NULL) {
568 crm_debug("Unrecognizable IPC data from PID %d", pcmk__client_pid(c));
569 pcmk__ipc_send_ack(client, id, flags, PCMK__XE_ACK, NULL,
570 CRM_EX_PROTOCOL);
571 return 0;
572
573 } else {
574 pcmk__request_t request = {
575 .ipc_client = client,
576 .ipc_id = id,
577 .ipc_flags = flags,
578 .peer = NULL,
579 .xml = xml,
580 .call_options = 0,
581 .result = PCMK__UNKNOWN_RESULT,
582 };
583
584 pcmk__assert(client->user != NULL);
585 pcmk__update_acl_user(xml, PCMK__XA_ATTR_USER, client->user);
586
587 request.op = crm_element_value_copy(request.xml, PCMK_XA_TASK);
588 CRM_CHECK(request.op != NULL, return 0);
589
590 attrd_handle_request(&request);
591 pcmk__reset_request(&request);
592 }
593
594 free_xml(xml);
595 return 0;
596 }
597
598 static struct qb_ipcs_service_handlers ipc_callbacks = {
599 .connection_accept = attrd_ipc_accept,
600 .connection_created = NULL,
601 .msg_process = attrd_ipc_dispatch,
602 .connection_closed = attrd_ipc_closed,
603 .connection_destroyed = attrd_ipc_destroy
604 };
605
606 void
607 attrd_ipc_fini(void)
608 {
609 if (ipcs != NULL) {
610 pcmk__drop_all_clients(ipcs);
611 qb_ipcs_destroy(ipcs);
612 ipcs = NULL;
613 }
614 }
615
616
617
618
619
620 void
621 attrd_init_ipc(void)
622 {
623 pcmk__serve_attrd_ipc(&ipcs, &ipc_callbacks);
624 }