dhodovsk / source-git / pacemaker

Forked from source-git/pacemaker 3 years ago
Clone

Blame lib/fencing/st_client.c

rpm-build 3ee90c
/*
rpm-build 3ee90c
 * Copyright 2004-2019 the Pacemaker project contributors
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * The version control history for this file may have further details.
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * This source code is licensed under the GNU Lesser General Public License
rpm-build 3ee90c
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
rpm-build 3ee90c
 */
rpm-build 3ee90c
rpm-build 3ee90c
#include <crm_internal.h>
rpm-build 3ee90c
#include <unistd.h>
rpm-build 3ee90c
#include <stdlib.h>
rpm-build 3ee90c
#include <stdio.h>
rpm-build 3ee90c
#include <stdbool.h>
rpm-build 3ee90c
#include <string.h>
rpm-build 3ee90c
#include <ctype.h>
rpm-build 3ee90c
#include <libgen.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <sys/stat.h>
rpm-build 3ee90c
#include <sys/types.h>
rpm-build 3ee90c
#include <sys/wait.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <glib.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <crm/crm.h>
rpm-build 3ee90c
#include <crm/stonith-ng.h>
rpm-build 3ee90c
#include <crm/fencing/internal.h>
rpm-build 3ee90c
#include <crm/msg_xml.h>
rpm-build 3ee90c
#include <crm/common/xml.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <crm/common/mainloop.h>
rpm-build 3ee90c
rpm-build 3ee90c
#if SUPPORT_CIBSECRETS
rpm-build 3ee90c
#  include <crm/common/cib_secrets.h>
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
CRM_TRACE_INIT_DATA(stonith);
rpm-build 3ee90c
rpm-build 3ee90c
struct stonith_action_s {
rpm-build 3ee90c
    /*! user defined data */
rpm-build 3ee90c
    char *agent;
rpm-build 3ee90c
    char *action;
rpm-build 3ee90c
    char *victim;
rpm-build 3ee90c
    GHashTable *args;
rpm-build 3ee90c
    int timeout;
rpm-build 3ee90c
    int async;
rpm-build 3ee90c
    void *userdata;
rpm-build 3ee90c
    void (*done_cb) (GPid pid, gint status, const char *output, gpointer user_data);
rpm-build 3ee90c
    void (*fork_cb) (GPid pid, gpointer user_data);
rpm-build 3ee90c
rpm-build 3ee90c
    svc_action_t *svc_action;
rpm-build 3ee90c
rpm-build 3ee90c
    /*! internal timing information */
rpm-build 3ee90c
    time_t initial_start_time;
rpm-build 3ee90c
    int tries;
rpm-build 3ee90c
    int remaining_timeout;
rpm-build 3ee90c
    int max_retries;
rpm-build 3ee90c
rpm-build 3ee90c
    /* device output data */
rpm-build 3ee90c
    GPid pid;
rpm-build 3ee90c
    int rc;
rpm-build 3ee90c
    char *output;
rpm-build 3ee90c
    char *error;
rpm-build 3ee90c
};
rpm-build 3ee90c
rpm-build 3ee90c
typedef struct stonith_private_s {
rpm-build 3ee90c
    char *token;
rpm-build 3ee90c
    crm_ipc_t *ipc;
rpm-build 3ee90c
    mainloop_io_t *source;
rpm-build 3ee90c
    GHashTable *stonith_op_callback_table;
rpm-build 3ee90c
    GList *notify_list;
rpm-build 3ee90c
    int notify_refcnt;
rpm-build 3ee90c
    bool notify_deletes;
rpm-build 3ee90c
rpm-build 3ee90c
    void (*op_callback) (stonith_t * st, stonith_callback_data_t * data);
rpm-build 3ee90c
rpm-build 3ee90c
} stonith_private_t;
rpm-build 3ee90c
rpm-build 3ee90c
typedef struct stonith_notify_client_s {
rpm-build 3ee90c
    const char *event;
rpm-build 3ee90c
    const char *obj_id;         /* implement one day */
rpm-build 3ee90c
    const char *obj_type;       /* implement one day */
rpm-build 3ee90c
    void (*notify) (stonith_t * st, stonith_event_t * e);
rpm-build 3ee90c
    bool delete;
rpm-build 3ee90c
rpm-build 3ee90c
} stonith_notify_client_t;
rpm-build 3ee90c
rpm-build 3ee90c
typedef struct stonith_callback_client_s {
rpm-build 3ee90c
    void (*callback) (stonith_t * st, stonith_callback_data_t * data);
rpm-build 3ee90c
    const char *id;
rpm-build 3ee90c
    void *user_data;
rpm-build 3ee90c
    gboolean only_success;
rpm-build 3ee90c
    gboolean allow_timeout_updates;
rpm-build 3ee90c
    struct timer_rec_s *timer;
rpm-build 3ee90c
rpm-build 3ee90c
} stonith_callback_client_t;
rpm-build 3ee90c
rpm-build 3ee90c
struct notify_blob_s {
rpm-build 3ee90c
    stonith_t *stonith;
rpm-build 3ee90c
    xmlNode *xml;
rpm-build 3ee90c
};
rpm-build 3ee90c
rpm-build 3ee90c
struct timer_rec_s {
rpm-build 3ee90c
    int call_id;
rpm-build 3ee90c
    int timeout;
rpm-build 3ee90c
    guint ref;
rpm-build 3ee90c
    stonith_t *stonith;
rpm-build 3ee90c
};
rpm-build 3ee90c
rpm-build 3ee90c
typedef int (*stonith_op_t) (const char *, int, const char *, xmlNode *,
rpm-build 3ee90c
                             xmlNode *, xmlNode *, xmlNode **, xmlNode **);
rpm-build 3ee90c
rpm-build 3ee90c
bool stonith_dispatch(stonith_t * st);
rpm-build 3ee90c
xmlNode *stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data,
rpm-build 3ee90c
                           int call_options);
rpm-build 3ee90c
static int stonith_send_command(stonith_t *stonith, const char *op,
rpm-build 3ee90c
                                xmlNode *data, xmlNode **output_data,
rpm-build 3ee90c
                                int call_options, int timeout);
rpm-build 3ee90c
rpm-build 3ee90c
static void stonith_connection_destroy(gpointer user_data);
rpm-build 3ee90c
static void stonith_send_notification(gpointer data, gpointer user_data);
rpm-build 3ee90c
static int internal_stonith_action_execute(stonith_action_t * action);
rpm-build 3ee90c
static void log_action(stonith_action_t *action, pid_t pid);
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \brief Get agent namespace by name
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] namespace_s  Name of namespace as string
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return Namespace as enum value
rpm-build 3ee90c
 */
