20 #include <sys/types.h> 35 typedef struct cib_file_opaque_s {
40 #define cib_set_file_flags(cibfile, flags_to_set) do { \ 41 (cibfile)->flags = pcmk__set_flags_as(__func__, __LINE__, \ 42 LOG_TRACE, "CIB file", \ 49 #define cib_clear_file_flags(cibfile, flags_to_clear) do { \ 50 (cibfile)->flags = pcmk__clear_flags_as(__func__, __LINE__, \ 51 LOG_TRACE, "CIB file", \ 59 xmlNode *
data, xmlNode ** output_data,
int call_options);
62 xmlNode *
data, xmlNode ** output_data,
int call_options,
63 const char *user_name);
70 cib_file_inputfd(
cib_t * cib)
72 return -EPROTONOSUPPORT;
76 cib_file_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
78 return -EPROTONOSUPPORT;
82 cib_file_register_notification(
cib_t * cib,
const char *callback,
int enabled)
84 return -EPROTONOSUPPORT;
97 cib_file_verify_digest(xmlNode *root,
const char *sigfile)
99 gboolean passed = FALSE;
105 if (expected == NULL) {
106 crm_err(
"On-disk digest at %s is empty", sigfile);
111 crm_warn(
"No on-disk digest present at %s", sigfile);
114 crm_err(
"Could not read on-disk digest from %s: %s",
143 char *local_sigfile = NULL;
144 xmlNode *local_root = NULL;
152 s_res = stat(filename, &buf);
154 crm_perror(LOG_WARNING,
"Could not verify cluster configuration file %s", filename);
156 }
else if (buf.st_size == 0) {
157 crm_warn(
"Cluster configuration file %s is corrupt (size is zero)", filename);
163 if (local_root == NULL) {
164 crm_warn(
"Cluster configuration file %s is corrupt (unparseable as XML)", filename);
169 if (sigfile == NULL) {
174 if (cib_file_verify_digest(local_root, sigfile) == FALSE) {
189 #define CIB_SERIES "cib" 190 #define CIB_SERIES_MAX 100 191 #define CIB_SERIES_BZIP FALSE 195 #define CIB_LIVE_NAME CIB_SERIES ".xml" 206 cib_file_is_live(
const char *filename)
208 gboolean same = FALSE;
210 if (filename != NULL) {
212 char *real_filename = NULL;
215 char *real_livename = NULL;
219 same = !strcmp(real_filename, real_livename);
232 static uid_t cib_file_owner = 0;
233 static uid_t cib_file_group = 0;
234 static gboolean cib_do_chown = FALSE;
246 cib_file_backup(
const char *cib_dirname,
const char *cib_filename)
267 unlink(backup_digest);
270 if ((link(cib_path, backup_path) < 0) && (errno != ENOENT)) {
271 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
272 cib_path, backup_path);
276 }
else if ((link(cib_digest, backup_digest) < 0) && (errno != ENOENT)) {
277 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
278 cib_digest, backup_digest);
288 if ((chown(backup_path, cib_file_owner, cib_file_group) < 0)
289 && (errno != ENOENT)) {
290 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_path);
293 if ((chown(backup_digest, cib_file_owner, cib_file_group) < 0)
294 && (errno != ENOENT)) {
295 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_digest);
299 cib_file_owner, cib_file_group);
301 crm_err(
"Could not set owner of sequence file in %s: %s",
307 crm_info(
"Archived previous version as %s", backup_path);
329 cib_file_prepare_xml(xmlNode *root)
331 xmlNode *cib_status_root = NULL;
341 if (cib_status_root != NULL) {
361 const char *cib_filename)
380 CRM_ASSERT((cib_path != NULL) && (digest_path != NULL)
381 && (tmp_cib != NULL) && (tmp_digest != NULL));
384 crm_trace(
"Reading cluster configuration file %s", cib_path);
387 crm_err(
"%s was manually modified while the cluster was active!",
394 if (cib_file_backup(cib_dirname, cib_filename) < 0) {
400 umask(S_IWGRP | S_IWOTH | S_IROTH);
401 cib_file_prepare_xml(cib_root);
404 fd = mkstemp(tmp_cib);
406 crm_perror(LOG_ERR,
"Couldn't open temporary file %s for writing CIB",
413 if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
414 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
419 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
420 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
428 crm_err(
"Changes couldn't be written to %s", tmp_cib);
436 crm_info(
"Wrote version %s.%s.0 of the CIB to disk (digest: %s)",
437 (admin_epoch ? admin_epoch :
"0"), (epoch ? epoch :
"0"), digest);
440 fd = mkstemp(tmp_digest);
442 crm_perror(LOG_ERR,
"Could not create temporary file for CIB digest");
446 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
447 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
455 crm_err(
"Could not write digest to %s: %s",
462 crm_debug(
"Wrote digest %s to disk", digest);
465 crm_info(
"Reading cluster configuration file %s (digest: %s)",
466 tmp_cib, tmp_digest);
472 if (rename(tmp_cib, cib_path) < 0) {
473 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_cib, cib_path);
476 if (rename(tmp_digest, digest_path) < 0) {
477 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_digest,
499 CRM_ASSERT((cib != NULL) && (
private != NULL));
504 if (cib_location == NULL) {
505 cib_location = getenv(
"CIB_file");
508 if (cib_file_is_live(cib_location)) {
510 crm_trace(
"File %s detected as live CIB", cib_location);
512 private->filename = strdup(cib_location);
527 static xmlNode *in_mem_cib = NULL;
544 load_file_cib(
const char *filename)
547 xmlNode *root = NULL;
550 if (stat(filename, &buf) < 0) {
569 crm_err(
"CIB does not validate against %s", schema);
585 if (private->filename == NULL) {
588 rc = load_file_cib(private->filename);
592 crm_debug(
"Opened connection to local file '%s' for %s",
593 private->filename,
name);
598 crm_info(
"Connection to local file '%s' for %s failed: %s\n",
613 cib_file_write_live(
char *path)
615 uid_t uid = geteuid();
616 struct passwd *daemon_pwent;
617 char *sep = strrchr(path,
'/');
618 const char *cib_dirname, *cib_filename;
624 if (daemon_pwent == NULL) {
633 if ((uid != 0) && (uid != daemon_pwent->pw_uid)) {
634 crm_perror(LOG_ERR,
"Must be root or %s to modify live CIB",
646 }
else if (sep == path) {
648 cib_filename = path + 1;
652 cib_filename = sep + 1;
657 cib_file_owner = daemon_pwent->pw_uid;
658 cib_file_group = daemon_pwent->pw_gid;
670 cib_do_chown = FALSE;
674 if ((sep != NULL) && (*sep ==
'\0')) {
700 crm_debug(
"Disconnecting from the CIB manager");
709 if (cib_file_write_live(private->filename) < 0) {
717 if (
write_xml_file(in_mem_cib, private->filename, do_bzip) <= 0) {
723 crm_info(
"Wrote CIB to %s", private->filename);
726 crm_err(
"Could not write CIB to %s", private->filename);
748 free(private->filename);
754 fprintf(stderr,
"Couldn't sign off: %d\n",
rc);
760 struct cib_func_entry {
767 static struct cib_func_entry cib_file_ops[] = {
782 xmlNode *
data, xmlNode ** output_data,
int call_options)
790 xmlNode *
data, xmlNode ** output_data,
int call_options,
791 const char *user_name)
794 char *effective_user = NULL;
795 gboolean query = FALSE;
796 gboolean changed = FALSE;
797 xmlNode *request = NULL;
798 xmlNode *output = NULL;
799 xmlNode *cib_diff = NULL;
800 xmlNode *result_cib = NULL;
803 static int max_msg_types =
DIMOF(cib_file_ops);
807 crm_info(
"Handling %s operation for %s as %s",
808 (op? op :
"invalid"), (section? section :
"entire CIB"),
809 (user_name? user_name :
"default user"));
811 crm_info(
"Handling %s operation for %s",
812 (op? op :
"invalid"), (section? section :
"entire CIB"));
822 if (output_data != NULL) {
830 for (lpc = 0; lpc < max_msg_types; lpc++) {
832 fn = &(cib_file_ops[lpc].fn);
833 query = cib_file_ops[lpc].read_only;
839 return -EPROTONOSUPPORT;
856 section, request,
data, TRUE, &changed, in_mem_cib, &result_cib, &cib_diff,
867 }
else if (query == FALSE) {
870 in_mem_cib = result_cib;
880 if (output_data && output) {
881 if(output == in_mem_cib) {
884 *output_data = output;
887 }
else if(output != in_mem_cib) {
891 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
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)
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)
int(* signoff)(cib_t *cib)
xmlNode * get_object_root(const char *object_type, xmlNode *the_root)
int pcmk__write_sync(int fd, const char *contents)
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)
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)
int(* inputfd)(cib_t *cib)
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
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)
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 CIB_OP_APPLY_DIFF
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
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)
int(* cib_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
#define crm_trace(fmt, args...)
#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)
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)
#define cib_clear_file_flags(cibfile, flags_to_clear)
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.
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
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)
#define cib__set_call_options(cib_call_opts, call_for, flags_to_set)
cib_t * cib_file_new(const char *cib_location)
#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)
#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)
IPC interface to Pacemaker daemons.
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)
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
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...)
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)
enum crm_ais_msg_types type