22 #include <sys/types.h> 31 #define CIB_SERIES "cib" 32 #define CIB_SERIES_MAX 100 33 #define CIB_SERIES_BZIP FALSE 37 #define CIB_LIVE_NAME CIB_SERIES ".xml" 40 static GHashTable *client_table = NULL;
47 typedef struct cib_file_opaque_s {
54 static int cib_file_process_commit_transaction(
const char *op,
int options,
56 xmlNode *req, xmlNode *
input,
57 xmlNode *existing_cib,
68 register_client(
const cib_t *cib)
72 if (client_table == NULL) {
75 g_hash_table_insert(client_table, private->id, (gpointer) cib);
85 unregister_client(
const cib_t *cib)
89 if (client_table == NULL) {
93 g_hash_table_remove(client_table, private->id);
98 if (g_hash_table_size(client_table) == 0) {
99 g_hash_table_destroy(client_table);
113 get_client(
const char *client_id)
115 if (client_table == NULL) {
118 return g_hash_table_lookup(client_table, (gpointer) client_id);
138 static uid_t cib_file_owner = 0;
139 static uid_t cib_file_group = 0;
140 static gboolean cib_do_chown = FALSE;
142 #define cib_set_file_flags(cibfile, flags_to_set) do { \ 143 (cibfile)->flags = pcmk__set_flags_as(__func__, __LINE__, \ 144 LOG_TRACE, "CIB file", \ 151 #define cib_clear_file_flags(cibfile, flags_to_clear) do { \ 152 (cibfile)->flags = pcmk__clear_flags_as(__func__, __LINE__, \ 153 LOG_TRACE, "CIB file", \ 178 return cib_op_functions[
type];
190 cib_file_is_live(
const char *filename)
192 gboolean same = FALSE;
194 if (filename != NULL) {
196 char *real_filename = NULL;
199 char *real_livename = NULL;
203 same = !strcmp(real_filename, real_livename);
213 cib_file_process_request(
cib_t *cib, xmlNode *request, xmlNode **output)
227 bool changed =
false;
228 bool read_only =
false;
229 xmlNode *result_cib = NULL;
230 xmlNode *cib_diff = NULL;
236 op_function = file_get_op_function(operation);
253 rc =
cib_perform_op(cib, op, call_options, op_function, read_only, section,
254 request,
data,
true, &changed, &private->cib_xml,
255 &result_cib, &cib_diff, output);
268 }
else if ((rc ==
pcmk_ok) && !read_only) {
271 if (result_cib != private->cib_xml) {
273 private->cib_xml = result_cib;
279 if ((result_cib != private->cib_xml) && (result_cib != *output)) {
287 cib_file_perform_op_delegate(
cib_t *cib,
const char *op,
const char *
host,
288 const char *section, xmlNode *
data,
289 xmlNode **output_data,
int call_options,
290 const char *user_name)
293 xmlNode *request = NULL;
294 xmlNode *output = NULL;
299 crm_info(
"Handling %s operation for %s as %s",
300 pcmk__s(op,
"invalid"), pcmk__s(section,
"entire CIB"),
301 pcmk__s(user_name,
"default user"));
303 if (output_data != NULL) {
315 return -EPROTONOSUPPORT;
318 if (file_get_op_function(operation) == NULL) {
320 crm_err(
"Operation %s is not supported by CIB file clients", op);
321 return -EPROTONOSUPPORT;
339 rc = cib_file_process_request(cib, request, &output);
341 if ((output_data != NULL) && (output != NULL)) {
342 if (output->doc == private->cib_xml->doc) {
345 *output_data = output;
351 && (output->doc != private->cib_xml->doc)
352 && ((output_data == NULL) || (output != *output_data))) {
376 load_file_cib(
const char *filename, xmlNode **output)
379 xmlNode *root = NULL;
382 if (strcmp(filename,
"-") && (stat(filename, &buf) < 0)) {
414 if (private->filename == NULL) {
417 rc = load_file_cib(private->filename, &private->cib_xml);
421 crm_debug(
"Opened connection to local file '%s' for %s",
422 private->filename, pcmk__s(
name,
"client"));
425 register_client(cib);
428 crm_info(
"Connection to local file '%s' for %s (client %s) failed: %s",
429 private->filename, pcmk__s(
name,
"client"), private->id,
445 cib_file_write_live(xmlNode *cib_root,
char *
path)
447 uid_t uid = geteuid();
448 struct passwd *daemon_pwent;
449 char *sep = strrchr(
path,
'/');
450 const char *cib_dirname, *cib_filename;
456 if (daemon_pwent == NULL) {
465 if ((uid != 0) && (uid != daemon_pwent->pw_uid)) {
466 crm_perror(LOG_ERR,
"Must be root or %s to modify live CIB",
478 }
else if (sep ==
path) {
480 cib_filename =
path + 1;
484 cib_filename = sep + 1;
489 cib_file_owner = daemon_pwent->pw_uid;
490 cib_file_group = daemon_pwent->pw_gid;
502 cib_do_chown = FALSE;
506 if ((sep != NULL) && (*sep ==
'\0')) {
527 cib_file_signoff(
cib_t *cib)
532 crm_debug(
"Disconnecting from the CIB manager");
535 unregister_client(cib);
543 if (cib_file_write_live(private->cib_xml, private->filename) < 0) {
558 crm_info(
"Wrote CIB to %s", private->filename);
561 crm_err(
"Could not write CIB to %s", private->filename);
567 private->cib_xml = NULL;
572 cib_file_free(
cib_t *cib)
577 rc = cib_file_signoff(cib);
584 free(private->filename);
591 fprintf(stderr,
"Couldn't sign off: %d\n", rc);
598 cib_file_register_notification(
cib_t *cib,
const char *callback,
int enabled)
600 return -EPROTONOSUPPORT;
604 cib_file_set_connection_dnotify(
cib_t *cib,
605 void (*dnotify) (gpointer user_data))
607 return -EPROTONOSUPPORT;
624 cib_file_client_id(
const cib_t *cib,
const char **async_id,
625 const char **sync_id)
629 if (async_id != NULL) {
630 *async_id =
private->id;
632 if (sync_id != NULL) {
633 *sync_id =
private->id;
643 char *filename = NULL;
645 if (cib_location == NULL) {
646 cib_location = getenv(
"CIB_file");
647 if (cib_location == NULL) {
657 filename = strdup(cib_location);
658 if (filename == NULL) {
664 if (
private == NULL) {
671 private->filename = filename;
677 if (cib_file_is_live(cib_location)) {
679 crm_trace(
"File %s detected as live CIB", cib_location);
705 cib_file_verify_digest(xmlNode *root,
const char *sigfile)
707 gboolean passed = FALSE;
713 if (expected == NULL) {
714 crm_err(
"On-disk digest at %s is empty", sigfile);
719 crm_warn(
"No on-disk digest present at %s", sigfile);
722 crm_err(
"Could not read on-disk digest from %s: %s",
751 char *local_sigfile = NULL;
752 xmlNode *local_root = NULL;
760 s_res = stat(filename, &buf);
762 crm_perror(LOG_WARNING,
"Could not verify cluster configuration file %s", filename);
764 }
else if (buf.st_size == 0) {
765 crm_warn(
"Cluster configuration file %s is corrupt (size is zero)", filename);
771 if (local_root == NULL) {
772 crm_warn(
"Cluster configuration file %s is corrupt (unparseable as XML)", filename);
777 if (sigfile == NULL) {
782 if (cib_file_verify_digest(local_root, sigfile) == FALSE) {
807 cib_file_backup(
const char *cib_dirname,
const char *cib_filename)
810 unsigned int seq = 0U;
828 unlink(backup_digest);
831 if ((link(cib_path, backup_path) < 0) && (errno != ENOENT)) {
832 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
833 cib_path, backup_path);
837 }
else if ((link(cib_digest, backup_digest) < 0) && (errno != ENOENT)) {
838 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
839 cib_digest, backup_digest);
849 if ((chown(backup_path, cib_file_owner, cib_file_group) < 0)
850 && (errno != ENOENT)) {
851 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_path);
854 if ((chown(backup_digest, cib_file_owner, cib_file_group) < 0)
855 && (errno != ENOENT)) {
856 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_digest);
860 cib_file_owner, cib_file_group);
862 crm_err(
"Could not set owner of sequence file in %s: %s",
868 crm_info(
"Archived previous version as %s", backup_path);
890 cib_file_prepare_xml(xmlNode *root)
892 xmlNode *cib_status_root = NULL;
901 CRM_CHECK(cib_status_root != NULL,
return);
920 const char *cib_filename)
939 crm_trace(
"Reading cluster configuration file %s", cib_path);
941 if ((rc !=
pcmk_ok) && (rc != -ENOENT)) {
942 crm_err(
"%s was manually modified while the cluster was active!",
949 if (cib_file_backup(cib_dirname, cib_filename) < 0) {
955 umask(S_IWGRP | S_IWOTH | S_IROTH);
956 cib_file_prepare_xml(cib_root);
959 fd = mkstemp(tmp_cib);
961 crm_perror(LOG_ERR,
"Couldn't open temporary file %s for writing CIB",
968 if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
969 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
974 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
975 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
983 crm_err(
"Changes couldn't be written to %s", tmp_cib);
991 crm_info(
"Wrote version %s.%s.0 of the CIB to disk (digest: %s)",
992 (admin_epoch ? admin_epoch :
"0"), (epoch ? epoch :
"0"), digest);
995 fd = mkstemp(tmp_digest);
997 crm_perror(LOG_ERR,
"Could not create temporary file for CIB digest");
1001 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
1002 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
1010 crm_err(
"Could not write digest to %s: %s",
1017 crm_debug(
"Wrote digest %s to disk", digest);
1020 crm_info(
"Reading cluster configuration file %s (digest: %s)",
1021 tmp_cib, tmp_digest);
1027 if (rename(tmp_cib, cib_path) < 0) {
1028 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_cib, cib_path);
1031 if (rename(tmp_digest, digest_path) < 0) {
1032 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_digest,
1059 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),
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
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
const char * pcmk_strerror(int rc)
char * crm_generate_uuid(void)
int pcmk_rc2legacy(int rc)
enum pcmk_ipc_server type
int cib_process_delete(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
#define PCMK__XA_CIB_CLIENTID
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)
#define PCMK__XE_CIB_COMMAND
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
#define PCMK_XA_NUM_UPDATES
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)
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
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)
#define PCMK__XA_CIB_SECTION
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
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
void pcmk__xml_free(xmlNode *xml)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
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)
#define PCMK__XA_CIB_CALLOPT
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
xmlNode * pcmk_find_cib_element(xmlNode *cib, const char *element_name)
Find an element in the CIB.
int pcmk__xml_write_file(const xmlNode *xml, const char *filename, bool compress)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
int pcmk__xe_get_flags(const xmlNode *xml, const char *name, uint32_t *dest, uint32_t default_value)
#define crm_trace(fmt, args...)
bool pcmk__validate_xml(xmlNode *xml_blob, const char *validation, xmlRelaxNGValidityErrorFunc error_handler, void *error_handler_context)
#define PCMK__XA_ACL_TARGET
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
#define PCMK__XE_CIB_CALLDATA
char * pcmk__digest_on_disk_cib(xmlNode *input)
void pcmk__sync_directory(const char *name)
Wrappers for and extensions to libxml2.
const char * pcmk__xe_add_last_written(xmlNode *xe)
int pcmk__xml_write_fd(const xmlNode *xml, const char *filename, int fd)
int pcmk_legacy2rc(int legacy_rc)
#define pcmk_err_cib_modified
char * pcmk__series_filename(const char *directory, const char *series, unsigned int sequence, bool bzip)
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)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
xmlNode * pcmk__xe_next(const xmlNode *node, const char *element_name)
xmlNode * pcmk__xml_read(const char *filename)
#define pcmk__assert(expr)
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 cib_file_read_and_verify(const char *filename, const char *sigfile, xmlNode **root)
int pcmk__file_contents(const char *filename, char **contents)
#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)
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
#define crm_log_xml_info(xml, text)
uint32_t flags
Group of enum cib__op_attr flags.
IPC interface to Pacemaker daemons.
#define pcmk__log_xml_patchset(level, patchset)
#define crm_log_xml_trace(xml, text)
bool pcmk__configured_schema_validates(xmlNode *xml)
int pcmk__real_path(const char *path, char **resolved_path)
int cib__get_operation(const char *op, const cib__operation_t **operation)
bool pcmk__verify_digest(xmlNode *input, const char *expected)
#define PCMK_XA_ADMIN_EPOCH
#define PCMK__XE_CIB_TRANSACTION
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
#define PCMK__XA_CIB_CALLID
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)
int cib_perform_op(cib_t *cib, const char *op, uint32_t 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)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1