summaryrefslogtreecommitdiffstats
path: root/lib/common/ipc_attrd.c
blob: 9caaabec0161178d405697b13ff86ff32a438b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
 * Copyright 2011-2023 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif

#include <crm_internal.h>

#include <stdio.h>

#include <crm/crm.h>
#include <crm/common/ipc.h>
#include <crm/common/ipc_attrd_internal.h>
#include <crm/common/attrd_internal.h>
#include <crm/msg_xml.h>
#include "crmcommon_private.h"

static void
set_pairs_data(pcmk__attrd_api_reply_t *data, xmlNode *msg_data)
{
    const char *name = NULL;
    pcmk__attrd_query_pair_t *pair;

    name = crm_element_value(msg_data, PCMK__XA_ATTR_NAME);

    for (xmlNode *node = first_named_child(msg_data, XML_CIB_TAG_NODE);
         node != NULL; node = crm_next_same_xml(node)) {
        pair = calloc(1, sizeof(pcmk__attrd_query_pair_t));

        CRM_ASSERT(pair != NULL);

        pair->node = crm_element_value(node, PCMK__XA_ATTR_NODE_NAME);
        pair->name = name;
        pair->value = crm_element_value(node, PCMK__XA_ATTR_VALUE);
        data->data.pairs = g_list_prepend(data->data.pairs, pair);
    }
}

static bool
reply_expected(pcmk_ipc_api_t *api, const xmlNode *request)
{
    const char *command = crm_element_value(request, PCMK__XA_TASK);

    return pcmk__str_any_of(command,
                            PCMK__ATTRD_CMD_CLEAR_FAILURE,
                            PCMK__ATTRD_CMD_QUERY,
                            PCMK__ATTRD_CMD_REFRESH,
                            PCMK__ATTRD_CMD_UPDATE,
                            PCMK__ATTRD_CMD_UPDATE_BOTH,
                            PCMK__ATTRD_CMD_UPDATE_DELAY,
                            NULL);
}

static bool
dispatch(pcmk_ipc_api_t *api, xmlNode *reply)
{
    const char *value = NULL;
    crm_exit_t status = CRM_EX_OK;

    pcmk__attrd_api_reply_t reply_data = {
        pcmk__attrd_reply_unknown
    };

    if (pcmk__str_eq((const char *) reply->name, "ack", pcmk__str_none)) {
        return false;
    }

    /* Do some basic validation of the reply */
    value = crm_element_value(reply, F_TYPE);
    if (pcmk__str_empty(value)
        || !pcmk__str_eq(value, T_ATTRD, pcmk__str_none)) {
        crm_info("Unrecognizable message from attribute manager: "
                 "message type '%s' not '" T_ATTRD "'", pcmk__s(value, ""));
        status = CRM_EX_PROTOCOL;
        goto done;
    }

    value = crm_element_value(reply, F_SUBTYPE);

    /* Only the query command gets a reply for now. NULL counts as query for
     * backward compatibility with attribute managers <2.1.3 that didn't set it.
     */
    if (pcmk__str_eq(value, PCMK__ATTRD_CMD_QUERY, pcmk__str_null_matches)) {
        if (!xmlHasProp(reply, (pcmkXmlStr) PCMK__XA_ATTR_NAME)) {
            status = ENXIO; // Most likely, the attribute doesn't exist
            goto done;
        }
        reply_data.reply_type = pcmk__attrd_reply_query;
        set_pairs_data(&reply_data, reply);

    } else {
        crm_info("Unrecognizable message from attribute manager: "
                 "message subtype '%s' unknown", pcmk__s(value, ""));
        status = CRM_EX_PROTOCOL;
        goto done;
    }

done:
    pcmk__call_ipc_callback(api, pcmk_ipc_event_reply, status, &reply_data);

    /* Free any reply data that was allocated */
    if (reply_data.data.pairs) {
        g_list_free_full(reply_data.data.pairs, free);
    }

    return false;
}

pcmk__ipc_methods_t *
pcmk__attrd_api_methods(void)
{
    pcmk__ipc_methods_t *cmds = calloc(1, sizeof(pcmk__ipc_methods_t));

    if (cmds != NULL) {
        cmds->new_data = NULL;
        cmds->free_data = NULL;
        cmds->post_connect = NULL;
        cmds->reply_expected = reply_expected;
        cmds->dispatch = dispatch;
    }
    return cmds;
}

/*!
 * \internal
 * \brief Create a generic pacemaker-attrd operation
 *
 * \param[in] user_name  If not NULL, ACL user to set for operation
 *
 * \return XML of pacemaker-attrd operation
 */
