19 #include <sys/types.h> 27 static GHashTable *cib_op_callback_table = NULL;
30 cib_client_set_op_callback(
cib_t *cib,
31 void (*callback) (
const xmlNode * msg,
int call_id,
32 int rc, xmlNode * output))
34 if (callback == NULL) {
35 crm_info(
"Un-Setting operation callback");
45 ciblib_GCompareFunc(gconstpointer a, gconstpointer b)
57 crm_trace(
"callbacks for %s are not equal: %p < %p",
61 crm_trace(
"callbacks for %s are not equal: %p > %p",
69 cib_client_add_notify_callback(
cib_t * cib,
const char *event,
70 void (*callback) (
const char *event,
73 GList *list_item = NULL;
77 return -EPROTONOSUPPORT;
80 crm_trace(
"Adding callback for %s events (%d)",
84 new_client->
event = event;
87 list_item = g_list_find_custom(cib->
notify_list, new_client,
90 if (list_item != NULL) {
91 crm_warn(
"Callback already present");
106 get_notify_list_event_count(
cib_t *cib,
const char *event)
110 for (GList *iter = g_list_first(cib->
notify_list); iter != NULL;
114 if (strcmp(client->
event, event) == 0) {
118 crm_trace(
"event(%s) count : %d", event, count);
123 cib_client_del_notify_callback(
cib_t *cib,
const char *event,
124 void (*callback) (
const char *event,
127 GList *list_item = NULL;
131 return -EPROTONOSUPPORT;
134 if (get_notify_list_event_count(cib, event) == 0) {
135 crm_debug(
"The callback of the event does not exist(%s)", event);
139 crm_debug(
"Removing callback for %s events", event);
142 new_client->
event = event;
145 list_item = g_list_find_custom(cib->
notify_list, new_client, ciblib_GCompareFunc);
147 if (list_item != NULL) {
159 if (get_notify_list_event_count(cib, event) == 0) {
169 cib_async_timeout_handler(gpointer
data)
173 crm_debug(
"Async call %d timed out after %ds",
178 return G_SOURCE_CONTINUE;
183 gboolean only_success,
void *user_data,
184 const char *callback_name,
185 void (*callback)(xmlNode *,
int,
int,
187 void (*free_func)(
void *))
192 if (only_success == FALSE) {
197 if (user_data && free_func) {
198 free_func(user_data);
204 blob->
id = callback_name;
214 blob->
timer = async_timer;
219 async_timer->
ref = g_timeout_add(async_timer->
timeout,
220 cib_async_timeout_handler,
225 pcmk__intkey_table_insert(cib_op_callback_table,
call_id, blob);
232 gboolean only_success,
void *user_data,
233 const char *callback_name,
234 void (*callback) (xmlNode *,
int,
int, xmlNode *,
238 only_success, user_data,
239 callback_name, callback, NULL);
243 cib_client_noop(
cib_t *
cib,
int call_options)
250 cib_client_ping(
cib_t *
cib, xmlNode ** output_data,
int call_options)
257 cib_client_query(
cib_t *
cib,
const char *section, xmlNode ** output_data,
int call_options)
263 cib_client_query_from(
cib_t *
cib,
const char *
host,
const char *section,
264 xmlNode ** output_data,
int call_options)
267 output_data, call_options,
cib->
user);
278 set_secondary(
cib_t *
cib,
int call_options)
281 NULL, call_options,
cib->
user);
285 set_all_secondary(
cib_t *
cib,
int call_options)
287 return -EPROTONOSUPPORT;
291 set_primary(
cib_t *
cib,
int call_options)
293 crm_trace(
"Adding cib_scope_local to options");
299 cib_client_bump_epoch(
cib_t *
cib,
int call_options)
306 cib_client_upgrade(
cib_t *
cib,
int call_options)
309 NULL, call_options,
cib->
user);
313 cib_client_sync(
cib_t *
cib,
const char *section,
int call_options)
319 cib_client_sync_from(
cib_t *
cib,
const char *
host,
const char *section,
int call_options)
322 NULL, NULL, call_options,
cib->
user);
326 cib_client_create(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
329 NULL, call_options,
cib->
user);
333 cib_client_modify(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
336 NULL, call_options,
cib->
user);
340 cib_client_replace(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
343 NULL, call_options,
cib->
user);
347 cib_client_delete(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
350 NULL, call_options,
cib->
user);
354 cib_client_delete_absolute(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
361 cib_client_erase(
cib_t *
cib, xmlNode ** output_data,
int call_options)
364 output_data, call_options,
cib->
user);
368 cib_client_init_transaction(
cib_t *
cib)
386 const char *client_id = NULL;
389 crm_err(
"Failed to initialize CIB transaction for client %s: %s",
396 cib_client_end_transaction(
cib_t *
cib,
bool commit,
int call_options)
398 const char *client_id = NULL;
406 client_id = pcmk__s(client_id,
"(unidentified)");
412 crm_err(
"Failed to commit transaction for CIB client %s: %s",
422 crm_trace(
"Discarded transaction for CIB client %s", client_id);
424 crm_trace(
"No transaction found for CIB client %s", client_id);
433 cib_client_fetch_schemas(
cib_t *
cib, xmlNode **output_data,
const char *after_ver,
442 output_data, call_options, NULL);
448 cib_client_set_user(
cib_t *
cib,
const char *user)
454 cib_destroy_op_callback(gpointer
data)
471 destroy_op_callback_table(
void)
473 if (cib_op_callback_table != NULL) {
474 g_hash_table_destroy(cib_op_callback_table);
475 cib_op_callback_table = NULL;
482 char *cib_home = NULL;
483 char *fullname = NULL;
485 const char *dir = getenv(
"CIB_shadow_dir");
488 uid_t uid = geteuid();
489 struct passwd *pwent = getpwuid(uid);
490 const char *user = NULL;
493 user = pwent->pw_name;
495 user = getenv(
"USER");
497 "Assuming %s because cannot get user details for user ID %d",
498 (user? user :
"unprivileged user"), uid);
505 const char *home = NULL;
507 if ((home = getenv(
"HOME")) == NULL) {
509 home = pwent->pw_dir;
514 if (home && home[0] ==
'/') {
519 rc = mkdir(cib_home, 0700);
520 if (rc < 0 && errno != EEXIST) {
521 crm_perror(LOG_ERR,
"Couldn't create user-specific shadow directory: %s",
542 cib_t *new_cib = NULL;
543 char *shadow_file = NULL;
569 const char *shadow = getenv(
"CIB_shadow");
572 unsetenv(
"CIB_shadow");
575 if (shadow != NULL) {
576 setenv(
"CIB_shadow", shadow, 1);
600 const char *value = getenv(
"CIB_shadow");
603 if (!pcmk__str_empty(value)) {
607 value = getenv(
"CIB_file");
608 if (!pcmk__str_empty(value)) {
612 value = getenv(
"CIB_port");
613 if (!pcmk__str_empty(value)) {
614 gboolean encrypted = TRUE;
615 const char *server = getenv(
"CIB_server");
616 const char *user = getenv(
"CIB_user");
617 const char *pass = getenv(
"CIB_passwd");
630 if (pcmk__str_empty(user)) {
634 if (pcmk__str_empty(server)) {
635 server =
"localhost";
638 crm_debug(
"Initializing %s remote CIB access to %s:%d as user %s",
639 (encrypted?
"encrypted" :
"plain-text"), server, port, user);
658 cib_t *new_cib = NULL;
660 new_cib = calloc(1,
sizeof(
cib_t));
662 if (new_cib == NULL) {
681 if (new_cib->
cmds == NULL) {
694 new_cib->
cmds->
noop = cib_client_noop;
695 new_cib->
cmds->
ping = cib_client_ping;
697 new_cib->
cmds->
sync = cib_client_sync;
742 while (list != NULL) {
745 list = g_list_remove(list, client);
762 destroy_op_callback_table();
783 destroy_op_callback_table();
784 cib_op_callback_table = pcmk__intkey_table(cib_destroy_op_callback);
786 pcmk__intkey_table_remove(cib_op_callback_table, call_id);
793 if (cib_op_callback_table == NULL) {
796 return g_hash_table_size(cib_op_callback_table);
800 cib_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
802 int call = GPOINTER_TO_INT(key);
805 crm_debug(
"Call %d (%s): pending", call, pcmk__s(blob->
id,
"without ID"));
811 if (cib_op_callback_table == NULL) {
814 return g_hash_table_foreach(cib_op_callback_table, cib_dump_pending_op, NULL);
820 return pcmk__intkey_table_lookup(cib_op_callback_table, call_id);
#define CRM_CHECK(expr, failure_action)
const char * pcmk_strerror(int rc)
gboolean(* register_callback)(cib_t *cib, int call_id, int timeout, gboolean only_success, void *user_data, const char *callback_name, void(*callback)(xmlNode *, int, int, xmlNode *, void *))
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
int pcmk_rc2legacy(int rc)
#define PCMK__CIB_REQUEST_PRIMARY
void cib_dump_pending_callbacks(void)
#define PCMK__CIB_REQUEST_CREATE
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
int(* delete_absolute)(cib_t *cib, const char *section, xmlNode *data, int call_options)
gboolean(* register_callback_full)(cib_t *cib, int call_id, int timeout, gboolean only_success, void *user_data, const char *callback_name, void(*callback)(xmlNode *, int, int, xmlNode *, void *), void(*free_func)(void *))
void(* callback)(const char *event, xmlNode *msg)
#define PCMK__CIB_REQUEST_QUERY
#define PCMK__CIB_REQUEST_COMMIT_TRANSACT
void cib_free_callbacks(cib_t *cib)
Free all callbacks for a CIB connection.
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
int(* fetch_schemas)(cib_t *cib, xmlNode **output_data, const char *after_ver, int call_options)
cib_t * cib_new(void)
Create a new CIB connection object.
cib_t * cib_shadow_new(const char *shadow)
int(* set_secondary)(cib_t *cib, int call_options)
Set the local CIB manager as a secondary instance.
#define PCMK__CIB_REQUEST_ERASE
int(* set_op_callback)(cib_t *cib, void(*callback)(const xmlNode *msg, int callid, int rc, xmlNode *output))
int(* sync)(cib_t *cib, const char *section, int call_options)
#define PCMK__CIB_REQUEST_ABS_DELETE
#define crm_warn(fmt, args...)
struct timer_rec_s * timer
int(* query_from)(cib_t *cib, const char *host, const char *section, xmlNode **output_data, int call_options)
int pcmk__scan_port(const char *text, int *port)
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int num_cib_op_callbacks(void)
int(* set_slave)(cib_t *cib, int call_options)
#define PCMK__CIB_REQUEST_IS_PRIMARY
void(* callback)(xmlNode *, int, int, xmlNode *, void *)
#define crm_trace(fmt, args...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
int(* modify)(cib_t *cib, const char *section, xmlNode *data, int call_options)
void(* op_callback)(const xmlNode *msg, int call_id, int rc, xmlNode *output)
char * get_shadow_file(const char *suffix)
int(* create)(cib_t *cib, const char *section, xmlNode *data, int call_options)
void pcmk__str_update(char **str, const char *value)
Wrappers for and extensions to libxml2.
int(* query)(cib_t *cib, const char *section, xmlNode **output_data, int call_options)
int(* is_master)(cib_t *cib)
cib_t * cib_new_no_shadow(void)
Create a new CIB connection object, ignoring any active shadow CIB.
cib_t * cib_native_new(void)
int(* replace)(cib_t *cib, const char *section, xmlNode *data, int call_options)
void free_xml(xmlNode *child)
int(* add_notify_callback)(cib_t *cib, const char *event, void(*callback)(const char *event, xmlNode *msg))
int(* sync_from)(cib_t *cib, const char *host, const char *section, int call_options)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
int(* ping)(cib_t *cib, xmlNode **output_data, int call_options)
int(* update)(cib_t *cib, const char *section, xmlNode *data, int call_options)
#define PCMK__CIB_REQUEST_UPGRADE
cib_t * cib_file_new(const char *filename)
cib_callback_client_t * cib__lookup_id(int call_id)
#define PCMK__CIB_REQUEST_BUMP
int(* set_primary)(cib_t *cib, int call_options)
Set the local CIB manager as the cluster's primary instance.
int(* noop)(cib_t *cib, int call_options)
const char * pcmk__get_tmpdir(void)
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
int(* set_slave_all)(cib_t *cib, int call_options)
int(* del_notify_callback)(cib_t *cib, const char *event, void(*callback)(const char *event, xmlNode *msg))
#define PCMK__CIB_REQUEST_SCHEMAS
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
void cib_free_notify(cib_t *cib)
#define crm_err(fmt, args...)
#define PCMK__CIB_REQUEST_REPLACE
int(* set_master)(cib_t *cib, int call_options)
#define PCMK__CIB_REQUEST_SYNC_TO_ALL
#define PCMK__CIB_REQUEST_SECONDARY
#define PCMK__CIB_REQUEST_NOOP
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
void(* set_user)(cib_t *cib, const char *user)
Set the user as whom all CIB requests via methods will be executed.
cib_t * cib_new_variant(void)
int(* bump_epoch)(cib_t *cib, int call_options)
gboolean crm_is_true(const char *s)
#define PCMK__CIB_REQUEST_MODIFY
int(* remove)(cib_t *cib, const char *section, xmlNode *data, int call_options)
int cib_internal_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *user_name)
int(* init_transaction)(cib_t *cib)
Initiate an atomic CIB transaction for this client.
void cib_delete(cib_t *cib)
Free all memory used by CIB connection.
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
#define pcmk__assert_alloc(nmemb, size)
#define PCMK__XE_CIB_TRANSACTION
void(* free_func)(void *)
int(* client_id)(const cib_t *cib, const char **async_id, const char **sync_id)
Get the given CIB connection's unique client identifier(s)
int(* upgrade)(cib_t *cib, int call_options)
#define crm_info(fmt, args...)
int(* erase)(cib_t *cib, xmlNode **output_data, int call_options)
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
#define PCMK__CIB_REQUEST_DELETE