22 #include <sys/types.h> 32 #define CIB_SERIES "cib" 33 #define CIB_SERIES_MAX 100 34 #define CIB_SERIES_BZIP FALSE 38 #define CIB_LIVE_NAME CIB_SERIES ".xml" 41 static GHashTable *client_table = NULL;
48 typedef struct cib_file_opaque_s {
55 static int cib_file_process_commit_transaction(
const char *op,
int options,
57 xmlNode *req, xmlNode *
input,
58 xmlNode *existing_cib,
69 register_client(
const cib_t *cib)
73 if (client_table == NULL) {
76 g_hash_table_insert(client_table, private->id, (gpointer) cib);
86 unregister_client(
const cib_t *cib)
90 if (client_table == NULL) {
94 g_hash_table_remove(client_table, private->id);
99 if (g_hash_table_size(client_table) == 0) {
100 g_hash_table_destroy(client_table);
114 get_client(
const char *client_id)
116 if (client_table == NULL) {
119 return g_hash_table_lookup(client_table, (gpointer) client_id);
139 static uid_t cib_file_owner = 0;
140 static uid_t cib_file_group = 0;
141 static gboolean cib_do_chown = FALSE;
143 #define cib_set_file_flags(cibfile, flags_to_set) do { \ 144 (cibfile)->flags = pcmk__set_flags_as(__func__, __LINE__, \ 145 LOG_TRACE, "CIB file", \ 152 #define cib_clear_file_flags(cibfile, flags_to_clear) do { \ 153 (cibfile)->flags = pcmk__clear_flags_as(__func__, __LINE__, \ 154 LOG_TRACE, "CIB file", \ 179 return cib_op_functions[
type];
191 cib_file_is_live(
const char *filename)
193 gboolean same = FALSE;
195 if (filename != NULL) {
197 char *real_filename = NULL;
200 char *real_livename = NULL;
204 same = !strcmp(real_filename, real_livename);
214 cib_file_process_request(
cib_t *cib, xmlNode *request, xmlNode **output)
226 bool changed =
false;
227 bool read_only =
false;
228 xmlNode *result_cib = NULL;
229 xmlNode *cib_diff = NULL;
235 op_function = file_get_op_function(operation);
248 rc =
cib_perform_op(op, call_options, op_function, read_only, section,
249 request,
data,
true, &changed, &private->cib_xml,
250 &result_cib, &cib_diff, output);
262 }
else if ((rc ==
pcmk_ok) && !read_only) {
265 if (result_cib != private->cib_xml) {
267 private->cib_xml = result_cib;
278 if ((result_cib != private->cib_xml) && (result_cib != *output)) {
286 cib_file_perform_op_delegate(
cib_t *cib,
const char *op,
const char *
host,
287 const char *section, xmlNode *
data,
288 xmlNode **output_data,
int call_options,
289 const char *user_name)
292 xmlNode *request = NULL;
293 xmlNode *output = NULL;
298 crm_info(
"Handling %s operation for %s as %s",
299 pcmk__s(op,
"invalid"), pcmk__s(section,
"entire CIB"),
300 pcmk__s(user_name,
"default user"));
302 if (output_data != NULL) {
314 return -EPROTONOSUPPORT;
317 if (file_get_op_function(operation) == NULL) {
319 crm_err(
"Operation %s is not supported by CIB file clients", op);
320 return -EPROTONOSUPPORT;
338 rc = cib_file_process_request(cib, request, &output);
340 if ((output_data != NULL) && (output != NULL)) {
341 if (output->doc == private->cib_xml->doc) {
344 *output_data = output;
350 && (output->doc != private->cib_xml->doc)
351 && ((output_data == NULL) || (output != *output_data))) {
375 load_file_cib(
const char *filename, xmlNode **output)
378 xmlNode *root = NULL;
381 if (strcmp(filename,
"-") && (stat(filename, &buf) < 0)) {
400 crm_err(
"CIB does not validate against %s", schema);
416 if (private->filename == NULL) {
419 rc = load_file_cib(private->filename, &private->cib_xml);
423 crm_debug(
"Opened connection to local file '%s' for %s",
424 private->filename,
name);
427 register_client(cib);
430 crm_info(
"Connection to local file '%s' for %s (client %s) failed: %s",
446 cib_file_write_live(xmlNode *cib_root,
char *
path)
448 uid_t uid = geteuid();
449 struct passwd *daemon_pwent;
450 char *sep = strrchr(
path,
'/');
451 const char *cib_dirname, *cib_filename;
457 if (daemon_pwent == NULL) {
466 if ((uid != 0) && (uid != daemon_pwent->pw_uid)) {
467 crm_perror(LOG_ERR,
"Must be root or %s to modify live CIB",
479 }
else if (sep ==
path) {
481 cib_filename =
path + 1;
485 cib_filename = sep + 1;
490 cib_file_owner = daemon_pwent->pw_uid;
491 cib_file_group = daemon_pwent->pw_gid;
503 cib_do_chown = FALSE;
507 if ((sep != NULL) && (*sep ==
'\0')) {
528 cib_file_signoff(
cib_t *cib)
533 crm_debug(
"Disconnecting from the CIB manager");
536 unregister_client(cib);
544 if (cib_file_write_live(private->cib_xml, private->filename) < 0) {
559 crm_info(
"Wrote CIB to %s", private->filename);
562 crm_err(
"Could not write CIB to %s", private->filename);
568 private->cib_xml = NULL;
573 cib_file_free(
cib_t *cib)
578 rc = cib_file_signoff(cib);
585 free(private->filename);
592 fprintf(stderr,
"Couldn't sign off: %d\n", rc);
599 cib_file_inputfd(
cib_t *cib)
601 return -EPROTONOSUPPORT;
605 cib_file_register_notification(
cib_t *cib,
const char *callback,
int enabled)
607 return -EPROTONOSUPPORT;
611 cib_file_set_connection_dnotify(
cib_t *cib,
612 void (*dnotify) (gpointer user_data))
614 return -EPROTONOSUPPORT;
631 cib_file_client_id(
const cib_t *cib,
const char **async_id,
632 const char **sync_id)
636 if (async_id != NULL) {
637 *async_id =
private->id;
639 if (sync_id != NULL) {
640 *sync_id =
private->id;
657 if (
private == NULL) {
666 if (cib_location == NULL) {
667 cib_location = getenv(
"CIB_file");
668 CRM_CHECK(cib_location != NULL,
return NULL);
671 if (cib_file_is_live(cib_location)) {
673 crm_trace(
"File %s detected as live CIB", cib_location);
675 private->filename = strdup(cib_location);
702 cib_file_verify_digest(xmlNode *root,
const char *sigfile)
704 gboolean passed = FALSE;
710 if (expected == NULL) {
711 crm_err(
"On-disk digest at %s is empty", sigfile);
716 crm_warn(
"No on-disk digest present at %s", sigfile);
719 crm_err(
"Could not read on-disk digest from %s: %s",
748 char *local_sigfile = NULL;
749 xmlNode *local_root = NULL;
757 s_res = stat(filename, &buf);
759 crm_perror(LOG_WARNING,
"Could not verify cluster configuration file %s", filename);
761 }
else if (buf.st_size == 0) {
762 crm_warn(
"Cluster configuration file %s is corrupt (size is zero)", filename);
768 if (local_root == NULL) {
769 crm_warn(
"Cluster configuration file %s is corrupt (unparseable as XML)", filename);
774 if (sigfile == NULL) {
779 if (cib_file_verify_digest(local_root, sigfile) == FALSE) {
804 cib_file_backup(
const char *cib_dirname,
const char *cib_filename)
825 unlink(backup_digest);
828 if ((link(cib_path, backup_path) < 0) && (errno != ENOENT)) {
829 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
830 cib_path, backup_path);
834 }
else if ((link(cib_digest, backup_digest) < 0) && (errno != ENOENT)) {
835 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
836 cib_digest, backup_digest);
846 if ((chown(backup_path, cib_file_owner, cib_file_group) < 0)
847 && (errno != ENOENT)) {
848 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_path);
851 if ((chown(backup_digest, cib_file_owner, cib_file_group) < 0)
852 && (errno != ENOENT)) {
853 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_digest);
857 cib_file_owner, cib_file_group);
859 crm_err(
"Could not set owner of sequence file in %s: %s",
865 crm_info(
"Archived previous version as %s", backup_path);
887 cib_file_prepare_xml(xmlNode *root)
889 xmlNode *cib_status_root = NULL;
899 if (cib_status_root != NULL) {
919 const char *cib_filename)
938 CRM_ASSERT((cib_path != NULL) && (digest_path != NULL)
939 && (tmp_cib != NULL) && (tmp_digest != NULL));
942 crm_trace(
"Reading cluster configuration file %s", cib_path);
944 if ((rc !=
pcmk_ok) && (rc != -ENOENT)) {
945 crm_err(
"%s was manually modified while the cluster was active!",
952 if (cib_file_backup(cib_dirname, cib_filename) < 0) {
958 umask(S_IWGRP | S_IWOTH | S_IROTH);
959 cib_file_prepare_xml(cib_root);
962 fd = mkstemp(tmp_cib);
964 crm_perror(LOG_ERR,
"Couldn't open temporary file %s for writing CIB",
971 if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
972 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
977 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
978 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
986 crm_err(
"Changes couldn't be written to %s", tmp_cib);
994 crm_info(
"Wrote version %s.%s.0 of the CIB to disk (digest: %s)",
995 (admin_epoch ? admin_epoch :
"0"), (epoch ? epoch :
"0"), digest);
998 fd = mkstemp(tmp_digest);
1000 crm_perror(LOG_ERR,
"Could not create temporary file for CIB digest");
1004 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
1005 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
1013 crm_err(
"Could not write digest to %s: %s",
1020 crm_debug(
"Wrote digest %s to disk", digest);
1023 crm_info(
"Reading cluster configuration file %s (digest: %s)",
1024 tmp_cib, tmp_digest);
1030 if (rename(tmp_cib, cib_path) < 0) {
1031 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_cib, cib_path);
1034 if (rename(tmp_digest, digest_path) < 0) {
1035 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_digest,
1062 cib_file_process_transaction_requests(
cib_t *cib, xmlNode *transaction)
1069 xmlNode *output = NULL;
1072 int rc = cib_file_process_request(cib, request, &output);
1076 crm_err(
"Aborting transaction for CIB file client (%s) on file " 1077 "'%s' due to failed %s request: %s",
1078 private->id, private->filename, op,
pcmk_rc_str(rc));
1083 crm_trace(
"Applied %s request to transaction working CIB for CIB file " 1084 "client (%s) on file '%s'",
1085 op, private->id, private->filename);
1107 cib_file_commit_transaction(
cib_t *cib, xmlNode *transaction,
1108 xmlNode **result_cib)
1112 xmlNode *saved_cib =
private->cib_xml;
1124 CRM_CHECK((*result_cib != NULL) && (*result_cib != private->cib_xml),
1125 *result_cib =
copy_xml(private->cib_xml));
1127 crm_trace(
"Committing transaction for CIB file client (%s) on file '%s' to " 1129 private->id, private->filename);
1132 private->cib_xml = *result_cib;
1134 rc = cib_file_process_transaction_requests(cib, transaction);
1136 crm_trace(
"Transaction commit %s for CIB file client (%s) on file '%s'",
1137 ((rc ==
pcmk_rc_ok)?
"succeeded" :
"failed"),
1138 private->id, private->filename);
1146 *result_cib =
private->cib_xml;
1149 private->cib_xml = saved_cib;
1155 cib_file_process_commit_transaction(
const char *op,
int options,
1156 const char *section, xmlNode *req,
1157 xmlNode *
input, xmlNode *existing_cib,
1158 xmlNode **result_cib, xmlNode **answer)
1164 CRM_CHECK(client_id != NULL,
return -EINVAL);
1166 cib = get_client(client_id);
1169 rc = cib_file_commit_transaction(cib,
input, result_cib);
1173 crm_err(
"Could not commit transaction for CIB file client (%s) on "
#define CRM_CHECK(expr, failure_action)
#define pcmk_err_schema_validation
const char * pcmk_strerror(int rc)
char * crm_generate_uuid(void)
#define XML_ATTR_NUMUPDATES
int pcmk_rc2legacy(int rc)
int cib_process_delete(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
const char * pcmk__xe_add_last_written(xmlNode *xe)
int cib_process_replace(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
int(* signoff)(cib_t *cib)
xmlNode * first_named_child(const xmlNode *parent, const char *name)
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
xmlNode * find_xml_node(const xmlNode *root, const char *search_path, gboolean must_find)
int cib_process_erase(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_process_bump(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
int write_xml_file(const xmlNode *xml, const char *filename, gboolean compress)
Write XML to a file.
gboolean validate_xml_verbose(const xmlNode *xml_blob)
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
#define CRM_LOG_ASSERT(expr)
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
int(* inputfd)(cib_t *cib)
enum crm_ais_msg_types type
int pcmk__write_sync(int fd, const char *contents)
int cib_file_write_with_digest(xmlNode *cib_root, const char *cib_dirname, const char *cib_filename)
xmlNode * filename2xml(const char *filename)
struct cib_file_opaque_s cib_file_opaque_t
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
#define pcmk_err_cib_backup
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
#define XML_ATTR_GENERATION
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
xmlNode * copy_xml(xmlNode *src_node)
cib_t * cib_new_variant(void)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
#define crm_warn(fmt, args...)
#define pcmk_err_cib_save
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int cib_process_diff(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
xmlNode * pcmk_find_cib_element(xmlNode *cib, const char *element_name)
Find an element in the CIB.
#define crm_trace(fmt, args...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
void(* op_callback)(const xmlNode *msg, int call_id, int rc, xmlNode *output)
void pcmk__sync_directory(const char *name)
Wrappers for and extensions to libxml2.
int cib_perform_op(const char *op, int call_options, cib__op_fn_t fn, bool is_query, const char *section, xmlNode *req, xmlNode *input, bool manage_counters, bool *config_changed, xmlNode **current_cib, xmlNode **result_cib, xmlNode **diff, xmlNode **output)
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define XML_ATTR_VALIDATION
int pcmk_legacy2rc(int legacy_rc)
#define pcmk_err_cib_modified
void free_xml(xmlNode *child)
int cib__create_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name, const char *client_name, xmlNode **op_msg)
#define cib_clear_file_flags(cibfile, flags_to_clear)
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
char * calculate_on_disk_digest(xmlNode *local_cib)
Calculate and return digest of XML tree, suitable for storing on disk.
int(* cib__op_fn_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
int cib_process_query(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
#define cib_set_file_flags(cibfile, flags_to_set)
int write_xml_fd(const xmlNode *xml, const char *filename, int fd, gboolean compress)
Write XML to a file descriptor.
int cib_file_read_and_verify(const char *filename, const char *sigfile, xmlNode **root)
int pcmk__file_contents(const char *filename, char **contents)
#define T_CIB_TRANSACTION
#define cib__set_call_options(cib_call_opts, call_for, flags_to_set)
cib_t * cib_file_new(const char *cib_location)
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
int cib__extend_transaction(cib_t *cib, xmlNode *request)
#define crm_log_xml_info(xml, text)
#define XML_ATTR_GENERATION_ADMIN
uint32_t flags
Group of enum cib__op_attr flags.
IPC interface to Pacemaker daemons.
bool pcmk__verify_digest(xmlNode *input, const char *expected)
#define XML_CIB_TAG_STATUS
#define pcmk__log_xml_patchset(level, patchset)
#define crm_log_xml_trace(xml, text)
int pcmk__real_path(const char *path, char **resolved_path)
int cib__get_operation(const char *op, const cib__operation_t **operation)
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)
#define pcmk_err_cib_corrupt
int cib_process_upgrade(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
#define crm_info(fmt, args...)
Process request when the client commits the active transaction.
int cib_process_modify(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
bool pcmk__ends_with_ext(const char *s, const char *match)
int cib_process_create(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
xmlNode * crm_next_same_xml(const xmlNode *sibling)
Get next instance of same XML tag.