static xmlNode *
create_attrd_op(const char *user_name)
{
    xmlNode *attrd_op = create_xml_node(NULL, __func__);

    crm_xml_add(attrd_op, F_TYPE, T_ATTRD);
    crm_xml_add(attrd_op, F_ORIG, (crm_system_name? crm_system_name: "unknown"));
    crm_xml_add(attrd_op, PCMK__XA_ATTR_USER, user_name);

    return attrd_op;
}

static int
create_api(pcmk_ipc_api_t **api)
{
    int rc = pcmk_new_ipc_api(api, pcmk_ipc_attrd);

    if (rc != pcmk_rc_ok) {
        crm_err("Could not connect to attrd: %s", pcmk_rc_str(rc));
    }

    return rc;
}

static void
destroy_api(pcmk_ipc_api_t *api)
{
    pcmk_disconnect_ipc(api);
    pcmk_free_ipc_api(api);
    api = NULL;
}

static int
connect_and_send_attrd_request(pcmk_ipc_api_t *api, const xmlNode *request)
{
    int rc = pcmk_rc_ok;

    rc = pcmk__connect_ipc(api, pcmk_ipc_dispatch_sync, 5);
    if (rc != pcmk_rc_ok) {
        crm_err("Could not connect to %s: %s",
                pcmk_ipc_name(api, true), pcmk_rc_str(rc));
        return rc;
    }

    rc = pcmk__send_ipc_request(api, request);
    if (rc != pcmk_rc_ok) {
        crm_err("Could not send request to %s: %s",
                pcmk_ipc_name(api, true), pcmk_rc_str(rc));
        return rc;
    }

    return pcmk_rc_ok;
}

static int
send_attrd_request(pcmk_ipc_api_t *api, const xmlNode *request)
{
    return pcmk__send_ipc_request(api, request);
}

int
pcmk__attrd_api_clear_failures(pcmk_ipc_api_t *api, const char *node,
                               const char *resource, const char *operation,
                               const char *interval_spec, const char *user_name,
                               uint32_t options)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = create_attrd_op(user_name);
    const char *interval_desc = NULL;
    const char *op_desc = NULL;
    const char *target = pcmk__node_attr_target(node);

    if (target != NULL) {
        node = target;
    }

    crm_xml_add(request, PCMK__XA_TASK, PCMK__ATTRD_CMD_CLEAR_FAILURE);
    pcmk__xe_add_node(request, node, 0);
    crm_xml_add(request, PCMK__XA_ATTR_RESOURCE, resource);
    crm_xml_add(request, PCMK__XA_ATTR_OPERATION, operation);
    crm_xml_add(request, PCMK__XA_ATTR_INTERVAL, interval_spec);
    crm_xml_add_int(request, PCMK__XA_ATTR_IS_REMOTE,
                    pcmk_is_set(options, pcmk__node_attr_remote));

    if (api == NULL) {
        rc = create_api(&api);
        if (rc != pcmk_rc_ok) {
            return rc;
        }

        rc = connect_and_send_attrd_request(api, request);
        destroy_api(api);

    } else if (!pcmk_ipc_is_connected(api)) {
        rc = connect_and_send_attrd_request(api, request);

    } else {
        rc = send_attrd_request(api, request);
    }

    free_xml(request);

    if (operation) {
        interval_desc = interval_spec? interval_spec : "nonrecurring";
        op_desc = operation;
    } else {
        interval_desc = "all";
        op_desc = "operations";
    }

    crm_debug("Asked pacemaker-attrd to clear failure of %s %s for %s on %s: %s (%d)",
              interval_desc, op_desc, (resource? resource : "all resources"),
              (node? node : "all nodes"), pcmk_rc_str(rc), rc);

    return rc;
}

int
pcmk__attrd_api_delete(pcmk_ipc_api_t *api, const char *node, const char *name,
                       uint32_t options)
{
    const char *target = NULL;

    if (name == NULL) {
        return EINVAL;
    }

    target = pcmk__node_attr_target(node);

    if (target != NULL) {
        node = target;
    }

    /* Make sure the right update option is set. */
    options &= ~pcmk__node_attr_delay;
    options |= pcmk__node_attr_value;

    return pcmk__attrd_api_update(api, node, name, NULL, NULL, NULL, NULL, options);
}