rpm-build 3ee90c
enum stonith_namespace
rpm-build 3ee90c
stonith_text2namespace(const char *namespace_s)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if ((namespace_s == NULL) || !strcmp(namespace_s, "any")) {
rpm-build 3ee90c
        return st_namespace_any;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (!strcmp(namespace_s, "redhat")
rpm-build 3ee90c
               || !strcmp(namespace_s, "stonith-ng")) {
rpm-build 3ee90c
        return st_namespace_rhcs;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (!strcmp(namespace_s, "internal")) {
rpm-build 3ee90c
        return st_namespace_internal;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (!strcmp(namespace_s, "heartbeat")) {
rpm-build 3ee90c
        return st_namespace_lha;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return st_namespace_invalid;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \brief Get agent namespace name
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] namespace  Namespace as enum value
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return Namespace name as string
rpm-build 3ee90c
 */
rpm-build 3ee90c
const char *
rpm-build 3ee90c
stonith_namespace2text(enum stonith_namespace st_namespace)
rpm-build 3ee90c
{
rpm-build 3ee90c
    switch (st_namespace) {
rpm-build 3ee90c
        case st_namespace_any:      return "any";
rpm-build 3ee90c
        case st_namespace_rhcs:     return "stonith-ng";
rpm-build 3ee90c
        case st_namespace_internal: return "internal";
rpm-build 3ee90c
        case st_namespace_lha:      return "heartbeat";
rpm-build 3ee90c
        default:                    break;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return "unsupported";
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \brief Determine namespace of a fence agent
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] agent        Fence agent type
rpm-build 3ee90c
 * \param[in] namespace_s  Name of agent namespace as string, if known
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return Namespace of specified agent, as enum value
rpm-build 3ee90c
 */
rpm-build 3ee90c
enum stonith_namespace
rpm-build 3ee90c
stonith_get_namespace(const char *agent, const char *namespace_s)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (safe_str_eq(namespace_s, "internal")) {
rpm-build 3ee90c
        return st_namespace_internal;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (stonith__agent_is_rhcs(agent)) {
rpm-build 3ee90c
        return st_namespace_rhcs;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
#if HAVE_STONITH_STONITH_H
rpm-build 3ee90c
    if (stonith__agent_is_lha(agent)) {
rpm-build 3ee90c
        return st_namespace_lha;
rpm-build 3ee90c
    }
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    crm_err("Unknown fence agent: %s", agent);
rpm-build 3ee90c
    return st_namespace_invalid;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
log_action(stonith_action_t *action, pid_t pid)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (action->output) {
rpm-build 3ee90c
        /* Logging the whole string confuses syslog when the string is xml */
rpm-build 3ee90c
        char *prefix = crm_strdup_printf("%s[%d] stdout:", action->agent, pid);
rpm-build 3ee90c
rpm-build 3ee90c
        crm_log_output(LOG_TRACE, prefix, action->output);
rpm-build 3ee90c
        free(prefix);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->error) {
rpm-build 3ee90c
        /* Logging the whole string confuses syslog when the string is xml */
rpm-build 3ee90c
        char *prefix = crm_strdup_printf("%s[%d] stderr:", action->agent, pid);
rpm-build 3ee90c
rpm-build 3ee90c
        crm_log_output(LOG_WARNING, prefix, action->error);
rpm-build 3ee90c
        free(prefix);
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/* when cycling through the list we don't want to delete items
rpm-build 3ee90c
   so just mark them and when we know nobody is using the list
rpm-build 3ee90c
   loop over it to remove the marked items
rpm-build 3ee90c
 */
rpm-build 3ee90c
static void
rpm-build 3ee90c
foreach_notify_entry (stonith_private_t *private,
rpm-build 3ee90c
                GFunc func,
rpm-build 3ee90c
                gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    private->notify_refcnt++;
rpm-build 3ee90c
    g_list_foreach(private->notify_list, func, user_data);
rpm-build 3ee90c
    private->notify_refcnt--;
rpm-build 3ee90c
    if ((private->notify_refcnt == 0) &&
rpm-build 3ee90c
        private->notify_deletes) {
rpm-build 3ee90c
        GList *list_item = private->notify_list;
rpm-build 3ee90c
rpm-build 3ee90c
        private->notify_deletes = FALSE;
rpm-build 3ee90c
        while (list_item != NULL)
rpm-build 3ee90c
        {
rpm-build 3ee90c
            stonith_notify_client_t *list_client = list_item->data;
rpm-build 3ee90c
            GList *next = g_list_next(list_item);
rpm-build 3ee90c
rpm-build 3ee90c
            if (list_client->delete) {
rpm-build 3ee90c
                free(list_client);
rpm-build 3ee90c
                private->notify_list =
rpm-build 3ee90c
                    g_list_delete_link(private->notify_list, list_item);
rpm-build 3ee90c
            }
rpm-build 3ee90c
            list_item = next;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_connection_destroy(gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_t *stonith = user_data;
rpm-build 3ee90c
    stonith_private_t *native = NULL;
rpm-build 3ee90c
    struct notify_blob_s blob;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Sending destroyed notification");
rpm-build 3ee90c
    blob.stonith = stonith;
rpm-build 3ee90c
    blob.xml = create_xml_node(NULL, "notify");
rpm-build 3ee90c
rpm-build 3ee90c
    native = stonith->st_private;
rpm-build 3ee90c
    native->ipc = NULL;
rpm-build 3ee90c
    native->source = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    free(native->token); native->token = NULL;
rpm-build 3ee90c
    stonith->state = stonith_disconnected;
rpm-build 3ee90c
    crm_xml_add(blob.xml, F_TYPE, T_STONITH_NOTIFY);
rpm-build 3ee90c
    crm_xml_add(blob.xml, F_SUBTYPE, T_STONITH_NOTIFY_DISCONNECT);
rpm-build 3ee90c
rpm-build 3ee90c
    foreach_notify_entry(native, stonith_send_notification, &blob;;
rpm-build 3ee90c
    free_xml(blob.xml);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
xmlNode *
rpm-build 3ee90c
create_device_registration_xml(const char *id, enum stonith_namespace namespace,
rpm-build 3ee90c
                               const char *agent, stonith_key_value_t *params,
rpm-build 3ee90c
                               const char *rsc_provides)
rpm-build 3ee90c
{
rpm-build 3ee90c
    xmlNode *data = create_xml_node(NULL, F_STONITH_DEVICE);
rpm-build 3ee90c
    xmlNode *args = create_xml_node(data, XML_TAG_ATTRS);
rpm-build 3ee90c
rpm-build 3ee90c
#if HAVE_STONITH_STONITH_H
rpm-build 3ee90c
    if (namespace == st_namespace_any) {
rpm-build 3ee90c
        namespace = stonith_get_namespace(agent, NULL);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (namespace == st_namespace_lha) {
rpm-build 3ee90c
        hash2field((gpointer) "plugin", (gpointer) agent, args);
rpm-build 3ee90c
        agent = "fence_legacy";
rpm-build 3ee90c
    }
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(data, XML_ATTR_ID, id);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add(data, "agent", agent);
rpm-build 3ee90c
    if ((namespace != st_namespace_any) && (namespace != st_namespace_invalid)) {
rpm-build 3ee90c
        crm_xml_add(data, "namespace", stonith_namespace2text(namespace));
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (rsc_provides) {
rpm-build 3ee90c
        crm_xml_add(data, "rsc_provides", rsc_provides);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    for (; params; params = params->next) {
rpm-build 3ee90c
        hash2field((gpointer) params->key, (gpointer) params->value, args);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return data;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_register_device(stonith_t * st, int call_options,
rpm-build 3ee90c
                            const char *id, const char *namespace, const char *agent,
rpm-build 3ee90c
                            stonith_key_value_t * params)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_device_registration_xml(id, stonith_text2namespace(namespace),
rpm-build 3ee90c
                                          agent, params, NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_send_command(st, STONITH_OP_DEVICE_ADD, data, NULL, call_options, 0);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_remove_device(stonith_t * st, int call_options, const char *name)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, F_STONITH_DEVICE);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add(data, XML_ATTR_ID, name);
rpm-build 3ee90c
    rc = stonith_send_command(st, STONITH_OP_DEVICE_DEL, data, NULL, call_options, 0);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_remove_level_full(stonith_t *st, int options,
rpm-build 3ee90c
                              const char *node, const char *pattern,
rpm-build 3ee90c
                              const char *attr, const char *value, int level)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(node || pattern || (attr && value), return -EINVAL);
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, XML_TAG_FENCING_LEVEL);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
rpm-build 3ee90c
    if (node) {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET, node);
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (pattern) {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_PATTERN, pattern);
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_ATTRIBUTE, attr);
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_VALUE, value);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add_int(data, XML_ATTR_STONITH_INDEX, level);
rpm-build 3ee90c
    rc = stonith_send_command(st, STONITH_OP_LEVEL_DEL, data, NULL, options, 0);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_remove_level(stonith_t * st, int options, const char *node, int level)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_api_remove_level_full(st, options, node,
rpm-build 3ee90c
                                         NULL, NULL, NULL, level);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Create XML for fence topology level registration request
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] node        If not NULL, target level by this node name
rpm-build 3ee90c
 * \param[in] pattern     If not NULL, target by node name using this regex
rpm-build 3ee90c
 * \param[in] attr        If not NULL, target by this node attribute
rpm-build 3ee90c
 * \param[in] value       If not NULL, target by this node attribute value
rpm-build 3ee90c
 * \param[in] level       Index number of level to register
rpm-build 3ee90c
 * \param[in] device_list List of devices in level
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return Newly allocated XML tree on success, NULL otherwise
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \note The caller should set only one of node, pattern or attr/value.
rpm-build 3ee90c
 */
rpm-build 3ee90c
xmlNode *
rpm-build 3ee90c
create_level_registration_xml(const char *node, const char *pattern,
rpm-build 3ee90c
                              const char *attr, const char *value,
rpm-build 3ee90c
                              int level, stonith_key_value_t *device_list)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int len = 0;
rpm-build 3ee90c
    char *list = NULL;
rpm-build 3ee90c
    xmlNode *data;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(node || pattern || (attr && value), return NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, XML_TAG_FENCING_LEVEL);
rpm-build 3ee90c
    CRM_CHECK(data, return NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add_int(data, XML_ATTR_ID, level);
rpm-build 3ee90c
    crm_xml_add_int(data, XML_ATTR_STONITH_INDEX, level);
rpm-build 3ee90c
rpm-build 3ee90c
    if (node) {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET, node);
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (pattern) {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_PATTERN, pattern);
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_ATTRIBUTE, attr);
rpm-build 3ee90c
        crm_xml_add(data, XML_ATTR_STONITH_TARGET_VALUE, value);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    for (; device_list; device_list = device_list->next) {
rpm-build 3ee90c
rpm-build 3ee90c
        int adding = strlen(device_list->value);
rpm-build 3ee90c
        if(list) {
rpm-build 3ee90c
            adding++;                                      /* +1 space */
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Adding %s (%dc) at offset %d", device_list->value, adding, len);
rpm-build 3ee90c
        list = realloc_safe(list, len + adding + 1);       /* +1 EOS */
rpm-build 3ee90c
        if (list == NULL) {
rpm-build 3ee90c
            crm_perror(LOG_CRIT, "Could not create device list");
rpm-build 3ee90c
            free_xml(data);
rpm-build 3ee90c
            return NULL;
rpm-build 3ee90c
        }
rpm-build 3ee90c
        sprintf(list + len, "%s%s", len?",":"", device_list->value);
rpm-build 3ee90c
        len += adding;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(data, XML_ATTR_STONITH_DEVICES, list);
rpm-build 3ee90c
rpm-build 3ee90c
    free(list);
rpm-build 3ee90c
    return data;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_register_level_full(stonith_t * st, int options, const char *node,
rpm-build 3ee90c
                                const char *pattern,
rpm-build 3ee90c
                                const char *attr, const char *value,
rpm-build 3ee90c
                                int level, stonith_key_value_t *device_list)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = create_level_registration_xml(node, pattern, attr, value,
rpm-build 3ee90c
                                                  level, device_list);
rpm-build 3ee90c
    CRM_CHECK(data != NULL, return -EINVAL);
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_send_command(st, STONITH_OP_LEVEL_ADD, data, NULL, options, 0);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_register_level(stonith_t * st, int options, const char *node, int level,
rpm-build 3ee90c
                           stonith_key_value_t * device_list)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_api_register_level_full(st, options, node, NULL, NULL, NULL,
rpm-build 3ee90c
                                           level, device_list);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
append_arg(const char *key, const char *value, GHashTable **args)
rpm-build 3ee90c
{
rpm-build 3ee90c
    CRM_CHECK(key != NULL, return);
rpm-build 3ee90c
    CRM_CHECK(value != NULL, return);
rpm-build 3ee90c
    CRM_CHECK(args != NULL, return);
rpm-build 3ee90c
rpm-build 3ee90c
    if (strstr(key, "pcmk_")) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    } else if (strstr(key, CRM_META)) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    } else if (safe_str_eq(key, "crm_feature_set")) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (!*args) {
rpm-build 3ee90c
        *args = crm_str_table_new();
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(*args != NULL, return);
rpm-build 3ee90c
    crm_trace("Appending: %s=%s", key, value);
rpm-build 3ee90c
    g_hash_table_replace(*args, strdup(key), strdup(value));
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
append_config_arg(gpointer key, gpointer value, gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    /* The fencer will filter action out when it registers the device,
rpm-build 3ee90c
     * but ignore it here just in case any other library callers
rpm-build 3ee90c
     * fail to do so.
rpm-build 3ee90c
     */
rpm-build 3ee90c
    if (safe_str_neq(key, STONITH_ATTR_ACTION_OP)) {
rpm-build 3ee90c
        append_arg(key, value, user_data);
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static GHashTable *
rpm-build 3ee90c
make_args(const char *agent, const char *action, const char *victim, uint32_t victim_nodeid, GHashTable * device_args,
rpm-build 3ee90c
          GHashTable * port_map)
rpm-build 3ee90c
{
rpm-build 3ee90c
    char buffer[512];
rpm-build 3ee90c
    GHashTable *arg_list = NULL;
rpm-build 3ee90c
    const char *value = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(action != NULL, return NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    snprintf(buffer, sizeof(buffer), "pcmk_%s_action", action);
rpm-build 3ee90c
    if (device_args) {
rpm-build 3ee90c
        value = g_hash_table_lookup(device_args, buffer);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (value) {
rpm-build 3ee90c
        crm_debug("Substituting action '%s' for requested operation '%s'", value, action);
rpm-build 3ee90c
        action = value;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    append_arg(STONITH_ATTR_ACTION_OP, action, &arg_list);
rpm-build 3ee90c
    if (victim && device_args) {
rpm-build 3ee90c
        const char *alias = victim;
rpm-build 3ee90c
        const char *param = g_hash_table_lookup(device_args, STONITH_ATTR_HOSTARG);
rpm-build 3ee90c
rpm-build 3ee90c
        if (port_map && g_hash_table_lookup(port_map, victim)) {
rpm-build 3ee90c
            alias = g_hash_table_lookup(port_map, victim);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        /* Always supply the node's name, too:
rpm-build 3ee90c
         * https://github.com/ClusterLabs/fence-agents/blob/master/doc/FenceAgentAPI.md
rpm-build 3ee90c
         */
rpm-build 3ee90c
        append_arg("nodename", victim, &arg_list);
rpm-build 3ee90c
        if (victim_nodeid) {
rpm-build 3ee90c
            char nodeid_str[33] = { 0, };
rpm-build 3ee90c
            if (snprintf(nodeid_str, 33, "%u", (unsigned int)victim_nodeid)) {
rpm-build 3ee90c
                crm_info("For stonith action (%s) for victim %s, adding nodeid (%s) to parameters",
rpm-build 3ee90c
                         action, victim, nodeid_str);
rpm-build 3ee90c
                append_arg("nodeid", nodeid_str, &arg_list);
rpm-build 3ee90c
            }
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        /* Check if we need to supply the victim in any other form */
rpm-build 3ee90c
        if(safe_str_eq(agent, "fence_legacy")) {
rpm-build 3ee90c
            value = agent;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (param == NULL) {
rpm-build 3ee90c
            param = "port";
rpm-build 3ee90c
            value = g_hash_table_lookup(device_args, param);
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (safe_str_eq(param, "none")) {
rpm-build 3ee90c
            value = param;      /* Nothing more to do */
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            value = g_hash_table_lookup(device_args, param);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        /* Don't overwrite explictly set values for $param */
rpm-build 3ee90c
        if (value == NULL || safe_str_eq(value, "dynamic")) {
rpm-build 3ee90c
            crm_debug("Performing '%s' action targeting '%s' as '%s=%s'", action, victim, param,
rpm-build 3ee90c
                      alias);
rpm-build 3ee90c
            append_arg(param, alias, &arg_list);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (device_args) {
rpm-build 3ee90c
        g_hash_table_foreach(device_args, append_config_arg, &arg_list);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return arg_list;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Free all memory used by a stonith action
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in,out] action  Action to free
rpm-build 3ee90c
 */
rpm-build 3ee90c
void
rpm-build 3ee90c
stonith__destroy_action(stonith_action_t *action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (action) {
rpm-build 3ee90c
        free(action->agent);
rpm-build 3ee90c
        if (action->args) {
rpm-build 3ee90c
            g_hash_table_destroy(action->args);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        free(action->action);
rpm-build 3ee90c
        free(action->victim);
rpm-build 3ee90c
        if (action->svc_action) {
rpm-build 3ee90c
            services_action_free(action->svc_action);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        free(action->output);
rpm-build 3ee90c
        free(action->error);
rpm-build 3ee90c
        free(action);
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Get the result of an executed stonith action
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in,out] action        Executed action
rpm-build 3ee90c
 * \param[out]    rc            Where to store result code (or NULL)
rpm-build 3ee90c
 * \param[out]    output        Where to store standard output (or NULL)
rpm-build 3ee90c
 * \param[out]    error_output  Where to store standard error output (or NULL)
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \note If output or error_output is not NULL, the caller is responsible for
rpm-build 3ee90c
 *       freeing the memory.
rpm-build 3ee90c
 */
rpm-build 3ee90c
void
rpm-build 3ee90c
stonith__action_result(stonith_action_t *action, int *rc, char **output,
rpm-build 3ee90c
                       char **error_output)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (rc) {
rpm-build 3ee90c
        *rc = pcmk_ok;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (output) {
rpm-build 3ee90c
        *output = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (error_output) {
rpm-build 3ee90c
        *error_output = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (action != NULL) {
rpm-build 3ee90c
        if (rc) {
rpm-build 3ee90c
            *rc = action->rc;
rpm-build 3ee90c
        }
rpm-build 3ee90c
        if (output && action->output) {
rpm-build 3ee90c
            *output = action->output;
rpm-build 3ee90c
            action->output = NULL; // hand off memory management to caller
rpm-build 3ee90c
        }
rpm-build 3ee90c
        if (error_output && action->error) {
rpm-build 3ee90c
            *error_output = action->error;
rpm-build 3ee90c
            action->error = NULL; // hand off memory management to caller
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
#define FAILURE_MAX_RETRIES 2
rpm-build 3ee90c
stonith_action_t *
rpm-build 3ee90c
stonith_action_create(const char *agent,
rpm-build 3ee90c
                      const char *_action,
rpm-build 3ee90c
                      const char *victim,
rpm-build 3ee90c
                      uint32_t victim_nodeid,
rpm-build 3ee90c
                      int timeout, GHashTable * device_args, GHashTable * port_map)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_action_t *action;
rpm-build 3ee90c
rpm-build 3ee90c
    action = calloc(1, sizeof(stonith_action_t));
rpm-build 3ee90c
    action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
rpm-build 3ee90c
    crm_debug("Preparing '%s' action for %s using agent %s",
rpm-build 3ee90c
              _action, (victim? victim : "no target"), agent);
rpm-build 3ee90c
    action->agent = strdup(agent);
rpm-build 3ee90c
    action->action = strdup(_action);
rpm-build 3ee90c
    if (victim) {
rpm-build 3ee90c
        action->victim = strdup(victim);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    action->timeout = action->remaining_timeout = timeout;
rpm-build 3ee90c
    action->max_retries = FAILURE_MAX_RETRIES;
rpm-build 3ee90c
rpm-build 3ee90c
    if (device_args) {
rpm-build 3ee90c
        char buffer[512];
rpm-build 3ee90c
        const char *value = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
        snprintf(buffer, sizeof(buffer), "pcmk_%s_retries", _action);
rpm-build 3ee90c
        value = g_hash_table_lookup(device_args, buffer);
rpm-build 3ee90c
rpm-build 3ee90c
        if (value) {
rpm-build 3ee90c
            action->max_retries = atoi(value);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return action;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static gboolean
rpm-build 3ee90c
update_remaining_timeout(stonith_action_t * action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int diff = time(NULL) - action->initial_start_time;
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->tries >= action->max_retries) {
rpm-build 3ee90c
        crm_info("Attempted to execute agent %s (%s) the maximum number of times (%d) allowed",
rpm-build 3ee90c
                 action->agent, action->action, action->max_retries);
rpm-build 3ee90c
        action->remaining_timeout = 0;
rpm-build 3ee90c
    } else if ((action->rc != -ETIME) && diff < (action->timeout * 0.7)) {
rpm-build 3ee90c
        /* only set remaining timeout period if there is 30%
rpm-build 3ee90c
         * or greater of the original timeout period left */
rpm-build 3ee90c
        action->remaining_timeout = action->timeout - diff;
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        action->remaining_timeout = 0;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return action->remaining_timeout ? TRUE : FALSE;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
svc_action_to_errno(svc_action_t *svc_action) {
rpm-build 3ee90c
    int rv = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    if (svc_action->rc > 0) {
rpm-build 3ee90c
        /* Try to provide a useful error code based on the fence agent's
rpm-build 3ee90c
            * error output.
rpm-build 3ee90c
            */
rpm-build 3ee90c
        if (svc_action->rc == PCMK_OCF_TIMEOUT) {
rpm-build 3ee90c
            rv = -ETIME;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (svc_action->stderr_data == NULL) {
rpm-build 3ee90c
            rv = -ENODATA;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (strstr(svc_action->stderr_data, "imed out")) {
rpm-build 3ee90c
            /* Some agents have their own internal timeouts */
rpm-build 3ee90c
            rv = -ETIME;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (strstr(svc_action->stderr_data, "Unrecognised action")) {
rpm-build 3ee90c
            rv = -EOPNOTSUPP;
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            rv = -pcmk_err_generic;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return rv;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_action_async_done(svc_action_t *svc_action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_action_t *action = (stonith_action_t *) svc_action->cb_data;
rpm-build 3ee90c
rpm-build 3ee90c
    action->rc = svc_action_to_errno(svc_action);
rpm-build 3ee90c
    action->output = svc_action->stdout_data;
rpm-build 3ee90c
    svc_action->stdout_data = NULL;
rpm-build 3ee90c
    action->error = svc_action->stderr_data;
rpm-build 3ee90c
    svc_action->stderr_data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    svc_action->params = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Child process %d performing action '%s' exited with rc %d",
rpm-build 3ee90c
                action->pid, action->action, svc_action->rc);
rpm-build 3ee90c
rpm-build 3ee90c
    log_action(action, action->pid);
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->rc != pcmk_ok && update_remaining_timeout(action)) {
rpm-build 3ee90c
        int rc = internal_stonith_action_execute(action);
rpm-build 3ee90c
        if (rc == pcmk_ok) {
rpm-build 3ee90c
            return;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->done_cb) {
rpm-build 3ee90c
        action->done_cb(action->pid, action->rc, action->output, action->userdata);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    action->svc_action = NULL; // don't remove our caller
rpm-build 3ee90c
    stonith__destroy_action(action);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_action_async_forked(svc_action_t *svc_action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_action_t *action = (stonith_action_t *) svc_action->cb_data;
rpm-build 3ee90c
rpm-build 3ee90c
    action->pid = svc_action->pid;
rpm-build 3ee90c
    action->svc_action = svc_action;
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->fork_cb) {
rpm-build 3ee90c
        (action->fork_cb) (svc_action->pid, action->userdata);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Child process %d performing action '%s' successfully forked",
rpm-build 3ee90c
              action->pid, action->action);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
internal_stonith_action_execute(stonith_action_t * action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = -EPROTO;
rpm-build 3ee90c
    int is_retry = 0;
rpm-build 3ee90c
    svc_action_t *svc_action = NULL;
rpm-build 3ee90c
    static int stonith_sequence = 0;
rpm-build 3ee90c
    char *buffer = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (!action->tries) {
rpm-build 3ee90c
        action->initial_start_time = time(NULL);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    action->tries++;
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->tries > 1) {
rpm-build 3ee90c
        crm_info("Attempt %d to execute %s (%s). remaining timeout is %d",
rpm-build 3ee90c
                 action->tries, action->agent, action->action, action->remaining_timeout);
rpm-build 3ee90c
        is_retry = 1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->args == NULL || action->agent == NULL)
rpm-build 3ee90c
        goto fail;
rpm-build 3ee90c
rpm-build 3ee90c
    buffer = crm_strdup_printf(RH_STONITH_DIR "/%s", basename(action->agent));
rpm-build 3ee90c
    svc_action = services_action_create_generic(buffer, NULL);
rpm-build 3ee90c
    free(buffer);
rpm-build 3ee90c
    svc_action->timeout = 1000 * action->remaining_timeout;
rpm-build 3ee90c
    svc_action->standard = strdup(PCMK_RESOURCE_CLASS_STONITH);
rpm-build 3ee90c
    svc_action->id = crm_strdup_printf("%s_%s_%d", basename(action->agent),
rpm-build 3ee90c
                                       action->action, action->tries);
rpm-build 3ee90c
    svc_action->agent = strdup(action->agent);
rpm-build 3ee90c
    svc_action->sequence = stonith_sequence++;
rpm-build 3ee90c
    svc_action->params = action->args;
rpm-build 3ee90c
    svc_action->cb_data = (void *) action;
rpm-build 3ee90c
    set_bit(svc_action->flags, SVC_ACTION_NON_BLOCKED);
rpm-build 3ee90c
rpm-build 3ee90c
    /* keep retries from executing out of control and free previous results */
rpm-build 3ee90c
    if (is_retry) {
rpm-build 3ee90c
        free(action->output);
rpm-build 3ee90c
        action->output = NULL;
rpm-build 3ee90c
        free(action->error);
rpm-build 3ee90c
        action->error = NULL;
rpm-build 3ee90c
        sleep(1);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (action->async) {
rpm-build 3ee90c
        /* async */
rpm-build 3ee90c
        if(services_action_async_fork_notify(svc_action,
rpm-build 3ee90c
            &stonith_action_async_done,
rpm-build 3ee90c
            &stonith_action_async_forked) == FALSE) {
rpm-build 3ee90c
            services_action_free(svc_action);
rpm-build 3ee90c
            svc_action = NULL;
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            rc = 0;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        /* sync */
rpm-build 3ee90c
        if (services_action_sync(svc_action)) {
rpm-build 3ee90c
            rc = 0;
rpm-build 3ee90c
            action->rc = svc_action_to_errno(svc_action);
rpm-build 3ee90c
            action->output = svc_action->stdout_data;
rpm-build 3ee90c
            svc_action->stdout_data = NULL;
rpm-build 3ee90c
            action->error = svc_action->stderr_data;
rpm-build 3ee90c
            svc_action->stderr_data = NULL;
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            action->rc = -ECONNABORTED;
rpm-build 3ee90c
            rc = action->rc;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        svc_action->params = NULL;
rpm-build 3ee90c
        services_action_free(svc_action);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
  fail:
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Kick off execution of an async stonith action
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in,out] action        Action to be executed
rpm-build 3ee90c
 * \param[in,out] userdata      Datapointer to be passed to callbacks
rpm-build 3ee90c
 * \param[in]     done          Callback to notify action has failed/succeeded
rpm-build 3ee90c
 * \param[in]     fork_callback Callback to notify successful fork of child
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return pcmk_ok if ownership of action has been taken, -errno otherwise
rpm-build 3ee90c
 */
rpm-build 3ee90c
int
rpm-build 3ee90c
stonith_action_execute_async(stonith_action_t * action,
rpm-build 3ee90c
                             void *userdata,
rpm-build 3ee90c
                             void (*done) (GPid pid, int rc, const char *output,
rpm-build 3ee90c
                                           gpointer user_data),
rpm-build 3ee90c
                             void (*fork_cb) (GPid pid, gpointer user_data))
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (!action) {
rpm-build 3ee90c
        return -EINVAL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    action->userdata = userdata;
rpm-build 3ee90c
    action->done_cb = done;
rpm-build 3ee90c
    action->fork_cb = fork_cb;
rpm-build 3ee90c
    action->async = 1;
rpm-build 3ee90c
rpm-build 3ee90c
    return internal_stonith_action_execute(action);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Execute a stonith action
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in,out] action  Action to execute
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return pcmk_ok on success, -errno otherwise
rpm-build 3ee90c
 */
rpm-build 3ee90c
int
rpm-build 3ee90c
stonith__execute(stonith_action_t *action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(action != NULL, return -EINVAL);
rpm-build 3ee90c
rpm-build 3ee90c
    // Keep trying until success, max retries, or timeout
rpm-build 3ee90c
    do {
rpm-build 3ee90c
        rc = internal_stonith_action_execute(action);
rpm-build 3ee90c
    } while ((rc != pcmk_ok) && update_remaining_timeout(action));
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_device_list(stonith_t * stonith, int call_options, const char *namespace,
rpm-build 3ee90c
                        stonith_key_value_t ** devices, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int count = 0;
rpm-build 3ee90c
    enum stonith_namespace ns = stonith_text2namespace(namespace);
rpm-build 3ee90c
rpm-build 3ee90c
    if (devices == NULL) {
rpm-build 3ee90c
        crm_err("Parameter error: stonith_api_device_list");
rpm-build 3ee90c
        return -EFAULT;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
#if HAVE_STONITH_STONITH_H
rpm-build 3ee90c
    // Include Linux-HA agents if requested
rpm-build 3ee90c
    if ((ns == st_namespace_any) || (ns == st_namespace_lha)) {
rpm-build 3ee90c
        count += stonith__list_lha_agents(devices);
rpm-build 3ee90c
    }
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    // Include Red Hat agents if requested
rpm-build 3ee90c
    if ((ns == st_namespace_any) || (ns == st_namespace_rhcs)) {
rpm-build 3ee90c
        count += stonith__list_rhcs_agents(devices);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return count;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_device_metadata(stonith_t * stonith, int call_options, const char *agent,
rpm-build 3ee90c
                            const char *namespace, char **output, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    /* By executing meta-data directly, we can get it from stonith_admin when
rpm-build 3ee90c
     * the cluster is not running, which is important for higher-level tools.
rpm-build 3ee90c
     */
rpm-build 3ee90c
rpm-build 3ee90c
    enum stonith_namespace ns = stonith_get_namespace(agent, namespace);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Looking up metadata for %s agent %s",
rpm-build 3ee90c
              stonith_namespace2text(ns), agent);
rpm-build 3ee90c
rpm-build 3ee90c
    switch (ns) {
rpm-build 3ee90c
        case st_namespace_rhcs:
rpm-build 3ee90c
            return stonith__rhcs_metadata(agent, timeout, output);
rpm-build 3ee90c
rpm-build 3ee90c
#if HAVE_STONITH_STONITH_H
rpm-build 3ee90c
        case st_namespace_lha:
rpm-build 3ee90c
            return stonith__lha_metadata(agent, timeout, output);
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
        default:
rpm-build 3ee90c
            crm_err("Can't get fence agent '%s' meta-data: No such agent",
rpm-build 3ee90c
                    agent);
rpm-build 3ee90c
            break;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return -ENODEV;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_query(stonith_t * stonith, int call_options, const char *target,
rpm-build 3ee90c
                  stonith_key_value_t ** devices, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0, lpc = 0, max = 0;
rpm-build 3ee90c
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
    xmlNode *output = NULL;
rpm-build 3ee90c
    xmlXPathObjectPtr xpathObj = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(devices != NULL, return -EINVAL);
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, F_STONITH_DEVICE);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_TARGET, target);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ACTION, "off");
rpm-build 3ee90c
    rc = stonith_send_command(stonith, STONITH_OP_QUERY, data, &output, call_options, timeout);
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc < 0) {
rpm-build 3ee90c
        return rc;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    xpathObj = xpath_search(output, "//@agent");
rpm-build 3ee90c
    if (xpathObj) {
rpm-build 3ee90c
        max = numXpathResults(xpathObj);
rpm-build 3ee90c
rpm-build 3ee90c
        for (lpc = 0; lpc < max; lpc++) {
rpm-build 3ee90c
            xmlNode *match = getXpathResult(xpathObj, lpc);
rpm-build 3ee90c
rpm-build 3ee90c
            CRM_LOG_ASSERT(match != NULL);
rpm-build 3ee90c
            if(match != NULL) {
rpm-build 3ee90c
                xmlChar *match_path = xmlGetNodePath(match);
rpm-build 3ee90c
rpm-build 3ee90c
                crm_info("%s[%d] = %s", "//@agent", lpc, match_path);
rpm-build 3ee90c
                free(match_path);
rpm-build 3ee90c
                *devices = stonith_key_value_add(*devices, NULL, crm_element_value(match, XML_ATTR_ID));
rpm-build 3ee90c
            }
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        freeXpathObject(xpathObj);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(output);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
    return max;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_call(stonith_t * stonith,
rpm-build 3ee90c
                 int call_options,
rpm-build 3ee90c
                 const char *id,
rpm-build 3ee90c
                 const char *action, const char *victim, int timeout, xmlNode ** output)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, F_STONITH_DEVICE);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_DEVICE, id);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ACTION, action);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_TARGET, victim);
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_send_command(stonith, STONITH_OP_EXEC, data, output, call_options, timeout);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_list(stonith_t * stonith, int call_options, const char *id, char **list_info,
rpm-build 3ee90c
                 int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc;
rpm-build 3ee90c
    xmlNode *output = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_api_call(stonith, call_options, id, "list", NULL, timeout, &output);
rpm-build 3ee90c
rpm-build 3ee90c
    if (output && list_info) {
rpm-build 3ee90c
        const char *list_str;
rpm-build 3ee90c
rpm-build 3ee90c
        list_str = crm_element_value(output, "st_output");
rpm-build 3ee90c
rpm-build 3ee90c
        if (list_str) {
rpm-build 3ee90c
            *list_info = strdup(list_str);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (output) {
rpm-build 3ee90c
        free_xml(output);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_monitor(stonith_t * stonith, int call_options, const char *id, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_api_call(stonith, call_options, id, "monitor", NULL, timeout, NULL);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_status(stonith_t * stonith, int call_options, const char *id, const char *port,
rpm-build 3ee90c
                   int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_api_call(stonith, call_options, id, "status", port, timeout, NULL);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_fence(stonith_t * stonith, int call_options, const char *node, const char *action,
rpm-build 3ee90c
                  int timeout, int tolerance)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    data = create_xml_node(NULL, __FUNCTION__);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_TARGET, node);
rpm-build 3ee90c
    crm_xml_add(data, F_STONITH_ACTION, action);
rpm-build 3ee90c
    crm_xml_add_int(data, F_STONITH_TIMEOUT, timeout);
rpm-build 3ee90c
    crm_xml_add_int(data, F_STONITH_TOLERANCE, tolerance);
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_send_command(stonith, STONITH_OP_FENCE, data, NULL, call_options, timeout);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_confirm(stonith_t * stonith, int call_options, const char *target)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_api_fence(stonith, call_options | st_opt_manual_ack, target, "off", 0, 0);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_history(stonith_t * stonith, int call_options, const char *node,
rpm-build 3ee90c
                    stonith_history_t ** history, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    xmlNode *data = NULL;
rpm-build 3ee90c
    xmlNode *output = NULL;
rpm-build 3ee90c
    stonith_history_t *last = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    *history = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (node) {
rpm-build 3ee90c
        data = create_xml_node(NULL, __FUNCTION__);
rpm-build 3ee90c
        crm_xml_add(data, F_STONITH_TARGET, node);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    rc = stonith_send_command(stonith, STONITH_OP_FENCE_HISTORY, data, &output,
rpm-build 3ee90c
                              call_options | st_opt_sync_call, timeout);
rpm-build 3ee90c
    free_xml(data);
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == 0) {
rpm-build 3ee90c
        xmlNode *op = NULL;
rpm-build 3ee90c
        xmlNode *reply = get_xpath_object("//" F_STONITH_HISTORY_LIST, output, LOG_TRACE);
rpm-build 3ee90c
rpm-build 3ee90c
        for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
rpm-build 3ee90c
            stonith_history_t *kvp;
rpm-build 3ee90c
            int completed;
rpm-build 3ee90c
rpm-build 3ee90c
            kvp = calloc(1, sizeof(stonith_history_t));
rpm-build 3ee90c
            kvp->target = crm_element_value_copy(op, F_STONITH_TARGET);
rpm-build 3ee90c
            kvp->action = crm_element_value_copy(op, F_STONITH_ACTION);
rpm-build 3ee90c
            kvp->origin = crm_element_value_copy(op, F_STONITH_ORIGIN);
rpm-build 3ee90c
            kvp->delegate = crm_element_value_copy(op, F_STONITH_DELEGATE);
rpm-build 3ee90c
            kvp->client = crm_element_value_copy(op, F_STONITH_CLIENTNAME);
rpm-build 3ee90c
            crm_element_value_int(op, F_STONITH_DATE, &completed);
rpm-build 3ee90c
            kvp->completed = (time_t) completed;
rpm-build 3ee90c
            crm_element_value_int(op, F_STONITH_STATE, &kvp->state);
rpm-build 3ee90c
rpm-build 3ee90c
            if (last) {
rpm-build 3ee90c
                last->next = kvp;
rpm-build 3ee90c
            } else {
rpm-build 3ee90c
                *history = kvp;
rpm-build 3ee90c
            }
rpm-build 3ee90c
            last = kvp;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(output);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
void stonith_history_free(stonith_history_t *history)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_history_t *hp, *hp_old;
rpm-build 3ee90c
rpm-build 3ee90c
    for (hp = history; hp; hp_old = hp, hp = hp->next, free(hp_old)) {
rpm-build 3ee90c
        free(hp->target);
rpm-build 3ee90c
        free(hp->action);
rpm-build 3ee90c
        free(hp->origin);
rpm-build 3ee90c
        free(hp->delegate);
rpm-build 3ee90c
        free(hp->client);
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \brief Deprecated (use stonith_get_namespace() instead)
rpm-build 3ee90c
 */
rpm-build 3ee90c
const char *
rpm-build 3ee90c
get_stonith_provider(const char *agent, const char *provider)
rpm-build 3ee90c
{
rpm-build 3ee90c
    return stonith_namespace2text(stonith_get_namespace(agent, provider));
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static gint
rpm-build 3ee90c
stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    const stonith_notify_client_t *a_client = a;
rpm-build 3ee90c
    const stonith_notify_client_t *b_client = b;
rpm-build 3ee90c
rpm-build 3ee90c
    if (a_client->delete || b_client->delete) {
rpm-build 3ee90c
        /* make entries marked for deletion not findable */
rpm-build 3ee90c
        return -1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    CRM_CHECK(a_client->event != NULL && b_client->event != NULL, return 0);
rpm-build 3ee90c
    rc = strcmp(a_client->event, b_client->event);
rpm-build 3ee90c
    if (rc == 0) {
rpm-build 3ee90c
        if (a_client->notify == NULL || b_client->notify == NULL) {
rpm-build 3ee90c
            return 0;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (a_client->notify == b_client->notify) {
rpm-build 3ee90c
            return 0;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (((long)a_client->notify) < ((long)b_client->notify)) {
rpm-build 3ee90c
            crm_err("callbacks for %s are not equal: %p vs. %p",
rpm-build 3ee90c
                    a_client->event, a_client->notify, b_client->notify);
rpm-build 3ee90c
            return -1;
rpm-build 3ee90c
        }
rpm-build 3ee90c
        crm_err("callbacks for %s are not equal: %p vs. %p",
rpm-build 3ee90c
                a_client->event, a_client->notify, b_client->notify);
rpm-build 3ee90c
        return 1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
xmlNode *
rpm-build 3ee90c
stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data, int call_options)
rpm-build 3ee90c
{
rpm-build 3ee90c
    xmlNode *op_msg = create_xml_node(NULL, "stonith_command");
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(op_msg != NULL, return NULL);
rpm-build 3ee90c
    CRM_CHECK(token != NULL, return NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(op_msg, F_XML_TAGNAME, "stonith_command");
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(op_msg, F_TYPE, T_STONITH_NG);
rpm-build 3ee90c
    crm_xml_add(op_msg, F_STONITH_CALLBACK_TOKEN, token);
rpm-build 3ee90c
    crm_xml_add(op_msg, F_STONITH_OPERATION, op);
rpm-build 3ee90c
    crm_xml_add_int(op_msg, F_STONITH_CALLID, call_id);
rpm-build 3ee90c
    crm_trace("Sending call options: %.8lx, %d", (long)call_options, call_options);
rpm-build 3ee90c
    crm_xml_add_int(op_msg, F_STONITH_CALLOPTS, call_options);
rpm-build 3ee90c
rpm-build 3ee90c
    if (data != NULL) {
rpm-build 3ee90c
        add_message_xml(op_msg, F_STONITH_CALLDATA, data);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return op_msg;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_destroy_op_callback(gpointer data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_callback_client_t *blob = data;
rpm-build 3ee90c
rpm-build 3ee90c
    if (blob->timer && blob->timer->ref > 0) {
rpm-build 3ee90c
        g_source_remove(blob->timer->ref);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    free(blob->timer);
rpm-build 3ee90c
    free(blob);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_signoff(stonith_t * stonith)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_private_t *native = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Disconnecting from the fencer");
rpm-build 3ee90c
rpm-build 3ee90c
    if (native->source != NULL) {
rpm-build 3ee90c
        /* Attached to mainloop */
rpm-build 3ee90c
        mainloop_del_ipc_client(native->source);
rpm-build 3ee90c
        native->source = NULL;
rpm-build 3ee90c
        native->ipc = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (native->ipc) {
rpm-build 3ee90c
        /* Not attached to mainloop */
rpm-build 3ee90c
        crm_ipc_t *ipc = native->ipc;
rpm-build 3ee90c
rpm-build 3ee90c
        native->ipc = NULL;
rpm-build 3ee90c
        crm_ipc_close(ipc);
rpm-build 3ee90c
        crm_ipc_destroy(ipc);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free(native->token); native->token = NULL;
rpm-build 3ee90c
    stonith->state = stonith_disconnected;
rpm-build 3ee90c
    return pcmk_ok;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_del_callback(stonith_t * stonith, int call_id, bool all_callbacks)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_private_t *private = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (all_callbacks) {
rpm-build 3ee90c
        private->op_callback = NULL;
rpm-build 3ee90c
        g_hash_table_destroy(private->stonith_op_callback_table);
rpm-build 3ee90c
        private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
rpm-build 3ee90c
                                                                   NULL,
rpm-build 3ee90c
                                                                   stonith_destroy_op_callback);
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (call_id == 0) {
rpm-build 3ee90c
        private->op_callback = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return pcmk_ok;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
invoke_callback(stonith_t * st, int call_id, int rc, void *userdata,
rpm-build 3ee90c
                void (*callback) (stonith_t * st, stonith_callback_data_t * data))
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_callback_data_t data = { 0, };
rpm-build 3ee90c
rpm-build 3ee90c
    data.call_id = call_id;
rpm-build 3ee90c
    data.rc = rc;
rpm-build 3ee90c
    data.userdata = userdata;
rpm-build 3ee90c
rpm-build 3ee90c
    callback(st, &data);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_perform_callback(stonith_t * stonith, xmlNode * msg, int call_id, int rc)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
    stonith_callback_client_t *blob = NULL;
rpm-build 3ee90c
    stonith_callback_client_t local_blob;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(stonith != NULL, return);
rpm-build 3ee90c
    CRM_CHECK(stonith->st_private != NULL, return);
rpm-build 3ee90c
rpm-build 3ee90c
    private = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    local_blob.id = NULL;
rpm-build 3ee90c
    local_blob.callback = NULL;
rpm-build 3ee90c
    local_blob.user_data = NULL;
rpm-build 3ee90c
    local_blob.only_success = FALSE;
rpm-build 3ee90c
rpm-build 3ee90c
    if (msg != NULL) {
rpm-build 3ee90c
        crm_element_value_int(msg, F_STONITH_RC, &rc);
rpm-build 3ee90c
        crm_element_value_int(msg, F_STONITH_CALLID, &call_id);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(call_id > 0, crm_log_xml_err(msg, "Bad result"));
rpm-build 3ee90c
rpm-build 3ee90c
    blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
rpm-build 3ee90c
rpm-build 3ee90c
    if (blob != NULL) {
rpm-build 3ee90c
        local_blob = *blob;
rpm-build 3ee90c
        blob = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
        stonith_api_del_callback(stonith, call_id, FALSE);
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_trace("No callback found for call %d", call_id);
rpm-build 3ee90c
        local_blob.callback = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (local_blob.callback != NULL && (rc == pcmk_ok || local_blob.only_success == FALSE)) {
rpm-build 3ee90c
        crm_trace("Invoking callback %s for call %d", crm_str(local_blob.id), call_id);
rpm-build 3ee90c
        invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (private->op_callback == NULL && rc != pcmk_ok) {
rpm-build 3ee90c
        crm_warn("Fencing command failed: %s", pcmk_strerror(rc));
rpm-build 3ee90c
        crm_log_xml_debug(msg, "Failed fence update");
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->op_callback != NULL) {
rpm-build 3ee90c
        crm_trace("Invoking global callback for call %d", call_id);
rpm-build 3ee90c
        invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    crm_trace("OP callback activated.");
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static gboolean
rpm-build 3ee90c
stonith_async_timeout_handler(gpointer data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    struct timer_rec_s *timer = data;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_err("Async call %d timed out after %dms", timer->call_id, timer->timeout);
rpm-build 3ee90c
    stonith_perform_callback(timer->stonith, NULL, timer->call_id, -ETIME);
rpm-build 3ee90c
rpm-build 3ee90c
    /* Always return TRUE, never remove the handler
rpm-build 3ee90c
     * We do that in stonith_del_callback()
rpm-build 3ee90c
     */
rpm-build 3ee90c
    return TRUE;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
set_callback_timeout(stonith_callback_client_t * callback, stonith_t * stonith, int call_id,
rpm-build 3ee90c
                     int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    struct timer_rec_s *async_timer = callback->timer;
rpm-build 3ee90c
rpm-build 3ee90c
    if (timeout <= 0) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (!async_timer) {
rpm-build 3ee90c
        async_timer = calloc(1, sizeof(struct timer_rec_s));
rpm-build 3ee90c
        callback->timer = async_timer;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    async_timer->stonith = stonith;
rpm-build 3ee90c
    async_timer->call_id = call_id;
rpm-build 3ee90c
    /* Allow a fair bit of grace to allow the server to tell us of a timeout
rpm-build 3ee90c
     * This is only a fallback
rpm-build 3ee90c
     */
rpm-build 3ee90c
    async_timer->timeout = (timeout + 60) * 1000;
rpm-build 3ee90c
    if (async_timer->ref) {
rpm-build 3ee90c
        g_source_remove(async_timer->ref);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    async_timer->ref =
rpm-build 3ee90c
        g_timeout_add(async_timer->timeout, stonith_async_timeout_handler, async_timer);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
update_callback_timeout(int call_id, int timeout, stonith_t * st)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_callback_client_t *callback = NULL;
rpm-build 3ee90c
    stonith_private_t *private = st->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
rpm-build 3ee90c
    if (!callback || !callback->allow_timeout_updates) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    set_callback_timeout(callback, st, call_id, timeout);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
rpm-build 3ee90c
{
rpm-build 3ee90c
    const char *type = NULL;
rpm-build 3ee90c
    struct notify_blob_s blob;
rpm-build 3ee90c
rpm-build 3ee90c
    stonith_t *st = userdata;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_ASSERT(st != NULL);
rpm-build 3ee90c
    private = st->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    blob.stonith = st;
rpm-build 3ee90c
    blob.xml = string2xml(buffer);
rpm-build 3ee90c
    if (blob.xml == NULL) {
rpm-build 3ee90c
        crm_warn("Received malformed message from fencer: %s", buffer);
rpm-build 3ee90c
        return 0;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    /* do callbacks */
rpm-build 3ee90c
    type = crm_element_value(blob.xml, F_TYPE);
rpm-build 3ee90c
    crm_trace("Activating %s callbacks...", type);
rpm-build 3ee90c
rpm-build 3ee90c
    if (safe_str_eq(type, T_STONITH_NG)) {
rpm-build 3ee90c
        stonith_perform_callback(st, blob.xml, 0, 0);
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (safe_str_eq(type, T_STONITH_NOTIFY)) {
rpm-build 3ee90c
        foreach_notify_entry(private, stonith_send_notification, &blob;;
rpm-build 3ee90c
    } else if (safe_str_eq(type, T_STONITH_TIMEOUT_VALUE)) {
rpm-build 3ee90c
        int call_id = 0;
rpm-build 3ee90c
        int timeout = 0;
rpm-build 3ee90c
rpm-build 3ee90c
        crm_element_value_int(blob.xml, F_STONITH_TIMEOUT, &timeout);
rpm-build 3ee90c
        crm_element_value_int(blob.xml, F_STONITH_CALLID, &call_id);
rpm-build 3ee90c
rpm-build 3ee90c
        update_callback_timeout(call_id, timeout, st);
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_err("Unknown message type: %s", type);
rpm-build 3ee90c
        crm_log_xml_warn(blob.xml, "BadReply");
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(blob.xml);
rpm-build 3ee90c
    return 1;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_signon(stonith_t * stonith, const char *name, int *stonith_fd)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    stonith_private_t *native = NULL;
rpm-build 3ee90c
    const char *display_name = name? name : "client";
rpm-build 3ee90c
rpm-build 3ee90c
    static struct ipc_client_callbacks st_callbacks = {
rpm-build 3ee90c
        .dispatch = stonith_dispatch_internal,
rpm-build 3ee90c
        .destroy = stonith_connection_destroy
rpm-build 3ee90c
    };
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(stonith != NULL, return -EINVAL);
rpm-build 3ee90c
rpm-build 3ee90c
    native = stonith->st_private;
rpm-build 3ee90c
    CRM_ASSERT(native != NULL);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Attempting fencer connection by %s with%s mainloop",
rpm-build 3ee90c
              display_name, (stonith_fd? "out" : ""));
rpm-build 3ee90c
rpm-build 3ee90c
    stonith->state = stonith_connected_command;
rpm-build 3ee90c
    if (stonith_fd) {
rpm-build 3ee90c
        /* No mainloop */
rpm-build 3ee90c
        native->ipc = crm_ipc_new("stonith-ng", 0);
rpm-build 3ee90c
rpm-build 3ee90c
        if (native->ipc && crm_ipc_connect(native->ipc)) {
rpm-build 3ee90c
            *stonith_fd = crm_ipc_get_fd(native->ipc);
rpm-build 3ee90c
        } else if (native->ipc) {
rpm-build 3ee90c
            crm_ipc_close(native->ipc);
rpm-build 3ee90c
            crm_ipc_destroy(native->ipc);
rpm-build 3ee90c
            native->ipc = NULL;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        /* With mainloop */
rpm-build 3ee90c
        native->source =
rpm-build 3ee90c
            mainloop_add_ipc_client("stonith-ng", G_PRIORITY_MEDIUM, 0, stonith, &st_callbacks);
rpm-build 3ee90c
        native->ipc = mainloop_get_ipc_client(native->source);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (native->ipc == NULL) {
rpm-build 3ee90c
        rc = -ENOTCONN;
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        xmlNode *reply = NULL;
rpm-build 3ee90c
        xmlNode *hello = create_xml_node(NULL, "stonith_command");
rpm-build 3ee90c
rpm-build 3ee90c
        crm_xml_add(hello, F_TYPE, T_STONITH_NG);
rpm-build 3ee90c
        crm_xml_add(hello, F_STONITH_OPERATION, CRM_OP_REGISTER);
rpm-build 3ee90c
        crm_xml_add(hello, F_STONITH_CLIENTNAME, name);
rpm-build 3ee90c
        rc = crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply);
rpm-build 3ee90c
rpm-build 3ee90c
        if (rc < 0) {
rpm-build 3ee90c
            crm_debug("Couldn't register with the fencer: %s "
rpm-build 3ee90c
                      CRM_XS " rc=%d", pcmk_strerror(rc), rc);
rpm-build 3ee90c
            rc = -ECOMM;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (reply == NULL) {
rpm-build 3ee90c
            crm_debug("Couldn't register with the fencer: no reply");
rpm-build 3ee90c
            rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            const char *msg_type = crm_element_value(reply, F_STONITH_OPERATION);
rpm-build 3ee90c
rpm-build 3ee90c
            native->token = crm_element_value_copy(reply, F_STONITH_CLIENTID);
rpm-build 3ee90c
            if (safe_str_neq(msg_type, CRM_OP_REGISTER)) {
rpm-build 3ee90c
                crm_debug("Couldn't register with the fencer: invalid reply type '%s'",
rpm-build 3ee90c
                          (msg_type? msg_type : "(missing)"));
rpm-build 3ee90c
                crm_log_xml_debug(reply, "Invalid fencer reply");
rpm-build 3ee90c
                rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
            } else if (native->token == NULL) {
rpm-build 3ee90c
                crm_debug("Couldn't register with the fencer: no token in reply");
rpm-build 3ee90c
                crm_log_xml_debug(reply, "Invalid fencer reply");
rpm-build 3ee90c
                rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
            } else {
rpm-build 3ee90c
#if HAVE_MSGFROMIPC_TIMEOUT
rpm-build 3ee90c
                stonith->call_timeout = MAX_IPC_DELAY;
rpm-build 3ee90c
#endif
rpm-build 3ee90c
                crm_debug("Connection to fencer by %s succeeded (registration token: %s)",
rpm-build 3ee90c
                          display_name, native->token);
rpm-build 3ee90c
                rc = pcmk_ok;
rpm-build 3ee90c
            }
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        free_xml(reply);
rpm-build 3ee90c
        free_xml(hello);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc != pcmk_ok) {
rpm-build 3ee90c
        crm_debug("Connection attempt to fencer by %s failed: %s "
rpm-build 3ee90c
                  CRM_XS " rc=%d", display_name, pcmk_strerror(rc), rc);
rpm-build 3ee90c
        stonith->cmds->disconnect(stonith);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_set_notification(stonith_t * stonith, const char *callback, int enabled)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    xmlNode *notify_msg = create_xml_node(NULL, __FUNCTION__);
rpm-build 3ee90c
    stonith_private_t *native = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (stonith->state != stonith_disconnected) {
rpm-build 3ee90c
rpm-build 3ee90c
        crm_xml_add(notify_msg, F_STONITH_OPERATION, T_STONITH_NOTIFY);
rpm-build 3ee90c
        if (enabled) {
rpm-build 3ee90c
            crm_xml_add(notify_msg, F_STONITH_NOTIFY_ACTIVATE, callback);
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            crm_xml_add(notify_msg, F_STONITH_NOTIFY_DEACTIVATE, callback);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response, -1, NULL);
rpm-build 3ee90c
        if (rc < 0) {
rpm-build 3ee90c
            crm_perror(LOG_DEBUG, "Couldn't register for fencing notifications: %d", rc);
rpm-build 3ee90c
            rc = -ECOMM;
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            rc = pcmk_ok;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(notify_msg);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_add_notification(stonith_t * stonith, const char *event,
rpm-build 3ee90c
                             void (*callback) (stonith_t * stonith, stonith_event_t * e))
rpm-build 3ee90c
{
rpm-build 3ee90c
    GList *list_item = NULL;
rpm-build 3ee90c
    stonith_notify_client_t *new_client = NULL;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    private = stonith->st_private;
rpm-build 3ee90c
    crm_trace("Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
rpm-build 3ee90c
rpm-build 3ee90c
    new_client = calloc(1, sizeof(stonith_notify_client_t));
rpm-build 3ee90c
    new_client->event = event;
rpm-build 3ee90c
    new_client->notify = callback;
rpm-build 3ee90c
rpm-build 3ee90c
    list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
rpm-build 3ee90c
rpm-build 3ee90c
    if (list_item != NULL) {
rpm-build 3ee90c
        crm_warn("Callback already present");
rpm-build 3ee90c
        free(new_client);
rpm-build 3ee90c
        return -ENOTUNIQ;
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        private->notify_list = g_list_append(private->notify_list, new_client);
rpm-build 3ee90c
rpm-build 3ee90c
        stonith_set_notification(stonith, event, 1);
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Callback added (%d)", g_list_length(private->notify_list));
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return pcmk_ok;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_del_notification(stonith_t * stonith, const char *event)
rpm-build 3ee90c
{
rpm-build 3ee90c
    GList *list_item = NULL;
rpm-build 3ee90c
    stonith_notify_client_t *new_client = NULL;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Removing callback for %s events", event);
rpm-build 3ee90c
rpm-build 3ee90c
    private = stonith->st_private;
rpm-build 3ee90c
    new_client = calloc(1, sizeof(stonith_notify_client_t));
rpm-build 3ee90c
    new_client->event = event;
rpm-build 3ee90c
    new_client->notify = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
rpm-build 3ee90c
rpm-build 3ee90c
    stonith_set_notification(stonith, event, 0);
rpm-build 3ee90c
rpm-build 3ee90c
    if (list_item != NULL) {
rpm-build 3ee90c
        stonith_notify_client_t *list_client = list_item->data;
rpm-build 3ee90c
rpm-build 3ee90c
        if (private->notify_refcnt) {
rpm-build 3ee90c
            list_client->delete = TRUE;
rpm-build 3ee90c
            private->notify_deletes = TRUE;
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            private->notify_list = g_list_remove(private->notify_list, list_client);
rpm-build 3ee90c
            free(list_client);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Removed callback");
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_trace("Callback not present");
rpm-build 3ee90c
    }
rpm-build 3ee90c
    free(new_client);
rpm-build 3ee90c
    return pcmk_ok;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_add_callback(stonith_t * stonith, int call_id, int timeout, int options,
rpm-build 3ee90c
                         void *user_data, const char *callback_name,
rpm-build 3ee90c
                         void (*callback) (stonith_t * st, stonith_callback_data_t * data))
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_callback_client_t *blob = NULL;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_CHECK(stonith != NULL, return -EINVAL);
rpm-build 3ee90c
    CRM_CHECK(stonith->st_private != NULL, return -EINVAL);
rpm-build 3ee90c
    private = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (call_id == 0) {
rpm-build 3ee90c
        private->op_callback = callback;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (call_id < 0) {
rpm-build 3ee90c
        if (!(options & st_opt_report_only_success)) {
rpm-build 3ee90c
            crm_trace("Call failed, calling %s: %s", callback_name, pcmk_strerror(call_id));
rpm-build 3ee90c
            invoke_callback(stonith, call_id, call_id, user_data, callback);
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            crm_warn("Fencer call failed: %s", pcmk_strerror(call_id));
rpm-build 3ee90c
        }
rpm-build 3ee90c
        return FALSE;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    blob = calloc(1, sizeof(stonith_callback_client_t));
rpm-build 3ee90c
    blob->id = callback_name;
rpm-build 3ee90c
    blob->only_success = (options & st_opt_report_only_success) ? TRUE : FALSE;
rpm-build 3ee90c
    blob->user_data = user_data;
rpm-build 3ee90c
    blob->callback = callback;
rpm-build 3ee90c
    blob->allow_timeout_updates = (options & st_opt_timeout_updates) ? TRUE : FALSE;
rpm-build 3ee90c
rpm-build 3ee90c
    if (timeout > 0) {
rpm-build 3ee90c
        set_callback_timeout(blob, stonith, call_id, timeout);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
rpm-build 3ee90c
    crm_trace("Added callback to %s for call %d", callback_name, call_id);
rpm-build 3ee90c
rpm-build 3ee90c
    return TRUE;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int call = GPOINTER_TO_INT(key);
rpm-build 3ee90c
    stonith_callback_client_t *blob = value;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Call %d (%s): pending", call, crm_str(blob->id));
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
void
rpm-build 3ee90c
stonith_dump_pending_callbacks(stonith_t * stonith)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_private_t *private = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->stonith_op_callback_table == NULL) {
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*
rpm-build 3ee90c
 <notify t="st_notify" subt="st_device_register" st_op="st_device_register" st_rc="0" >
rpm-build 3ee90c
   <st_calldata >
rpm-build 3ee90c
     <stonith_command t="stonith-ng" st_async_id="088fb640-431a-48b9-b2fc-c4ff78d0a2d9" st_op="st_device_register" st_callid="2" st_callopt="4096" st_timeout="0" st_clientid="088fb640-431a-48b9-b2fc-c4ff78d0a2d9" st_clientname="cts-fence-helper" >
rpm-build 3ee90c
       <st_calldata >
rpm-build 3ee90c
         <st_device_id id="test-id" origin="create_device_registration_xml" agent="fence_virsh" namespace="stonith-ng" >
rpm-build 3ee90c
           <attributes ipaddr="localhost" pcmk-portmal="some-host=pcmk-1 pcmk-3=3,4" login="root" identity_file="/root/.ssh/id_dsa" />
rpm-build 3ee90c
         </st_device_id>
rpm-build 3ee90c
       </st_calldata>
rpm-build 3ee90c
     </stonith_command>
rpm-build 3ee90c
   </st_calldata>
rpm-build 3ee90c
 </notify>
rpm-build 3ee90c
rpm-build 3ee90c
 <notify t="st_notify" subt="st_notify_fence" st_op="st_notify_fence" st_rc="0" >
rpm-build 3ee90c
   <st_calldata >
rpm-build 3ee90c
     <st_notify_fence st_rc="0" st_target="some-host" st_op="st_fence" st_delegate="test-id" st_origin="61dd7759-e229-4be7-b1f8-ef49dd14d9f0" />
rpm-build 3ee90c
   </st_calldata>
rpm-build 3ee90c
 </notify>
rpm-build 3ee90c
*/
rpm-build 3ee90c
static stonith_event_t *
rpm-build 3ee90c
xml_to_event(xmlNode * msg)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_event_t *event = calloc(1, sizeof(stonith_event_t));
rpm-build 3ee90c
    const char *ntype = crm_element_value(msg, F_SUBTYPE);
rpm-build 3ee90c
    char *data_addr = crm_strdup_printf("//%s", ntype);
rpm-build 3ee90c
    xmlNode *data = get_xpath_object(data_addr, msg, LOG_DEBUG);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_log_xml_trace(msg, "stonith_notify");
rpm-build 3ee90c
rpm-build 3ee90c
    crm_element_value_int(msg, F_STONITH_RC, &(event->result));
rpm-build 3ee90c
rpm-build 3ee90c
    if (safe_str_eq(ntype, T_STONITH_NOTIFY_FENCE)) {
rpm-build 3ee90c
        event->operation = crm_element_value_copy(msg, F_STONITH_OPERATION);
rpm-build 3ee90c
rpm-build 3ee90c
        if (data) {
rpm-build 3ee90c
            event->origin = crm_element_value_copy(data, F_STONITH_ORIGIN);
rpm-build 3ee90c
            event->action = crm_element_value_copy(data, F_STONITH_ACTION);
rpm-build 3ee90c
            event->target = crm_element_value_copy(data, F_STONITH_TARGET);
rpm-build 3ee90c
            event->executioner = crm_element_value_copy(data, F_STONITH_DELEGATE);
rpm-build 3ee90c
            event->id = crm_element_value_copy(data, F_STONITH_REMOTE_OP_ID);
rpm-build 3ee90c
            event->client_origin = crm_element_value_copy(data, F_STONITH_CLIENTNAME);
rpm-build 3ee90c
            event->device = crm_element_value_copy(data, F_STONITH_DEVICE);
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            crm_err("No data for %s event", ntype);
rpm-build 3ee90c
            crm_log_xml_notice(msg, "BadEvent");
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free(data_addr);
rpm-build 3ee90c
    return event;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
event_free(stonith_event_t * event)
rpm-build 3ee90c
{
rpm-build 3ee90c
    free(event->id);
rpm-build 3ee90c
    free(event->type);
rpm-build 3ee90c
    free(event->message);
rpm-build 3ee90c
    free(event->operation);
rpm-build 3ee90c
    free(event->origin);
rpm-build 3ee90c
    free(event->action);
rpm-build 3ee90c
    free(event->target);
rpm-build 3ee90c
    free(event->executioner);
rpm-build 3ee90c
    free(event->device);
rpm-build 3ee90c
    free(event->client_origin);
rpm-build 3ee90c
    free(event);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static void
rpm-build 3ee90c
stonith_send_notification(gpointer data, gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    struct notify_blob_s *blob = user_data;
rpm-build 3ee90c
    stonith_notify_client_t *entry = data;
rpm-build 3ee90c
    stonith_event_t *st_event = NULL;
rpm-build 3ee90c
    const char *event = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (blob->xml == NULL) {
rpm-build 3ee90c
        crm_warn("Skipping callback - NULL message");
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    event = crm_element_value(blob->xml, F_SUBTYPE);
rpm-build 3ee90c
rpm-build 3ee90c
    if (entry == NULL) {
rpm-build 3ee90c
        crm_warn("Skipping callback - NULL callback client");
rpm-build 3ee90c
        return;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (entry->delete) {
rpm-build 3ee90c
        crm_trace("Skipping callback - marked for deletion");
rpm-build 3ee90c
        return;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (entry->notify == NULL) {
rpm-build 3ee90c
        crm_warn("Skipping callback - NULL callback");
rpm-build 3ee90c
        return;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (safe_str_neq(entry->event, event)) {
rpm-build 3ee90c
        crm_trace("Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    st_event = xml_to_event(blob->xml);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Invoking callback for %p/%s event...", entry, event);
rpm-build 3ee90c
    entry->notify(blob->stonith, st_event);
rpm-build 3ee90c
    crm_trace("Callback invoked...");
rpm-build 3ee90c
rpm-build 3ee90c
    event_free(st_event);
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Create and send an API request
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in]  stonith       Stonith connection
rpm-build 3ee90c
 * \param[in]  op            API operation to request
rpm-build 3ee90c
 * \param[in]  data          Data to attach to request
rpm-build 3ee90c
 * \param[out] output_data   If not NULL, will be set to reply if synchronous
rpm-build 3ee90c
 * \param[in]  call_options  Bitmask of stonith_call_options to use
rpm-build 3ee90c
 * \param[in]  timeout       Error if not completed within this many seconds
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return pcmk_ok (for synchronous requests) or positive call ID
rpm-build 3ee90c
 *         (for asynchronous requests) on success, -errno otherwise
rpm-build 3ee90c
 */
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_send_command(stonith_t * stonith, const char *op, xmlNode * data, xmlNode ** output_data,
rpm-build 3ee90c
                     int call_options, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    int reply_id = -1;
rpm-build 3ee90c
rpm-build 3ee90c
    xmlNode *op_msg = NULL;
rpm-build 3ee90c
    xmlNode *op_reply = NULL;
rpm-build 3ee90c
    stonith_private_t *native = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_ASSERT(stonith && stonith->st_private && op);
rpm-build 3ee90c
    native = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (output_data != NULL) {
rpm-build 3ee90c
        *output_data = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if ((stonith->state == stonith_disconnected) || (native->token == NULL)) {
rpm-build 3ee90c
        return -ENOTCONN;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    /* Increment the call ID, which must be positive to avoid conflicting with
rpm-build 3ee90c
     * error codes. This shouldn't be a problem unless the client mucked with
rpm-build 3ee90c
     * it or the counter wrapped around.
rpm-build 3ee90c
     */
rpm-build 3ee90c
    stonith->call_id++;
rpm-build 3ee90c
    if (stonith->call_id < 1) {
rpm-build 3ee90c
        stonith->call_id = 1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    op_msg = stonith_create_op(stonith->call_id, native->token, op, data, call_options);
rpm-build 3ee90c
    if (op_msg == NULL) {
rpm-build 3ee90c
        return -EINVAL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add_int(op_msg, F_STONITH_TIMEOUT, timeout);
rpm-build 3ee90c
    crm_trace("Sending %s message to fencer with timeout %ds", op, timeout);
rpm-build 3ee90c
rpm-build 3ee90c
    {
rpm-build 3ee90c
        enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
rpm-build 3ee90c
rpm-build 3ee90c
        if (call_options & st_opt_sync_call) {
rpm-build 3ee90c
            ipc_flags |= crm_ipc_client_response;
rpm-build 3ee90c
        }
rpm-build 3ee90c
        rc = crm_ipc_send(native->ipc, op_msg, ipc_flags,
rpm-build 3ee90c
                          1000 * (timeout + 60), &op_reply);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    free_xml(op_msg);
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc < 0) {
rpm-build 3ee90c
        crm_perror(LOG_ERR, "Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
rpm-build 3ee90c
        rc = -ECOMM;
rpm-build 3ee90c
        goto done;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_log_xml_trace(op_reply, "Reply");
rpm-build 3ee90c
rpm-build 3ee90c
    if (!(call_options & st_opt_sync_call)) {
rpm-build 3ee90c
        crm_trace("Async call %d, returning", stonith->call_id);
rpm-build 3ee90c
        free_xml(op_reply);
rpm-build 3ee90c
        return stonith->call_id;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    rc = pcmk_ok;
rpm-build 3ee90c
    crm_element_value_int(op_reply, F_STONITH_CALLID, &reply_id);
rpm-build 3ee90c
rpm-build 3ee90c
    if (reply_id == stonith->call_id) {
rpm-build 3ee90c
        crm_trace("Synchronous reply %d received", reply_id);
rpm-build 3ee90c
rpm-build 3ee90c
        if (crm_element_value_int(op_reply, F_STONITH_RC, &rc) != 0) {
rpm-build 3ee90c
            rc = -ENOMSG;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        if ((call_options & st_opt_discard_reply) || output_data == NULL) {
rpm-build 3ee90c
            crm_trace("Discarding reply");
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            *output_data = op_reply;
rpm-build 3ee90c
            op_reply = NULL;    /* Prevent subsequent free */
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (reply_id <= 0) {
rpm-build 3ee90c
        crm_err("Received bad reply: No id set");
rpm-build 3ee90c
        crm_log_xml_err(op_reply, "Bad reply");
rpm-build 3ee90c
        free_xml(op_reply);
rpm-build 3ee90c
        rc = -ENOMSG;
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_err("Received bad reply: %d (wanted %d)", reply_id, stonith->call_id);
rpm-build 3ee90c
        crm_log_xml_err(op_reply, "Old reply");
rpm-build 3ee90c
        free_xml(op_reply);
rpm-build 3ee90c
        rc = -ENOMSG;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
  done:
rpm-build 3ee90c
    if (crm_ipc_connected(native->ipc) == FALSE) {
rpm-build 3ee90c
        crm_err("Fencer disconnected");
rpm-build 3ee90c
        free(native->token); native->token = NULL;
rpm-build 3ee90c
        stonith->state = stonith_disconnected;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(op_reply);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/* Not used with mainloop */
rpm-build 3ee90c
bool
rpm-build 3ee90c
stonith_dispatch(stonith_t * st)
rpm-build 3ee90c
{
rpm-build 3ee90c
    gboolean stay_connected = TRUE;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    CRM_ASSERT(st != NULL);
rpm-build 3ee90c
    private = st->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
    while (crm_ipc_ready(private->ipc)) {
rpm-build 3ee90c
rpm-build 3ee90c
        if (crm_ipc_read(private->ipc) > 0) {
rpm-build 3ee90c
            const char *msg = crm_ipc_buffer(private->ipc);
rpm-build 3ee90c
rpm-build 3ee90c
            stonith_dispatch_internal(msg, strlen(msg), st);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        if (crm_ipc_connected(private->ipc) == FALSE) {
rpm-build 3ee90c
            crm_err("Connection closed");
rpm-build 3ee90c
            stay_connected = FALSE;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return stay_connected;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_free(stonith_t * stonith)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Destroying %p", stonith);
rpm-build 3ee90c
rpm-build 3ee90c
    if (stonith->state != stonith_disconnected) {
rpm-build 3ee90c
        crm_trace("Disconnecting %p first", stonith);
rpm-build 3ee90c
        rc = stonith->cmds->disconnect(stonith);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (stonith->state == stonith_disconnected) {
rpm-build 3ee90c
        stonith_private_t *private = stonith->st_private;
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
rpm-build 3ee90c
        g_hash_table_destroy(private->stonith_op_callback_table);
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Destroying %d notification clients", g_list_length(private->notify_list));
rpm-build 3ee90c
        g_list_free_full(private->notify_list, free);
rpm-build 3ee90c
rpm-build 3ee90c
        free(stonith->st_private);
rpm-build 3ee90c
        free(stonith->cmds);
rpm-build 3ee90c
        free(stonith);
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_err("Not free'ing active connection: %s (%d)", pcmk_strerror(rc), rc);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
void
rpm-build 3ee90c
stonith_api_delete(stonith_t * stonith)
rpm-build 3ee90c
{
rpm-build 3ee90c
    crm_trace("Destroying %p", stonith);
rpm-build 3ee90c
    if(stonith) {
rpm-build 3ee90c
        stonith->cmds->free(stonith);
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
stonith_api_validate(stonith_t *st, int call_options, const char *rsc_id,
rpm-build 3ee90c
                     const char *namespace_s, const char *agent,
rpm-build 3ee90c
                     stonith_key_value_t *params, int timeout, char **output,
rpm-build 3ee90c
                     char **error_output)
rpm-build 3ee90c
{
rpm-build 3ee90c
    /* Validation should be done directly via the agent, so we can get it from
rpm-build 3ee90c
     * stonith_admin when the cluster is not running, which is important for
rpm-build 3ee90c
     * higher-level tools.
rpm-build 3ee90c
     */
rpm-build 3ee90c
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    /* Use a dummy node name in case the agent requires a target. We assume the
rpm-build 3ee90c
     * actual target doesn't matter for validation purposes (if in practice,
rpm-build 3ee90c
     * that is incorrect, we will need to allow the caller to pass the target).
rpm-build 3ee90c
     */
rpm-build 3ee90c
    const char *target = "node1";
rpm-build 3ee90c
rpm-build 3ee90c
    GHashTable *params_table = crm_str_table_new();
rpm-build 3ee90c
rpm-build 3ee90c
    // Convert parameter list to a hash table
rpm-build 3ee90c
    for (; params; params = params->next) {
rpm-build 3ee90c
rpm-build 3ee90c
        // Strip out Pacemaker-implemented parameters
rpm-build 3ee90c
        if (!crm_starts_with(params->key, "pcmk_")
rpm-build 3ee90c
                && strcmp(params->key, "provides")
rpm-build 3ee90c
                && strcmp(params->key, "stonith-timeout")) {
rpm-build 3ee90c
            g_hash_table_insert(params_table, strdup(params->key),
rpm-build 3ee90c
                                strdup(params->value));
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
#if SUPPORT_CIBSECRETS
rpm-build 3ee90c
    rc = replace_secret_params(rsc_id, params_table);
rpm-build 3ee90c
    if (rc < 0) {
rpm-build 3ee90c
        crm_warn("Could not replace secret parameters for validation of %s: %s",
rpm-build 3ee90c
                 agent, pcmk_strerror(rc));
rpm-build 3ee90c
    }
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    if (output) {
rpm-build 3ee90c
        *output = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (error_output) {
rpm-build 3ee90c
        *error_output = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    switch (stonith_get_namespace(agent, namespace_s)) {
rpm-build 3ee90c
        case st_namespace_rhcs:
rpm-build 3ee90c
            rc = stonith__rhcs_validate(st, call_options, target, agent,
rpm-build 3ee90c
                                        params_table, timeout, output,
rpm-build 3ee90c
                                        error_output);
rpm-build 3ee90c
            break;
rpm-build 3ee90c
rpm-build 3ee90c
#if HAVE_STONITH_STONITH_H
rpm-build 3ee90c
        case st_namespace_lha:
rpm-build 3ee90c
            rc = stonith__lha_validate(st, call_options, target, agent,
rpm-build 3ee90c
                                       params_table, timeout, output,
rpm-build 3ee90c
                                       error_output);
rpm-build 3ee90c
            break;
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
        default:
rpm-build 3ee90c
            rc = -EINVAL;
rpm-build 3ee90c
            errno = EINVAL;
rpm-build 3ee90c
            crm_perror(LOG_ERR,
rpm-build 3ee90c
                       "Agent %s not found or does not support validation",
rpm-build 3ee90c
                       agent);
rpm-build 3ee90c
            break;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    g_hash_table_destroy(params_table);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
stonith_t *
rpm-build 3ee90c
stonith_api_new(void)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_t *new_stonith = NULL;
rpm-build 3ee90c
    stonith_private_t *private = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith = calloc(1, sizeof(stonith_t));
rpm-build 3ee90c
    if (new_stonith == NULL) {
rpm-build 3ee90c
        return NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    private = calloc(1, sizeof(stonith_private_t));
rpm-build 3ee90c
    if (private == NULL) {
rpm-build 3ee90c
        free(new_stonith);
rpm-build 3ee90c
        return NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    new_stonith->st_private = private;
rpm-build 3ee90c
rpm-build 3ee90c
    private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
rpm-build 3ee90c
                                                               NULL, stonith_destroy_op_callback);
rpm-build 3ee90c
    private->notify_list = NULL;
rpm-build 3ee90c
    private->notify_refcnt = 0;
rpm-build 3ee90c
    private->notify_deletes = FALSE;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->call_id = 1;
rpm-build 3ee90c
    new_stonith->state = stonith_disconnected;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds = calloc(1, sizeof(stonith_api_operations_t));
rpm-build 3ee90c
    if (new_stonith->cmds == NULL) {
rpm-build 3ee90c
        free(new_stonith->st_private);
rpm-build 3ee90c
        free(new_stonith);
rpm-build 3ee90c
        return NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
/* *INDENT-OFF* */
rpm-build 3ee90c
    new_stonith->cmds->free       = stonith_api_free;
rpm-build 3ee90c
    new_stonith->cmds->connect    = stonith_api_signon;
rpm-build 3ee90c
    new_stonith->cmds->disconnect = stonith_api_signoff;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->list       = stonith_api_list;
rpm-build 3ee90c
    new_stonith->cmds->monitor    = stonith_api_monitor;
rpm-build 3ee90c
    new_stonith->cmds->status     = stonith_api_status;
rpm-build 3ee90c
    new_stonith->cmds->fence      = stonith_api_fence;
rpm-build 3ee90c
    new_stonith->cmds->confirm    = stonith_api_confirm;
rpm-build 3ee90c
    new_stonith->cmds->history    = stonith_api_history;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->list_agents  = stonith_api_device_list;
rpm-build 3ee90c
    new_stonith->cmds->metadata     = stonith_api_device_metadata;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->query           = stonith_api_query;
rpm-build 3ee90c
    new_stonith->cmds->remove_device   = stonith_api_remove_device;
rpm-build 3ee90c
    new_stonith->cmds->register_device = stonith_api_register_device;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->remove_level          = stonith_api_remove_level;
rpm-build 3ee90c
    new_stonith->cmds->remove_level_full     = stonith_api_remove_level_full;
rpm-build 3ee90c
    new_stonith->cmds->register_level        = stonith_api_register_level;
rpm-build 3ee90c
    new_stonith->cmds->register_level_full   = stonith_api_register_level_full;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->remove_callback       = stonith_api_del_callback;
rpm-build 3ee90c
    new_stonith->cmds->register_callback     = stonith_api_add_callback;
rpm-build 3ee90c
    new_stonith->cmds->remove_notification   = stonith_api_del_notification;
rpm-build 3ee90c
    new_stonith->cmds->register_notification = stonith_api_add_notification;
rpm-build 3ee90c
rpm-build 3ee90c
    new_stonith->cmds->validate              = stonith_api_validate;
rpm-build 3ee90c
/* *INDENT-ON* */
rpm-build 3ee90c
rpm-build 3ee90c
    return new_stonith;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \brief Make a blocking connection attempt to the fencer
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in,out] st            Fencer API object
rpm-build 3ee90c
 * \param[in]     name          Client name to use with fencer
rpm-build 3ee90c
 * \param[in]     max_attempts  Return error if this many attempts fail
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return pcmk_ok on success, result of last attempt otherwise
rpm-build 3ee90c
 */
rpm-build 3ee90c
int
rpm-build 3ee90c
stonith_api_connect_retry(stonith_t *st, const char *name, int max_attempts)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = -EINVAL; // if max_attempts is not positive
rpm-build 3ee90c
rpm-build 3ee90c
    for (int attempt = 1; attempt <= max_attempts; attempt++) {
rpm-build 3ee90c
        rc = st->cmds->connect(st, name, NULL);
rpm-build 3ee90c
        if (rc == pcmk_ok) {
rpm-build 3ee90c
            return pcmk_ok;
rpm-build 3ee90c
        } else if (attempt < max_attempts) {
rpm-build 3ee90c
            crm_notice("Fencer connection attempt %d of %d failed (retrying in 2s): %s "
rpm-build 3ee90c
                       CRM_XS " rc=%d",
rpm-build 3ee90c
                       attempt, max_attempts, pcmk_strerror(rc), rc);
rpm-build 3ee90c
            sleep(2);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
    crm_notice("Could not connect to fencer: %s " CRM_XS " rc=%d",
rpm-build 3ee90c
               pcmk_strerror(rc), rc);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
stonith_key_value_t *
rpm-build 3ee90c
stonith_key_value_add(stonith_key_value_t * head, const char *key, const char *value)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_key_value_t *p, *end;
rpm-build 3ee90c
rpm-build 3ee90c
    p = calloc(1, sizeof(stonith_key_value_t));
rpm-build 3ee90c
    if (key) {
rpm-build 3ee90c
        p->key = strdup(key);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (value) {
rpm-build 3ee90c
        p->value = strdup(value);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    end = head;
rpm-build 3ee90c
    while (end && end->next) {
rpm-build 3ee90c
        end = end->next;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (end) {
rpm-build 3ee90c
        end->next = p;
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        head = p;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return head;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
void
rpm-build 3ee90c
stonith_key_value_freeall(stonith_key_value_t * head, int keys, int values)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_key_value_t *p;
rpm-build 3ee90c
rpm-build 3ee90c
    while (head) {
rpm-build 3ee90c
        p = head->next;
rpm-build 3ee90c
        if (keys) {
rpm-build 3ee90c
            free(head->key);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        if (values) {
rpm-build 3ee90c
            free(head->value);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        free(head);
rpm-build 3ee90c
        head = p;
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
#define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON)
rpm-build 3ee90c
#define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args)
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
stonith_api_kick(uint32_t nodeid, const char *uname, int timeout, bool off)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    stonith_t *st = stonith_api_new();
rpm-build 3ee90c
    const char *action = off? "off" : "reboot";
rpm-build 3ee90c
rpm-build 3ee90c
    api_log_open();
rpm-build 3ee90c
    if (st == NULL) {
rpm-build 3ee90c
        api_log(LOG_ERR, "API initialization failed, could not kick (%s) node %u/%s",
rpm-build 3ee90c
                action, nodeid, uname);
rpm-build 3ee90c
        return -EPROTO;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    rc = st->cmds->connect(st, "stonith-api", NULL);
rpm-build 3ee90c
    if (rc != pcmk_ok) {
rpm-build 3ee90c
        api_log(LOG_ERR, "Connection failed, could not kick (%s) node %u/%s : %s (%d)",
rpm-build 3ee90c
                action, nodeid, uname, pcmk_strerror(rc), rc);
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        char *name = NULL;
rpm-build 3ee90c
        enum stonith_call_options opts = st_opt_sync_call | st_opt_allow_suicide;
rpm-build 3ee90c
rpm-build 3ee90c
        if (uname != NULL) {
rpm-build 3ee90c
            name = strdup(uname);
rpm-build 3ee90c
        } else if (nodeid > 0) {
rpm-build 3ee90c
            opts |= st_opt_cs_nodeid;
rpm-build 3ee90c
            name = crm_itoa(nodeid);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        rc = st->cmds->fence(st, opts, name, action, timeout, 0);
rpm-build 3ee90c
        free(name);
rpm-build 3ee90c
rpm-build 3ee90c
        if (rc != pcmk_ok) {
rpm-build 3ee90c
            api_log(LOG_ERR, "Could not kick (%s) node %u/%s : %s (%d)",
rpm-build 3ee90c
                    action, nodeid, uname, pcmk_strerror(rc), rc);
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            api_log(LOG_NOTICE, "Node %u/%s kicked: %s", nodeid, uname, action);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    stonith_api_delete(st);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
time_t
rpm-build 3ee90c
stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    time_t when = 0;
rpm-build 3ee90c
    stonith_t *st = stonith_api_new();
rpm-build 3ee90c
    stonith_history_t *history = NULL, *hp = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (st == NULL) {
rpm-build 3ee90c
        api_log(LOG_ERR, "Could not retrieve fence history for %u/%s: "
rpm-build 3ee90c
                "API initialization failed", nodeid, uname);
rpm-build 3ee90c
        return when;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    rc = st->cmds->connect(st, "stonith-api", NULL);
rpm-build 3ee90c
    if (rc != pcmk_ok) {
rpm-build 3ee90c
        api_log(LOG_NOTICE, "Connection failed: %s (%d)", pcmk_strerror(rc), rc);
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        int entries = 0;
rpm-build 3ee90c
        int progress = 0;
rpm-build 3ee90c
        int completed = 0;
rpm-build 3ee90c
        char *name = NULL;
rpm-build 3ee90c
        enum stonith_call_options opts = st_opt_sync_call;
rpm-build 3ee90c
rpm-build 3ee90c
        if (uname != NULL) {
rpm-build 3ee90c
            name = strdup(uname);
rpm-build 3ee90c
        } else if (nodeid > 0) {
rpm-build 3ee90c
            opts |= st_opt_cs_nodeid;
rpm-build 3ee90c
            name = crm_itoa(nodeid);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        rc = st->cmds->history(st, opts, name, &history, 120);
rpm-build 3ee90c
        free(name);
rpm-build 3ee90c
rpm-build 3ee90c
        for (hp = history; hp; hp = hp->next) {
rpm-build 3ee90c
            entries++;
rpm-build 3ee90c
            if (in_progress) {
rpm-build 3ee90c
                progress++;
rpm-build 3ee90c
                if (hp->state != st_done && hp->state != st_failed) {
rpm-build 3ee90c
                    when = time(NULL);
rpm-build 3ee90c
                }
rpm-build 3ee90c
rpm-build 3ee90c
            } else if (hp->state == st_done) {
rpm-build 3ee90c
                completed++;
rpm-build 3ee90c
                if (hp->completed > when) {
rpm-build 3ee90c
                    when = hp->completed;
rpm-build 3ee90c
                }
rpm-build 3ee90c
            }
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        stonith_history_free(history);
rpm-build 3ee90c
rpm-build 3ee90c
        if(rc == pcmk_ok) {
rpm-build 3ee90c
            api_log(LOG_INFO, "Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            api_log(LOG_ERR, "Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname, pcmk_strerror(rc), rc);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    stonith_api_delete(st);
rpm-build 3ee90c
rpm-build 3ee90c
    if(when) {
rpm-build 3ee90c
        api_log(LOG_INFO, "Node %u/%s last kicked at: %ld", nodeid, uname, (long int)when);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return when;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
bool
rpm-build 3ee90c
stonith_agent_exists(const char *agent, int timeout)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_t *st = NULL;
rpm-build 3ee90c
    stonith_key_value_t *devices = NULL;
rpm-build 3ee90c
    stonith_key_value_t *dIter = NULL;
rpm-build 3ee90c
    bool rc = FALSE;
rpm-build 3ee90c
rpm-build 3ee90c
    if (agent == NULL) {
rpm-build 3ee90c
        return rc;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    st = stonith_api_new();
rpm-build 3ee90c
    if (st == NULL) {
rpm-build 3ee90c
        crm_err("Could not list fence agents: API memory allocation failed");
rpm-build 3ee90c
        return FALSE;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    st->cmds->list_agents(st, st_opt_sync_call, NULL, &devices, timeout == 0 ? 120 : timeout);
rpm-build 3ee90c
rpm-build 3ee90c
    for (dIter = devices; dIter != NULL; dIter = dIter->next) {
rpm-build 3ee90c
        if (crm_str_eq(dIter->value, agent, TRUE)) {
rpm-build 3ee90c
            rc = TRUE;
rpm-build 3ee90c
            break;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    stonith_key_value_freeall(devices, 1, 1);
rpm-build 3ee90c
    stonith_api_delete(st);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
const char *
rpm-build 3ee90c
stonith_action_str(const char *action)
rpm-build 3ee90c
{
rpm-build 3ee90c
    if (action == NULL) {
rpm-build 3ee90c
        return "fencing";
rpm-build 3ee90c
    } else if (!strcmp(action, "on")) {
rpm-build 3ee90c
        return "unfencing";
rpm-build 3ee90c
    } else if (!strcmp(action, "off")) {
rpm-build 3ee90c
        return "turning off";
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        return action;
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Parse a target name from one line of a target list string
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in]     line    One line of a target list string
rpm-build 3ee90c
 * \parma[in]     len     String length of line
rpm-build 3ee90c
 * \param[in,out] output  List to add newly allocated target name to
rpm-build 3ee90c
 */
rpm-build 3ee90c
static void
rpm-build 3ee90c
parse_list_line(const char *line, int len, GList **output)
rpm-build 3ee90c
{
rpm-build 3ee90c
    size_t i = 0;
rpm-build 3ee90c
    size_t entry_start = 0;
rpm-build 3ee90c
rpm-build 3ee90c
    /* Skip complaints about additional parameters device doesn't understand
rpm-build 3ee90c
     *
rpm-build 3ee90c
     * @TODO Document or eliminate the implied restriction of target names
rpm-build 3ee90c
     */
rpm-build 3ee90c
    if (strstr(line, "invalid") || strstr(line, "variable")) {
rpm-build 3ee90c
        crm_debug("Skipping list output line: %s", line);
rpm-build 3ee90c
        return;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    // Process line content, character by character
rpm-build 3ee90c
    for (i = 0; i <= len; i++) {
rpm-build 3ee90c
rpm-build 3ee90c
        if (isspace(line[i]) || (line[i] == ',') || (line[i] == ';')
rpm-build 3ee90c
            || (line[i] == '\0')) {
rpm-build 3ee90c
            // We've found a separator (i.e. the end of an entry)
rpm-build 3ee90c
rpm-build 3ee90c
            int rc = 0;
rpm-build 3ee90c
            char *entry = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
            if (i == entry_start) {
rpm-build 3ee90c
                // Skip leading and sequential separators
rpm-build 3ee90c
                entry_start = i + 1;
rpm-build 3ee90c
                continue;
rpm-build 3ee90c
            }
rpm-build 3ee90c
rpm-build 3ee90c
            entry = calloc(i - entry_start + 1, sizeof(char));
rpm-build 3ee90c
            CRM_ASSERT(entry != NULL);
rpm-build 3ee90c
rpm-build 3ee90c
            /* Read entry, stopping at first separator
rpm-build 3ee90c
             *
rpm-build 3ee90c
             * @TODO Document or eliminate these character restrictions
rpm-build 3ee90c
             */
rpm-build 3ee90c
            rc = sscanf(line + entry_start, "%[a-zA-Z0-9_-.]", entry);
rpm-build 3ee90c
            if (rc != 1) {
rpm-build 3ee90c
                crm_warn("Could not parse list output entry: %s "
rpm-build 3ee90c
                         CRM_XS " entry_start=%d position=%d",
rpm-build 3ee90c
                         line + entry_start, entry_start, i);
rpm-build 3ee90c
                free(entry);
rpm-build 3ee90c
rpm-build 3ee90c
            } else if (safe_str_eq(entry, "on") || safe_str_eq(entry, "off")) {
rpm-build 3ee90c
                /* Some agents print the target status in the list output,
rpm-build 3ee90c
                 * though none are known now (the separate list-status command
rpm-build 3ee90c
                 * is used for this, but it can also print "UNKNOWN"). To handle
rpm-build 3ee90c
                 * this possibility, skip such entries.
rpm-build 3ee90c
                 *
rpm-build 3ee90c
                 * @TODO Document or eliminate the implied restriction of target
rpm-build 3ee90c
                 * names.
rpm-build 3ee90c
                 */
rpm-build 3ee90c
                free(entry);
rpm-build 3ee90c
rpm-build 3ee90c
            } else {
rpm-build 3ee90c
                // We have a valid entry
rpm-build 3ee90c
                *output = g_list_append(*output, entry);
rpm-build 3ee90c
            }
rpm-build 3ee90c
            entry_start = i + 1;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Parse a list of targets from a string
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] list_output  Target list as a string
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \return List of target names
rpm-build 3ee90c
 * \note The target list string format is flexible, to allow for user-specified
rpm-build 3ee90c
 *       lists such pcmk_host_list and the output of an agent's list action
rpm-build 3ee90c
 *       (whether direct or via the API, which escapes newlines). There may be
rpm-build 3ee90c
 *       multiple lines, separated by either a newline or an escaped newline
rpm-build 3ee90c
 *       (backslash n). Each line may have one or more target names, separated
rpm-build 3ee90c
 *       by any combination of whitespace, commas, and semi-colons. Lines
rpm-build 3ee90c
 *       containing "invalid" or "variable" will be ignored entirely. Target
rpm-build 3ee90c
 *       names "on" or "off" (case-insensitive) will be ignored. Target names
rpm-build 3ee90c
 *       may contain only alphanumeric characters, underbars (_), dashes (-),
rpm-build 3ee90c
 *       and dots (.) (if any other character occurs in the name, it and all
rpm-build 3ee90c
 *       subsequent characters in the name will be ignored).
rpm-build 3ee90c
 * \note The caller is responsible for freeing the result with
rpm-build 3ee90c
 *       g_list_free_full(result, free).
rpm-build 3ee90c
 */
rpm-build 3ee90c
GList *
rpm-build 3ee90c
stonith__parse_targets(const char *target_spec)
rpm-build 3ee90c
{
rpm-build 3ee90c
    GList *targets = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (target_spec != NULL) {
rpm-build 3ee90c
        size_t out_len = strlen(target_spec);
rpm-build 3ee90c
        size_t line_start = 0; // Starting index of line being processed
rpm-build 3ee90c
rpm-build 3ee90c
        for (size_t i = 0; i <= out_len; ++i) {
rpm-build 3ee90c
            if ((target_spec[i] == '\n') || (target_spec[i] == '\0')
rpm-build 3ee90c
                || ((target_spec[i] == '\\') && (target_spec[i + 1] == 'n'))) {
rpm-build 3ee90c
                // We've reached the end of one line of output
rpm-build 3ee90c
rpm-build 3ee90c
                int len = i - line_start;
rpm-build 3ee90c
rpm-build 3ee90c
                if (len > 0) {
rpm-build 3ee90c
                    char *line = strndup(target_spec + line_start, len);
rpm-build 3ee90c
rpm-build 3ee90c
                    line[len] = '\0'; // Because it might be a newline
rpm-build 3ee90c
                    parse_list_line(line, len, &targets;;
rpm-build 3ee90c
                    free(line);
rpm-build 3ee90c
                }
rpm-build 3ee90c
                if (target_spec[i] == '\\') {
rpm-build 3ee90c
                    ++i; // backslash-n takes up two positions
rpm-build 3ee90c
                }
rpm-build 3ee90c
                line_start = i + 1;
rpm-build 3ee90c
            }
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return targets;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Determine if a later stonith event succeeded.
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \note Before calling this function, use stonith__sort_history() to sort the
rpm-build 3ee90c
 *       top_history argument.
rpm-build 3ee90c
 */
rpm-build 3ee90c
gboolean
rpm-build 3ee90c
stonith__later_succeeded(stonith_history_t *event, stonith_history_t *top_history)
rpm-build 3ee90c
{
rpm-build 3ee90c
     gboolean ret = FALSE;
rpm-build 3ee90c
rpm-build 3ee90c
     for (stonith_history_t *prev_hp = top_history; prev_hp; prev_hp = prev_hp->next) {
rpm-build 3ee90c
        if (prev_hp == event) {
rpm-build 3ee90c
            break;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
         if ((prev_hp->state == st_done) &&
rpm-build 3ee90c
            safe_str_eq(event->target, prev_hp->target) &&
rpm-build 3ee90c
            safe_str_eq(event->action, prev_hp->action) &&
rpm-build 3ee90c
            safe_str_eq(event->delegate, prev_hp->delegate) &&
rpm-build 3ee90c
            (event->completed < prev_hp->completed)) {
rpm-build 3ee90c
            ret = TRUE;
rpm-build 3ee90c
            break;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return ret;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
/*!
rpm-build 3ee90c
 * \internal
rpm-build 3ee90c
 * \brief Sort the stonith-history
rpm-build 3ee90c
 *        sort by competed most current on the top
rpm-build 3ee90c
 *        pending actions lacking a completed-stamp are gathered at the top
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * \param[in] history    List of stonith actions
rpm-build 3ee90c
 *
rpm-build 3ee90c
 */
rpm-build 3ee90c
stonith_history_t *
rpm-build 3ee90c
stonith__sort_history(stonith_history_t *history)
rpm-build 3ee90c
{
rpm-build 3ee90c
    stonith_history_t *new = NULL, *pending = NULL, *hp, *np, *tmp;
rpm-build 3ee90c
rpm-build 3ee90c
    for (hp = history; hp; ) {
rpm-build 3ee90c
        tmp = hp->next;
rpm-build 3ee90c
        if ((hp->state == st_done) || (hp->state == st_failed)) {
rpm-build 3ee90c
            /* sort into new */
rpm-build 3ee90c
            if ((!new) || (hp->completed > new->completed)) {
rpm-build 3ee90c
                hp->next = new;
rpm-build 3ee90c
                new = hp;
rpm-build 3ee90c
            } else {
rpm-build 3ee90c
                np = new;
rpm-build 3ee90c
                do {
rpm-build 3ee90c
                    if ((!np->next) || (hp->completed > np->next->completed)) {
rpm-build 3ee90c
                        hp->next = np->next;
rpm-build 3ee90c
                        np->next = hp;
rpm-build 3ee90c
                        break;
rpm-build 3ee90c
                    }
rpm-build 3ee90c
                    np = np->next;
rpm-build 3ee90c
                } while (1);
rpm-build 3ee90c
            }
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            /* put into pending */
rpm-build 3ee90c
            hp->next = pending;
rpm-build 3ee90c
            pending = hp;
rpm-build 3ee90c
        }
rpm-build 3ee90c
        hp = tmp;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    /* pending actions don't have a completed-stamp so make them go front */
rpm-build 3ee90c
    if (pending) {
rpm-build 3ee90c
        stonith_history_t *last_pending = pending;
rpm-build 3ee90c
rpm-build 3ee90c
        while (last_pending->next) {
rpm-build 3ee90c
            last_pending = last_pending->next;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        last_pending->next = new;
rpm-build 3ee90c
        new = pending;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return new;
rpm-build 3ee90c
}