19 #include <sys/types.h> 28 static GHashTable *cib_op_callback_table = NULL;
30 #define op_common(cib) do { \ 33 } else if(cib->delegate_fn == NULL) { \ 34 return -EPROTONOSUPPORT; \ 39 cib_client_set_op_callback(
cib_t *cib,
40 void (*callback) (
const xmlNode * msg,
int call_id,
41 int rc, xmlNode * output))
43 if (callback == NULL) {
44 crm_info(
"Un-Setting operation callback");
54 ciblib_GCompareFunc(gconstpointer a, gconstpointer b)
66 crm_trace(
"callbacks for %s are not equal: %p < %p",
70 crm_trace(
"callbacks for %s are not equal: %p > %p",
78 cib_client_add_notify_callback(
cib_t * cib,
const char *event,
79 void (*callback) (
const char *event,
82 GList *list_item = NULL;
86 return -EPROTONOSUPPORT;
89 crm_trace(
"Adding callback for %s events (%d)",
93 new_client->
event = event;
96 list_item = g_list_find_custom(cib->
notify_list, new_client,
99 if (list_item != NULL) {
100 crm_warn(
"Callback already present");
115 get_notify_list_event_count(
cib_t *cib,
const char *event)
119 for (GList *iter = g_list_first(cib->
notify_list); iter != NULL;
123 if (strcmp(client->
event, event) == 0) {
127 crm_trace(
"event(%s) count : %d", event, count);
132 cib_client_del_notify_callback(
cib_t *cib,
const char *event,
133 void (*callback) (
const char *event,
136 GList *list_item = NULL;
140 return -EPROTONOSUPPORT;
143 if (get_notify_list_event_count(cib, event) == 0) {
144 crm_debug(
"The callback of the event does not exist(%s)", event);
148 crm_debug(
"Removing callback for %s events", event);
151 new_client->
event = event;
154 list_item = g_list_find_custom(cib->
notify_list, new_client, ciblib_GCompareFunc);
156 if (list_item != NULL) {
168 if (get_notify_list_event_count(cib, event) == 0) {
178 cib_async_timeout_handler(gpointer
data)
182 crm_debug(
"Async call %d timed out after %ds",
187 return G_SOURCE_CONTINUE;
192 gboolean only_success,
void *user_data,
193 const char *callback_name,
194 void (*callback)(xmlNode *,
int,
int,
196 void (*free_func)(
void *))
201 if (only_success == FALSE) {
206 if (user_data && free_func) {
207 free_func(user_data);
213 blob->
id = callback_name;
222 async_timer = calloc(1,
sizeof(
struct timer_rec_s));
223 blob->
timer = async_timer;
228 async_timer->
ref = g_timeout_add(async_timer->
timeout,
229 cib_async_timeout_handler,
234 pcmk__intkey_table_insert(cib_op_callback_table,
call_id, blob);
241 gboolean only_success,
void *user_data,
242 const char *callback_name,
243 void (*callback) (xmlNode *,
int,
int, xmlNode *,
247 only_success, user_data,
248 callback_name, callback, NULL);
252 cib_client_noop(
cib_t *
cib,
int call_options)
260 cib_client_ping(
cib_t *
cib, xmlNode ** output_data,
int call_options)
268 cib_client_query(
cib_t *
cib,
const char *section, xmlNode ** output_data,
int call_options)
274 cib_client_query_from(
cib_t *
cib,
const char *
host,
const char *section,
275 xmlNode ** output_data,
int call_options)
279 output_data, call_options,
cib->
user);
291 set_secondary(
cib_t *
cib,
int call_options)
295 NULL, call_options,
cib->
user);
299 set_all_secondary(
cib_t *
cib,
int call_options)
301 return -EPROTONOSUPPORT;
305 set_primary(
cib_t *
cib,
int call_options)
308 crm_trace(
"Adding cib_scope_local to options");
314 cib_client_bump_epoch(
cib_t *
cib,
int call_options)
322 cib_client_upgrade(
cib_t *
cib,
int call_options)
326 NULL, call_options,
cib->
user);
330 cib_client_sync(
cib_t *
cib,
const char *section,
int call_options)
336 cib_client_sync_from(
cib_t *
cib,
const char *
host,
const char *section,
int call_options)
340 NULL, NULL, call_options,
cib->
user);
344 cib_client_create(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
348 NULL, call_options,
cib->
user);
352 cib_client_modify(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
356 NULL, call_options,
cib->
user);
360 cib_client_replace(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
364 NULL, call_options,
cib->
user);
368 cib_client_delete(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
372 NULL, call_options,
cib->
user);
376 cib_client_delete_absolute(
cib_t *
cib,
const char *section, xmlNode *
data,
int call_options)
384 cib_client_erase(
cib_t *
cib, xmlNode ** output_data,
int call_options)
388 output_data, call_options,
cib->
user);
392 cib_client_init_transaction(
cib_t *
cib)
411 const char *client_id = NULL;
414 crm_err(
"Failed to initialize CIB transaction for client %s: %s",
421 cib_client_end_transaction(
cib_t *
cib,
bool commit,
int call_options)
423 const char *client_id = NULL;
428 client_id = pcmk__s(client_id,
"(unidentified)");
434 crm_err(
"Failed to commit transaction for CIB client %s: %s",
444 crm_trace(
"Discarded transaction for CIB client %s", client_id);
446 crm_trace(
"No transaction found for CIB client %s", client_id);
455 cib_client_set_user(
cib_t *
cib,
const char *user)
461 cib_destroy_op_callback(gpointer
data)
478 destroy_op_callback_table(
void)
480 if (cib_op_callback_table != NULL) {
481 g_hash_table_destroy(cib_op_callback_table);
482 cib_op_callback_table = NULL;
489 char *cib_home = NULL;
490 char *fullname = NULL;
492 const char *dir = getenv(
"CIB_shadow_dir");
495 uid_t uid = geteuid();
496 struct passwd *pwent = getpwuid(uid);
497 const char *user = NULL;
500 user = pwent->pw_name;
502 user = getenv(
"USER");
504 "Assuming %s because cannot get user details for user ID %d",
505 (user? user :
"unprivileged user"), uid);
512 const char *home = NULL;
514 if ((home = getenv(
"HOME")) == NULL) {
516 home = pwent->pw_dir;
521 if (home && home[0] ==
'/') {
526 rc = mkdir(cib_home, 0700);
527 if (rc < 0 && errno != EEXIST) {
528 crm_perror(LOG_ERR,
"Couldn't create user-specific shadow directory: %s",
549 cib_t *new_cib = NULL;
550 char *shadow_file = NULL;
576 const char *shadow = getenv(
"CIB_shadow");
579 unsetenv(
"CIB_shadow");
582 if (shadow != NULL) {
583 setenv(
"CIB_shadow", shadow, 1);
607 const char *value = getenv(
"CIB_shadow");
610 if (value && value[0] != 0) {
614 value = getenv(
"CIB_file");
619 value = getenv(
"CIB_port");
621 gboolean encrypted = TRUE;
622 const char *server = getenv(
"CIB_server");
623 const char *user = getenv(
"CIB_user");
624 const char *pass = getenv(
"CIB_passwd");
633 value = getenv(
"CIB_encrypted");
641 crm_info(
"Defaulting to user: %s", user);
644 if (server == NULL) {
645 server =
"localhost";
646 crm_info(
"Defaulting to localhost");
667 cib_t *new_cib = NULL;
669 new_cib = calloc(1,
sizeof(
cib_t));
671 if (new_cib == NULL) {
690 if (new_cib->
cmds == NULL) {
703 new_cib->
cmds->
noop = cib_client_noop;
704 new_cib->
cmds->
ping = cib_client_ping;
706 new_cib->
cmds->
sync = cib_client_sync;
749 while (list != NULL) {
752 list = g_list_remove(list, client);
769 destroy_op_callback_table();
790 destroy_op_callback_table();
791 cib_op_callback_table = pcmk__intkey_table(cib_destroy_op_callback);
793 pcmk__intkey_table_remove(cib_op_callback_table, call_id);
800 if (cib_op_callback_table == NULL) {
803 return g_hash_table_size(cib_op_callback_table);
807 cib_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
809 int call = GPOINTER_TO_INT(key);
812 crm_debug(
"Call %d (%s): pending", call, pcmk__s(blob->
id,
"without ID"));
818 if (cib_op_callback_table == NULL) {
821 return g_hash_table_foreach(cib_op_callback_table, cib_dump_pending_op, NULL);
827 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 * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
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.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
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)
#define T_CIB_TRANSACTION
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 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.
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