int
pcmk__attrd_api_purge(pcmk_ipc_api_t *api, const char *node)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = NULL;
    const char *display_host = (node ? node : "localhost");
    const char *target = pcmk__node_attr_target(node);

    if (target != NULL) {
        node = target;
    }

    request = create_attrd_op(NULL);

    crm_xml_add(request, PCMK__XA_TASK, PCMK__ATTRD_CMD_PEER_REMOVE);
    pcmk__xe_add_node(request, node, 0);

    if (api == NULL) {
        rc = create_api(&api);
        if (rc != pcmk_rc_ok) {
            return rc;
        }

        rc = connect_and_send_attrd_request(api, request);
        destroy_api(api);

    } else if (!pcmk_ipc_is_connected(api)) {
        rc = connect_and_send_attrd_request(api, request);

    } else {
        rc = send_attrd_request(api, request);
    }

    free_xml(request);

    crm_debug("Asked pacemaker-attrd to purge %s: %s (%d)",
              display_host, pcmk_rc_str(rc), rc);

    return rc;
}

int
pcmk__attrd_api_query(pcmk_ipc_api_t *api, const char *node, const char *name,
                      uint32_t options)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = NULL;
    const char *target = NULL;

    if (name == NULL) {
        return EINVAL;
    }

    if (pcmk_is_set(options, pcmk__node_attr_query_all)) {
        node = NULL;
    } else {
        target = pcmk__node_attr_target(node);

        if (target != NULL) {
            node = target;
        }
    }

    request = create_attrd_op(NULL);

    crm_xml_add(request, PCMK__XA_ATTR_NAME, name);
    crm_xml_add(request, PCMK__XA_TASK, PCMK__ATTRD_CMD_QUERY);
    pcmk__xe_add_node(request, node, 0);

    rc = send_attrd_request(api, request);
    free_xml(request);

    if (node) {
        crm_debug("Queried pacemaker-attrd for %s on %s: %s (%d)",
                  name, node, pcmk_rc_str(rc), rc);
    } else {
        crm_debug("Queried pacemaker-attrd for %s: %s (%d)",
                  name, pcmk_rc_str(rc), rc);
    }

    return rc;
}

int
pcmk__attrd_api_refresh(pcmk_ipc_api_t *api, const char *node)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = NULL;
    const char *display_host = (node ? node : "localhost");
    const char *target = pcmk__node_attr_target(node);

    if (target != NULL) {
        node = target;
    }

    request = create_attrd_op(NULL);

    crm_xml_add(request, PCMK__XA_TASK, PCMK__ATTRD_CMD_REFRESH);
    pcmk__xe_add_node(request, node, 0);

    if (api == NULL) {
        rc = create_api(&api);
        if (rc != pcmk_rc_ok) {
            return rc;
        }

        rc = connect_and_send_attrd_request(api, request);
        destroy_api(api);

    } else if (!pcmk_ipc_is_connected(api)) {
        rc = connect_and_send_attrd_request(api, request);

    } else {
        rc = send_attrd_request(api, request);
    }

    free_xml(request);

    crm_debug("Asked pacemaker-attrd to refresh %s: %s (%d)",
              display_host, pcmk_rc_str(rc), rc);

    return rc;
}

static void
add_op_attr(xmlNode *op, uint32_t options)
{
    if (pcmk_all_flags_set(options, pcmk__node_attr_value | pcmk__node_attr_delay)) {
        crm_xml_add(op, PCMK__XA_TASK, PCMK__ATTRD_CMD_UPDATE_BOTH);
    } else if (pcmk_is_set(options, pcmk__node_attr_value)) {
        crm_xml_add(op, PCMK__XA_TASK, PCMK__ATTRD_CMD_UPDATE);
    } else if (pcmk_is_set(options, pcmk__node_attr_delay)) {
        crm_xml_add(op, PCMK__XA_TASK, PCMK__ATTRD_CMD_UPDATE_DELAY);
    }
}

static void
populate_update_op(xmlNode *op, const char *node, const char *name, const char *value,
                   const char *dampen, const char *set, uint32_t options)
{
    if (pcmk_is_set(options, pcmk__node_attr_pattern)) {
        crm_xml_add(op, PCMK__XA_ATTR_PATTERN, name);
    } else {
        crm_xml_add(op, PCMK__XA_ATTR_NAME, name);
    }

    if (pcmk_is_set(options, pcmk__node_attr_utilization)) {
        crm_xml_add(op, PCMK__XA_ATTR_SET_TYPE, XML_TAG_UTILIZATION);
    } else {
        crm_xml_add(op, PCMK__XA_ATTR_SET_TYPE, XML_TAG_ATTR_SETS);
    }

    add_op_attr(op, options);

    crm_xml_add(op, PCMK__XA_ATTR_VALUE, value);
    crm_xml_add(op, PCMK__XA_ATTR_DAMPENING, dampen);
    pcmk__xe_add_node(op, node, 0);
    crm_xml_add(op, PCMK__XA_ATTR_SET, set);
    crm_xml_add_int(op, PCMK__XA_ATTR_IS_REMOTE,
                    pcmk_is_set(options, pcmk__node_attr_remote));
    crm_xml_add_int(op, PCMK__XA_ATTR_IS_PRIVATE,
                    pcmk_is_set(options, pcmk__node_attr_private));

    if (pcmk_is_set(options, pcmk__node_attr_sync_local)) {
        crm_xml_add(op, PCMK__XA_ATTR_SYNC_POINT, PCMK__VALUE_LOCAL);
    } else if (pcmk_is_set(options, pcmk__node_attr_sync_cluster)) {
        crm_xml_add(op, PCMK__XA_ATTR_SYNC_POINT, PCMK__VALUE_CLUSTER);
    }
}

