19 #include <sys/types.h>
28 #define CIB_FLAG_DIRTY 0x00001
29 #define CIB_FLAG_LIVE 0x00002
31 typedef struct cib_file_opaque_s {
38 xmlNode *
data, xmlNode ** output_data,
int call_options);
41 xmlNode *
data, xmlNode ** output_data,
int call_options,
42 const char *user_name);
49 cib_file_inputfd(
cib_t * cib)
51 return -EPROTONOSUPPORT;
55 cib_file_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
57 return -EPROTONOSUPPORT;
61 cib_file_register_notification(
cib_t * cib,
const char *callback,
int enabled)
63 return -EPROTONOSUPPORT;
76 cib_file_verify_digest(xmlNode *root,
const char *sigfile)
78 gboolean passed = FALSE;
84 if (expected == NULL) {
85 crm_err(
"On-disk digest at %s is empty", sigfile);
90 crm_warn(
"No on-disk digest present at %s", sigfile);
93 crm_err(
"Could not read on-disk digest from %s: %s",
122 char *local_sigfile = NULL;
123 xmlNode *local_root = NULL;
131 s_res = stat(filename, &buf);
133 crm_perror(LOG_WARNING,
"Could not verify cluster configuration file %s", filename);
135 }
else if (buf.st_size == 0) {
136 crm_warn(
"Cluster configuration file %s is corrupt (size is zero)", filename);
142 if (local_root == NULL) {
143 crm_warn(
"Cluster configuration file %s is corrupt (unparseable as XML)", filename);
148 if (sigfile == NULL) {
153 if (cib_file_verify_digest(local_root, sigfile) == FALSE) {
168 #define CIB_SERIES "cib"
169 #define CIB_SERIES_MAX 100
170 #define CIB_SERIES_BZIP FALSE
174 #define CIB_LIVE_NAME CIB_SERIES ".xml"
185 cib_file_is_live(
const char *filename)
187 gboolean same = FALSE;
189 if (filename != NULL) {
191 char *real_filename = NULL;
194 char *real_livename = NULL;
198 same = !strcmp(real_filename, real_livename);
211 static uid_t cib_file_owner = 0;
212 static uid_t cib_file_group = 0;
213 static gboolean cib_do_chown = FALSE;
225 cib_file_backup(
const char *cib_dirname,
const char *cib_filename)
246 unlink(backup_digest);
249 if ((link(cib_path, backup_path) < 0) && (errno != ENOENT)) {
250 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
251 cib_path, backup_path);
255 }
else if ((link(cib_digest, backup_digest) < 0) && (errno != ENOENT)) {
256 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
257 cib_digest, backup_digest);
267 if ((chown(backup_path, cib_file_owner, cib_file_group) < 0)
268 && (errno != ENOENT)) {
269 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_path);
272 if ((chown(backup_digest, cib_file_owner, cib_file_group) < 0)
273 && (errno != ENOENT)) {
274 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_digest);
278 cib_file_owner, cib_file_group);
280 crm_err(
"Could not set owner of sequence file in %s: %s",
286 crm_info(
"Archived previous version as %s", backup_path);
308 cib_file_prepare_xml(xmlNode *root)
310 xmlNode *cib_status_root = NULL;
320 if (cib_status_root != NULL) {
340 const char *cib_filename)
359 CRM_ASSERT((cib_path != NULL) && (digest_path != NULL)
360 && (tmp_cib != NULL) && (tmp_digest != NULL));
363 crm_trace(
"Reading cluster configuration file %s", cib_path);
365 if ((rc !=
pcmk_ok) && (rc != -ENOENT)) {
366 crm_err(
"%s was manually modified while the cluster was active!",
373 if (cib_file_backup(cib_dirname, cib_filename) < 0) {
379 umask(S_IWGRP | S_IWOTH | S_IROTH);
380 cib_file_prepare_xml(cib_root);
383 fd = mkstemp(tmp_cib);
385 crm_perror(LOG_ERR,
"Couldn't open temporary file %s for writing CIB",
392 if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
393 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
398 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
399 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
407 crm_err(
"Changes couldn't be written to %s", tmp_cib);
415 crm_info(
"Wrote version %s.%s.0 of the CIB to disk (digest: %s)",
416 (admin_epoch ? admin_epoch :
"0"), (epoch ? epoch :
"0"), digest);
419 fd = mkstemp(tmp_digest);
421 crm_perror(LOG_ERR,
"Could not create temporary file for CIB digest");
425 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
426 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
434 crm_err(
"Could not write digest to %s: %s",
441 crm_debug(
"Wrote digest %s to disk", digest);
444 crm_info(
"Reading cluster configuration file %s (digest: %s)",
445 tmp_cib, tmp_digest);
451 if (rename(tmp_cib, cib_path) < 0) {
452 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_cib, cib_path);
455 if (rename(tmp_digest, digest_path) < 0) {
456 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_digest,
478 CRM_ASSERT((cib != NULL) && (
private != NULL));
483 if (cib_location == NULL) {
484 cib_location = getenv(
"CIB_file");
487 if (cib_file_is_live(cib_location)) {
489 crm_trace(
"File %s detected as live CIB", cib_location);
491 private->filename = strdup(cib_location);
506 static xmlNode *in_mem_cib = NULL;
523 load_file_cib(
const char *filename)
526 xmlNode *root = NULL;
529 if (stat(filename, &buf) < 0) {
548 crm_err(
"CIB does not validate against %s", schema);
564 if (private->filename == NULL) {
567 rc = load_file_cib(private->filename);
571 crm_debug(
"Opened connection to local file '%s' for %s",
572 private->filename, name);
577 crm_info(
"Connection to local file '%s' for %s failed: %s\n",
592 cib_file_write_live(
char *path)
594 uid_t uid = geteuid();
595 struct passwd *daemon_pwent;
596 char *sep = strrchr(path,
'/');
597 const char *cib_dirname, *cib_filename;
603 if (daemon_pwent == NULL) {
612 if ((uid != 0) && (uid != daemon_pwent->pw_uid)) {
613 crm_perror(LOG_ERR,
"Must be root or %s to modify live CIB",
625 }
else if (sep == path) {
627 cib_filename = path + 1;
631 cib_filename = sep + 1;
636 cib_file_owner = daemon_pwent->pw_uid;
637 cib_file_group = daemon_pwent->pw_gid;
649 cib_do_chown = FALSE;
653 if ((sep != NULL) && (*sep ==
'\0')) {
679 crm_debug(
"Disconnecting from the CIB manager");
688 if (cib_file_write_live(private->filename) < 0) {
696 if (
write_xml_file(in_mem_cib, private->filename, do_bzip) <= 0) {
702 crm_info(
"Wrote CIB to %s", private->filename);
705 crm_err(
"Could not write CIB to %s", private->filename);
727 free(private->filename);
733 fprintf(stderr,
"Couldn't sign off: %d\n", rc);
739 struct cib_func_entry {
746 static struct cib_func_entry cib_file_ops[] = {
761 xmlNode *
data, xmlNode ** output_data,
int call_options)
769 xmlNode *
data, xmlNode ** output_data,
int call_options,
770 const char *user_name)
773 char *effective_user = NULL;
774 gboolean query = FALSE;
775 gboolean changed = FALSE;
776 xmlNode *request = NULL;
777 xmlNode *output = NULL;
778 xmlNode *cib_diff = NULL;
779 xmlNode *result_cib = NULL;
782 static int max_msg_types =
DIMOF(cib_file_ops);
786 crm_info(
"Handling %s operation for %s as %s",
787 (op? op :
"invalid"), (section? section :
"entire CIB"),
788 (user_name? user_name :
"default user"));
790 crm_info(
"Handling %s operation for %s",
791 (op? op :
"invalid"), (section? section :
"entire CIB"));
800 if (output_data != NULL) {
808 for (lpc = 0; lpc < max_msg_types; lpc++) {
810 fn = &(cib_file_ops[lpc].fn);
811 query = cib_file_ops[lpc].read_only;
817 return -EPROTONOSUPPORT;
821 request =
cib_create_op(cib->
call_id,
"dummy-token", op, host, section, data, call_options, user_name);
834 section, request, data, TRUE, &changed, in_mem_cib, &result_cib, &cib_diff,
845 }
else if (query == FALSE) {
848 in_mem_cib = result_cib;
858 if (output_data && output) {
859 if(output == in_mem_cib) {
862 *output_data = output;
865 }
else if(output != in_mem_cib) {
869 free(effective_user);
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
#define pcmk_err_schema_validation
const char * pcmk_strerror(int rc)
#define XML_ATTR_NUMUPDATES
bool pcmk__ends_with_ext(const char *s, const char *match)
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
int cib_process_delete(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_process_replace(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
xmlNode * get_object_root(const char *object_type, xmlNode *the_root)
int pcmk__write_sync(int fd, const char *contents)
int(* cib_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
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_file_perform_op_delegate(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *user_name)
bool pcmk__verify_digest(xmlNode *input, const char *expected)
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(* inputfd)(cib_t *cib)
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(xmlNode *xml_node, const char *filename, gboolean compress)
Write XML to a file.
#define CRM_LOG_ASSERT(expr)
#define clear_bit(word, bit)
xmlNode * filename2xml(const char *filename)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
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
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
#define XML_ATTR_GENERATION
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
gboolean validate_xml_verbose(xmlNode *xml_blob)
xmlNode * cib_create_op(int call_id, const char *token, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
int cib_file_perform_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options)
xmlNode * copy_xml(xmlNode *src_node)
cib_t * cib_new_variant(void)
#define crm_warn(fmt, args...)
#define pcmk_err_cib_save
#define set_bit(word, bit)
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 CIB_OP_APPLY_DIFF
int pcmk__real_path(const char *path, char **resolved_path)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
int cib_file_signoff(cib_t *cib)
void(* op_callback)(const xmlNode *msg, int call_id, int rc, xmlNode *output)
#define crm_trace(fmt, args...)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Wrappers for and extensions to libxml2.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define XML_ATTR_VALIDATION
int pcmk__file_contents(const char *filename, char **contents)
#define pcmk_err_cib_modified
void free_xml(xmlNode *child)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
char * calculate_on_disk_digest(xmlNode *local_cib)
Calculate and return digest of XML tree, suitable for storing on disk.
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
cib_t * cib_file_new(const char *filename)
int cib_process_query(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_file_write_with_digest(xmlNode *cib_root, const char *cib_dirname, const char *cib_filename)
int cib_file_read_and_verify(const char *filename, const char *sigfile, xmlNode **root)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
int cib_perform_op(const char *op, int call_options, cib_op_t *fn, gboolean is_query, const char *section, xmlNode *req, xmlNode *input, gboolean manage_counters, gboolean *config_changed, xmlNode *current_cib, xmlNode **result_cib, xmlNode **diff, xmlNode **output)
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
#define XML_ATTR_GENERATION_ADMIN
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
void pcmk__sync_directory(const char *name)
Wrappers for and extensions to libqb IPC.
int cib_file_free(cib_t *cib)
#define XML_CIB_TAG_STATUS
int cib_file_signon(cib_t *cib, const char *name, enum cib_conn_type type)
int(* signoff)(cib_t *cib)
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
#define safe_str_eq(a, b)
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
Write XML to a file descriptor.
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#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...)
const char * crm_xml_add_last_written(xmlNode *xml_node)
int cib_process_modify(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_process_create(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
enum crm_ais_msg_types type