int
pcmk__attrd_api_update(pcmk_ipc_api_t *api, const char *node, const char *name,
                       const char *value, const char *dampen, const char *set,
                       const char *user_name, uint32_t options)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = NULL;
    const char *display_host = (node ? node : "localhost");
    const char *target = NULL;

    if (name == NULL) {
        return EINVAL;
    }

    target = pcmk__node_attr_target(node);

    if (target != NULL) {
        node = target;
    }

    request = create_attrd_op(user_name);
    populate_update_op(request, node, name, value, dampen, set, options);

    if (api == NULL) {
        rc = create_api(&api);
        if (rc != pcmk_rc_ok) {
            return rc;
        }

        rc = connect_and_send_attrd_request(api, request);
        destroy_api(api);

    } else if (!pcmk_ipc_is_connected(api)) {
        rc = connect_and_send_attrd_request(api, request);

    } else {
        rc = send_attrd_request(api, request);
    }

    free_xml(request);

    crm_debug("Asked pacemaker-attrd to update %s on %s: %s (%d)",
              name, display_host, pcmk_rc_str(rc), rc);

    return rc;
}

int
pcmk__attrd_api_update_list(pcmk_ipc_api_t *api, GList *attrs, const char *dampen,
                            const char *set, const char *user_name,
                            uint32_t options)
{
    int rc = pcmk_rc_ok;
    xmlNode *request = NULL;

    if (attrs == NULL) {
        return EINVAL;
    }

    /* There are two different ways of handling a list of attributes:
     *
     * (1) For messages originating from some command line tool, we have to send
     *     them one at a time.  In this loop, we just call pcmk__attrd_api_update
     *     for each, letting it deal with creating the API object if it doesn't
     *     already exist.
     *
     *     The reason we can't use a single message in this case is that we can't
     *     trust that the server supports it.  Remote nodes could be involved
     *     here, and there's no guarantee that a newer client running on a remote
     *     node is talking to (or proxied through) a cluster node with a newer
     *     attrd.  We also can't just try sending a single message and then falling
     *     back on multiple.  There's no handshake with the attrd server to
     *     determine its version.  And then we would need to do that fallback in the
     *     dispatch function for this to work for all connection types (mainloop in
     *     particular), and at that point we won't know what the original message
     *     was in order to break it apart and resend as individual messages.
     *
     * (2) For messages between daemons, we can be assured that the local attrd
     *     will support the new message and that it can send to the other attrds
     *     as one request or split up according to the minimum supported version.
     */
    for (GList *iter = attrs; iter != NULL; iter = iter->next) {
        pcmk__attrd_query_pair_t *pair = (pcmk__attrd_query_pair_t *) iter->data;

        if (pcmk__is_daemon) {
            const char *target = NULL;
            xmlNode *child = NULL;

            /* First time through this loop - create the basic request. */
            if (request == NULL) {
                request = create_attrd_op(user_name);
                add_op_attr(request, options);
            }

            /* Add a child node for this operation.  We add the task to the top
             * level XML node so attrd_ipc_dispatch doesn't need changes.  And
             * then we also add the task to each child node in populate_update_op
             * so attrd_client_update knows what form of update is taking place.
             */
            child = create_xml_node(request, XML_ATTR_OP);
            target = pcmk__node_attr_target(pair->node);

            if (target != NULL) {
                pair->node = target;
            }

            populate_update_op(child, pair->node, pair->name, pair->value, dampen,
                               set, options);
        } else {
            rc = pcmk__attrd_api_update(api, pair->node, pair->name, pair->value,
                                        dampen, set, user_name, options);
        }
    }

    /* If we were doing multiple attributes at once, we still need to send the
     * request.  Do that now, creating and destroying the API object if needed.
     */
    if (pcmk__is_daemon) {
        bool created_api = false;

        if (api == NULL) {
            rc = create_api(&api);
            if (rc != pcmk_rc_ok) {
                return rc;
            }

            created_api = true;
        }

        rc = connect_and_send_attrd_request(api, request);
        free_xml(request);

        if (created_api) {
            destroy_api(api);
        }
    }

    return rc;
}