Page MenuHomeClusterLabs Projects

No OneTemporary

diff --git a/include/corosync/hdb.h b/include/corosync/hdb.h
index bf1d45d8..08bc20f8 100644
--- a/include/corosync/hdb.h
+++ b/include/corosync/hdb.h
@@ -1,361 +1,369 @@
/*
* Copyright (c) 2002-2006 MontaVista Software, Inc.
* Copyright (c) 2006-2009 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Steven Dake (sdake@redhat.com)
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the MontaVista Software, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef HDB_H_DEFINED
#define HDB_H_DEFINED
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
+#include <stdint.h>
+#include <inttypes.h>
-typedef unsigned long long hdb_handle_t;
+typedef uint64_t hdb_handle_t;
+
+/*
+ * Formatting for string printing on 32/64 bit systems
+ */
+#define HDB_D_FORMAT "%"PRIu64
+#define HDB_X_FORMAT "%"PRIx64
enum HDB_HANDLE_STATE {
HDB_HANDLE_STATE_EMPTY,
HDB_HANDLE_STATE_PENDINGREMOVAL,
HDB_HANDLE_STATE_ACTIVE
};
struct hdb_handle {
int state;
void *instance;
int check;
int ref_count;
};
struct hdb_handle_database {
unsigned int handle_count;
struct hdb_handle *handles;
unsigned int iterator;
void (*destructor) (void *);
#if defined(HAVE_PTHREAD_SPIN_LOCK)
pthread_spinlock_t lock;
#else
pthread_mutex_t lock;
#endif
unsigned int first_run;
};
#if defined(HAVE_PTHREAD_SPIN_LOCK)
static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
{
pthread_spin_lock (spinlock);
}
static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
{
pthread_spin_unlock (spinlock);
}
static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
{
pthread_spin_init (spinlock, 0);
}
static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
{
pthread_spin_destroy (spinlock);
}
#else
static inline void hdb_database_lock (pthread_mutex_t *mutex)
{
pthread_mutex_lock (mutex);
}
static inline void hdb_database_unlock (pthread_mutex_t *mutex)
{
pthread_mutex_unlock (mutex);
}
static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
{
pthread_mutex_init (mutex, NULL);
}
static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
{
pthread_mutex_destroy (mutex);
}
#endif
#define DECLARE_HDB_DATABASE(database_name,destructor_function) \
static struct hdb_handle_database (database_name) = { \
.handle_count = 0, \
.handles = NULL, \
.iterator = 0, \
.destructor = destructor_function, \
.first_run = 0 \
}; \
static void database_name##_init(void)__attribute__((constructor)); \
static void database_name##_init(void) \
{ \
hdb_database_lock_init (&(database_name).lock); \
}
#define DECLARE_HDB_DATABASE_FIRSTRUN(database_name) \
static struct hdb_handle_database (database_name) = { \
.first_run = 1, \
}; \
static void database_name##_init(void)__attribute__((constructor)); \
static void database_name##_init(void) \
{ \
memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
hdb_database_lock_init (&(database_name).lock); \
}
static inline void hdb_create (
struct hdb_handle_database *handle_database)
{
memset (handle_database, 0, sizeof (struct hdb_handle_database));
hdb_database_lock_init (&handle_database->lock);
}
static inline void hdb_destroy (
struct hdb_handle_database *handle_database)
{
free (handle_database->handles);
hdb_database_lock_destroy (&handle_database->lock);
memset (handle_database, 0, sizeof (struct hdb_handle_database));
}
static inline int hdb_handle_create (
struct hdb_handle_database *handle_database,
int instance_size,
hdb_handle_t *handle_id_out)
{
int handle;
unsigned int check;
void *new_handles;
int found = 0;
void *instance;
int i;
if (handle_database->first_run == 1) {
memset (handle_database, 0, sizeof (struct hdb_handle_database));
hdb_database_lock_init (&handle_database->lock);
}
hdb_database_lock (&handle_database->lock);
for (handle = 0; handle < handle_database->handle_count; handle++) {
if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
found = 1;
break;
}
}
if (found == 0) {
handle_database->handle_count += 1;
new_handles = (struct hdb_handle *)realloc (handle_database->handles,
sizeof (struct hdb_handle) * handle_database->handle_count);
if (new_handles == NULL) {
hdb_database_unlock (&handle_database->lock);
errno = ENOMEM;
return (-1);
}
handle_database->handles = new_handles;
}
instance = (void *)malloc (instance_size);
if (instance == 0) {
errno = ENOMEM;
return (-1);
}
/*
* This code makes sure the random number isn't zero
* We use 0 to specify an invalid handle out of the 1^64 address space
* If we get 0 200 times in a row, the RNG may be broken
*/
for (i = 0; i < 200; i++) {
check = random();
if (check != 0 && check != 0xffffffff) {
break;
}
}
memset (instance, 0, instance_size);
handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
handle_database->handles[handle].instance = instance;
handle_database->handles[handle].ref_count = 1;
handle_database->handles[handle].check = check;
*handle_id_out = (((unsigned long long)(check)) << 32) | handle;
hdb_database_unlock (&handle_database->lock);
return (0);
}
static inline int hdb_handle_get (
struct hdb_handle_database *handle_database,
hdb_handle_t handle_in,
void **instance)
{
unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
unsigned int handle = handle_in & 0xffffffff;
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
hdb_database_unlock (&handle_database->lock);
errno = EBADF;
return (-1);
}
*instance = NULL;
if (handle >= handle_database->handle_count) {
hdb_database_unlock (&handle_database->lock);
errno = EBADF;
return (-1);
}
if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
hdb_database_unlock (&handle_database->lock);
errno = EBADF;
return (-1);
}
*instance = handle_database->handles[handle].instance;
handle_database->handles[handle].ref_count += 1;
hdb_database_unlock (&handle_database->lock);
return (0);
}
static inline int hdb_handle_put (
struct hdb_handle_database *handle_database,
hdb_handle_t handle_in)
{
unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
unsigned int handle = handle_in & 0xffffffff;
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
hdb_database_unlock (&handle_database->lock);
errno = EBADF;
return (-1);
}
handle_database->handles[handle].ref_count -= 1;
assert (handle_database->handles[handle].ref_count >= 0);
if (handle_database->handles[handle].ref_count == 0) {
if (handle_database->destructor) {
handle_database->destructor (handle_database->handles[handle].instance);
}
free (handle_database->handles[handle].instance);
memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
}
hdb_database_unlock (&handle_database->lock);
return (0);
}
static inline int hdb_handle_destroy (
struct hdb_handle_database *handle_database,
hdb_handle_t handle_in)
{
unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
unsigned int handle = handle_in & 0xffffffff;
int res;
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
hdb_database_unlock (&handle_database->lock);
errno = EBADF;
return (-1);
}
handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
hdb_database_unlock (&handle_database->lock);
res = hdb_handle_put (handle_database, handle);
return (res);
}
static inline void hdb_iterator_reset (
struct hdb_handle_database *handle_database)
{
handle_database->iterator = 0;
}
static inline int hdb_iterator_next (
struct hdb_handle_database *handle_database,
void **instance,
hdb_handle_t *handle)
{
int res = -1;
while (handle_database->iterator < handle_database->handle_count) {
*handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
res = hdb_handle_get (
handle_database,
*handle,
instance);
handle_database->iterator += 1;
if (res == 0) {
break;
}
}
return (res);
}
static inline unsigned int hdb_base_convert (hdb_handle_t handle)
{
return (handle & 0xffffffff);
}
static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
{
unsigned long long retvalue = 0xffffffffULL << 32 | handle;
return (retvalue);
}
#endif /* HDB_H_DEFINED */
diff --git a/test/testconfdb.c b/test/testconfdb.c
index 874e0eed..f9a3daf6 100644
--- a/test/testconfdb.c
+++ b/test/testconfdb.c
@@ -1,260 +1,260 @@
/*
* Copyright (c) 2008, 2009 Red Hat Inc
*
* All rights reserved.
*
* Author: Christine Caulfield <ccaulfie@redhat.com>
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the MontaVista Software, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/un.h>
#include <corosync/corotypes.h>
#include <corosync/confdb.h>
#define INCDEC_VALUE 45
/* Callbacks are not supported yet */
confdb_callbacks_t callbacks = {
.confdb_key_change_notify_fn = NULL,
.confdb_object_create_change_notify_fn = NULL,
.confdb_object_delete_change_notify_fn = NULL
};
/* Recursively dump the object tree */
static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, int depth)
{
hdb_handle_t object_handle;
char object_name[1024];
size_t object_name_len;
char key_name[1024];
size_t key_name_len;
char key_value[1024];
size_t key_value_len;
int res;
int i;
/* Show the keys */
res = confdb_key_iter_start(handle, parent_object_handle);
if (res != CS_OK) {
- printf( "error resetting key iterator for object %llu: %d\n", parent_object_handle, res);
+ printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
return;
}
while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
key_value, &key_value_len)) == CS_OK) {
key_name[key_name_len] = '\0';
key_value[key_value_len] = '\0';
for (i=0; i<depth; i++) printf(" ");
printf(" KEY %s=%s\n", key_name, key_value);
}
/* Show sub-objects */
res = confdb_object_iter_start(handle, parent_object_handle);
if (res != CS_OK) {
- printf( "error resetting object iterator for object %llu: %d\n", parent_object_handle, res);
+ printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res);
return;
}
while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) {
hdb_handle_t parent;
res = confdb_object_parent_get(handle, object_handle, &parent);
if (res != CS_OK) {
- printf( "error getting parent for object %lld: %d\n", object_handle, res);
+ printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res);
return;
}
for (i=0; i<depth; i++) printf(" ");
object_name[object_name_len] = '\0';
- printf("OBJECT: %s (%llu, parent: %llu)\n", object_name, object_handle, parent);
+ printf("OBJECT: %s ("HDB_X_FORMAT", parent: "HDB_X_FORMAT")\n", object_name, object_handle, parent);
/* Down we go ... */
print_config_tree(handle, object_handle, depth+1);
}
}
static void do_write_tests(confdb_handle_t handle)
{
int res;
unsigned int incdec_value;
hdb_handle_t object_handle;
char error_string[1024];
/* Add a scratch object and put some keys into it */
res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, "testconfdb", strlen("testconfdb"), &object_handle);
if (res != CS_OK) {
printf( "error creating 'testconfdb' object: %d\n", res);
return;
}
res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
if (res != CS_OK) {
printf( "error creating 'testconfdb' key 1: %d\n", res);
return;
}
res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
if (res != CS_OK) {
printf( "error creating 'testconfdb' key 2: %d\n", res);
return;
}
res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
if (res != CS_OK) {
printf( "error creating 'testconfdb' key 3: %d\n", res);
return;
}
res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
"newtwo", strlen("newtwo"));
if (res != CS_OK) {
printf( "error replace 'testconfdb' key 2: %d\n", res);
return;
}
/* Print it for verification */
print_config_tree(handle, object_handle, 0);
incdec_value = INCDEC_VALUE;
res = confdb_key_create(handle, object_handle, "incdec", strlen("incdec"), &incdec_value, sizeof(incdec_value));
if (res != CS_OK) {
printf( "error creating 'testconfdb' key 4: %d\n", res);
return;
}
res = confdb_key_increment(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
if (res != CS_OK) {
printf( "error incrementing 'testconfdb' key 4: %d\n", res);
return;
}
if (incdec_value == INCDEC_VALUE+1)
printf("incremented value = %d\n", incdec_value);
else
printf("ERROR: incremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE+1);
res = confdb_key_decrement(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
if (res != CS_OK) {
printf( "error decrementing 'testconfdb' key 4: %d\n", res);
return;
}
if (incdec_value == INCDEC_VALUE)
printf("decremented value = %d\n", incdec_value);
else
printf("ERROR: decremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE);
printf("-------------------------\n");
/* Remove it.
Check that it doesn't exist when the full tree dump runs next */
res = confdb_object_destroy(handle, object_handle);
if (res != CS_OK) {
printf( "error destroying 'testconfdb' object: %d\n", res);
return;
}
res = confdb_write(handle, error_string, sizeof error_string);
printf("confdb_write returned %d: %s\n", res, error_string);
}
int main (int argc, char *argv[]) {
confdb_handle_t handle;
int result;
hdb_handle_t totem_handle;
char key_value[256];
size_t value_len;
result = confdb_initialize (&handle, &callbacks);
if (result != CS_OK) {
printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
exit (1);
}
if (argv[1] && strcmp(argv[1], "write")==0)
do_write_tests(handle);
if (argv[1] && strcmp(argv[1], "reload")==0) {
/* Test reload interface */
result = confdb_reload(handle, 0, key_value, sizeof key_value);
printf ("Try to reload the config (noflush): %d (should be 1)\n", result);
result = confdb_reload(handle, 1, key_value, sizeof key_value);
printf ("Try to reload the config (flush): %d (should be 1)\n", result);
}
/* Test iterators */
print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
/* Find "totem" and dump bits of it again, to test the direct APIs */
result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
if (result != CS_OK) {
printf ("Could not start object_find %d\n", result);
exit (1);
}
result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
if (result != CS_OK) {
printf ("Could not object_find \"totem\": %d\n", result);
exit (1);
}
result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
if (result != CS_OK) {
printf ("Could not get \"version\" key: %d\n", result);
exit (1);
}
key_value[value_len] = '\0';
printf("totem/version = '%s'\n", key_value);
result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
if (result != CS_OK) {
printf ("Could not get \"secauth\" key: %d\n", result);
exit (1);
}
key_value[value_len] = '\0';
printf("totem/secauth = '%s'\n", key_value);
/* Try a call that fails */
result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
printf ("Get \"grot\" key returned: %d (should fail)\n", result);
result = confdb_finalize (handle);
printf ("Finalize result is %d (should be 1)\n", result);
return (0);
}
diff --git a/tools/corosync-objctl.c b/tools/corosync-objctl.c
index ccafbba3..b72c0974 100644
--- a/tools/corosync-objctl.c
+++ b/tools/corosync-objctl.c
@@ -1,649 +1,649 @@
/*
* Copyright (c) 2008 Allied Telesis Labs NZ
*
* All rights reserved.
*
* Author: Angus Salkeld <angus.salkeld@alliedtelesis.co.nz>
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the MontaVista Software, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <sys/select.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/un.h>
#include <corosync/corotypes.h>
#include <corosync/confdb.h>
#define SEPERATOR '.'
#define SEPERATOR_STR "."
#define OBJ_NAME_SIZE 512
typedef enum {
ACTION_READ,
ACTION_WRITE,
ACTION_CREATE,
ACTION_DELETE,
ACTION_PRINT_ALL,
ACTION_PRINT_DEFAULT,
ACTION_TRACK,
} action_types_t;
typedef enum {
FIND_OBJECT_ONLY,
FIND_OBJECT_OR_KEY,
FIND_KEY_ONLY
} find_object_of_type_t;
static void tail_key_changed(confdb_handle_t handle,
confdb_change_type_t change_type,
hdb_handle_t parent_object_handle,
hdb_handle_t object_handle,
const void *object_name,
size_t object_name_len,
const void *key_name,
size_t key_name_len,
const void *key_value,
size_t key_value_len);
static void tail_object_created(confdb_handle_t handle,
hdb_handle_t parent_object_handle,
hdb_handle_t object_handle,
const void *name_pt,
size_t name_len);
static void tail_object_deleted(confdb_handle_t handle,
hdb_handle_t parent_object_handle,
const void *name_pt,
size_t name_len);
static confdb_callbacks_t callbacks = {
.confdb_key_change_notify_fn = tail_key_changed,
.confdb_object_create_change_notify_fn = tail_object_created,
.confdb_object_delete_change_notify_fn = tail_object_deleted,
};
static int action;
/* Recursively dump the object tree */
static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, char * parent_name)
{
hdb_handle_t object_handle;
char object_name[OBJ_NAME_SIZE];
size_t object_name_len;
char key_name[OBJ_NAME_SIZE];
size_t key_name_len;
char key_value[OBJ_NAME_SIZE];
size_t key_value_len;
cs_error_t res;
int children_printed;
/* Show the keys */
res = confdb_key_iter_start(handle, parent_object_handle);
if (res != CS_OK) {
- fprintf(stderr, "error resetting key iterator for object %llu: %d\n", parent_object_handle, res);
+ fprintf(stderr, "error resetting key iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
exit(EXIT_FAILURE);
}
children_printed = 0;
while ( (res = confdb_key_iter(handle,
parent_object_handle,
key_name,
&key_name_len,
key_value,
&key_value_len)) == CS_OK) {
key_name[key_name_len] = '\0';
key_value[key_value_len] = '\0';
if (parent_name != NULL)
printf("%s%c%s=%s\n", parent_name, SEPERATOR,key_name, key_value);
else
printf("%s=%s\n", key_name, key_value);
children_printed++;
}
/* Show sub-objects */
res = confdb_object_iter_start(handle, parent_object_handle);
if (res != CS_OK) {
- fprintf(stderr, "error resetting object iterator for object %llu: %d\n", parent_object_handle, res);
+ fprintf(stderr, "error resetting object iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
exit(EXIT_FAILURE);
}
while ( (res = confdb_object_iter(handle,
parent_object_handle,
&object_handle,
object_name,
&object_name_len)) == CS_OK) {
object_name[object_name_len] = '\0';
if (parent_name != NULL) {
snprintf(key_value, OBJ_NAME_SIZE, "%s%c%s", parent_name, SEPERATOR, object_name);
} else {
if ((action == ACTION_PRINT_DEFAULT) && strcmp(object_name, "internal_configuration") == 0) continue;
snprintf(key_value, OBJ_NAME_SIZE, "%s", object_name);
}
print_config_tree(handle, object_handle, key_value);
children_printed++;
}
if (children_printed == 0 && parent_name != NULL) {
printf("%s\n", parent_name);
}
}
static int print_all(void)
{
confdb_handle_t handle;
int result;
result = confdb_initialize (&handle, &callbacks);
if (result != CS_OK) {
fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
return 1;
}
print_config_tree(handle, OBJECT_PARENT_HANDLE, NULL);
result = confdb_finalize (handle);
return 0;
}
static int print_help(void)
{
printf("\n");
printf ("usage: corosync-objctl object%ckey ... Print an object\n", SEPERATOR);
printf (" corosync-objctl -c object%cchild_obj ... Create Object\n", SEPERATOR);
printf (" corosync-objctl -d object%cchild_obj ... Delete object\n", SEPERATOR);
printf (" corosync-objctl -w object%cchild_obj.key=value ... Create a key\n", SEPERATOR);
printf (" corosync-objctl -t object%cchild_obj ... Track changes\n", SEPERATOR);
printf (" corosync-objctl -a Print all objects\n");
printf("\n");
return 0;
}
static cs_error_t validate_name(char * obj_name_pt)
{
if ((strchr (obj_name_pt, SEPERATOR) == NULL) &&
(strchr (obj_name_pt, '=') == NULL))
return CS_OK;
else
return CS_ERR_INVALID_PARAM;
}
static void get_parent_name(const char * name_pt, char * parent_name)
{
char * tmp;
strcpy(parent_name, name_pt);
/* first remove the value (it could be a file path */
tmp = strchr(parent_name, '=');
if (tmp != NULL) *tmp = '\0';
/* then truncate the child name */
tmp = strrchr(parent_name, SEPERATOR);
if (tmp != NULL) *tmp = '\0';
}
static void get_key(const char * name_pt, char * key_name, char * key_value)
{
char * tmp;
char str_copy[OBJ_NAME_SIZE];
strcpy(str_copy, name_pt);
/* first remove the value (it could have a SEPERATOR in it */
tmp = strchr(str_copy, '=');
if (tmp != NULL && strlen(tmp) > 0) {
strcpy(key_value, tmp+1);
*tmp = '\0';
} else {
key_value[0] = '\0';
}
/* then remove the name */
tmp = strrchr(str_copy, SEPERATOR);
if (tmp == NULL) tmp = str_copy;
strcpy(key_name, tmp+1);
}
static cs_error_t find_object (confdb_handle_t handle,
char * name_pt,
find_object_of_type_t type,
hdb_handle_t * out_handle)
{
char * obj_name_pt;
char * save_pt;
hdb_handle_t obj_handle;
confdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
char tmp_name[OBJ_NAME_SIZE];
cs_error_t res = CS_OK;
strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
while (obj_name_pt != NULL) {
res = confdb_object_find_start(handle, parent_object_handle);
if (res != CS_OK) {
fprintf (stderr, "Could not start object_find %d\n", res);
exit (EXIT_FAILURE);
}
res = confdb_object_find(handle, parent_object_handle,
obj_name_pt, strlen (obj_name_pt), &obj_handle);
if (res != CS_OK) {
return res;
}
parent_object_handle = obj_handle;
obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
}
*out_handle = parent_object_handle;
return res;
}
static void read_object(confdb_handle_t handle, char * name_pt)
{
char parent_name[OBJ_NAME_SIZE];
hdb_handle_t obj_handle;
cs_error_t res;
get_parent_name(name_pt, parent_name);
res = find_object (handle, name_pt, FIND_OBJECT_OR_KEY, &obj_handle);
if (res == CS_OK) {
print_config_tree(handle, obj_handle, parent_name);
}
}
static void write_key(confdb_handle_t handle, char * path_pt)
{
hdb_handle_t obj_handle;
char parent_name[OBJ_NAME_SIZE];
char key_name[OBJ_NAME_SIZE];
char key_value[OBJ_NAME_SIZE];
char old_key_value[OBJ_NAME_SIZE];
size_t old_key_value_len;
cs_error_t res;
/* find the parent object */
get_parent_name(path_pt, parent_name);
get_key(path_pt, key_name, key_value);
if (validate_name(key_name) != CS_OK) {
fprintf(stderr, "Incorrect key name, can not have \"=\" or \"%c\"\n", SEPERATOR);
exit(EXIT_FAILURE);
}
res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
if (res != CS_OK) {
fprintf(stderr, "Can't find parent object of \"%s\"\n", path_pt);
exit(EXIT_FAILURE);
}
/* get the current key */
res = confdb_key_get (handle,
obj_handle,
key_name,
strlen(key_name),
old_key_value,
&old_key_value_len);
if (res == CS_OK) {
/* replace the current value */
res = confdb_key_replace (handle,
obj_handle,
key_name,
strlen(key_name),
old_key_value,
old_key_value_len,
key_value,
strlen(key_value));
if (res != CS_OK)
fprintf(stderr, "Failed to replace the key %s=%s. Error %d\n", key_name, key_value, res);
} else {
/* not there, create a new key */
res = confdb_key_create (handle,
obj_handle,
key_name,
strlen(key_name),
key_value,
strlen(key_value));
if (res != CS_OK)
fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res);
}
}
static void create_object(confdb_handle_t handle, char * name_pt)
{
char * obj_name_pt;
char * save_pt;
hdb_handle_t obj_handle;
hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
char tmp_name[OBJ_NAME_SIZE];
cs_error_t res;
strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
while (obj_name_pt != NULL) {
res = confdb_object_find_start(handle, parent_object_handle);
if (res != CS_OK) {
fprintf (stderr, "Could not start object_find %d\n", res);
exit (EXIT_FAILURE);
}
res = confdb_object_find(handle, parent_object_handle,
obj_name_pt, strlen (obj_name_pt), &obj_handle);
if (res != CS_OK) {
if (validate_name(obj_name_pt) != CS_OK) {
fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
obj_name_pt);
exit(EXIT_FAILURE);
}
res = confdb_object_create (handle,
parent_object_handle,
obj_name_pt,
strlen (obj_name_pt),
&obj_handle);
if (res != CS_OK)
fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
obj_name_pt, res);
}
parent_object_handle = obj_handle;
obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
}
}
/* Print "?" in place of any non-printable byte of OBJ. */
static void print_name (FILE *fp, const void *obj, size_t obj_len)
{
const char *p = obj;
size_t i;
for (i = 0; i < obj_len; i++) {
int c = *p++;
if (!isprint (c)) {
c = '?';
}
fputc (c, fp);
}
}
static void tail_key_changed(confdb_handle_t handle,
confdb_change_type_t change_type,
hdb_handle_t parent_object_handle,
hdb_handle_t object_handle,
const void *object_name_pt,
size_t object_name_len,
const void *key_name_pt,
size_t key_name_len,
const void *key_value_pt,
size_t key_value_len)
{
/* printf("key_changed> %.*s.%.*s=%.*s\n", */
fputs("key_changed> ", stdout);
print_name (stdout, object_name_pt, object_name_len);
fputs(".", stdout);
print_name (stdout, key_name_pt, key_name_len);
fputs("=", stdout);
print_name (stdout, key_value_pt, key_value_len);
fputs("\n", stdout);
}
static void tail_object_created(confdb_handle_t handle,
hdb_handle_t parent_object_handle,
hdb_handle_t object_handle,
const void *name_pt,
size_t name_len)
{
fputs("object_created>", stdout);
print_name(stdout, name_pt, name_len);
fputs("\n", stdout);
}
static void tail_object_deleted(confdb_handle_t handle,
hdb_handle_t parent_object_handle,
const void *name_pt,
size_t name_len)
{
fputs("object_deleted>", stdout);
print_name(stdout, name_pt, name_len);
fputs("\n", stdout);
}
static void listen_for_object_changes(confdb_handle_t handle)
{
int result;
fd_set read_fds;
int select_fd;
int quit = CS_FALSE;
FD_ZERO (&read_fds);
if (confdb_fd_get (handle, &select_fd) != CS_OK) {
printf ("can't get the confdb selector object.\n");
return;
}
printf ("Type \"q\" to finish\n");
do {
FD_SET (select_fd, &read_fds);
FD_SET (STDIN_FILENO, &read_fds);
result = select (select_fd + 1, &read_fds, 0, 0, 0);
if (result == -1) {
perror ("select\n");
}
if (FD_ISSET (STDIN_FILENO, &read_fds)) {
char inbuf[3];
if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
quit = CS_TRUE;
else if (strncmp(inbuf, "q", 1) == 0)
quit = CS_TRUE;
}
if (FD_ISSET (select_fd, &read_fds)) {
if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
exit(1);
}
} while (result && quit == CS_FALSE);
(void)confdb_stop_track_changes(handle);
}
static void track_object(confdb_handle_t handle, char * name_pt)
{
cs_error_t res;
hdb_handle_t obj_handle;
res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
if (res != CS_OK) {
fprintf (stderr, "Could not find object \"%s\". Error %d\n",
name_pt, res);
return;
}
res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
if (res != CS_OK) {
fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
name_pt, res);
return;
}
}
static void stop_tracking(confdb_handle_t handle)
{
cs_error_t res;
res = confdb_stop_track_changes (handle);
if (res != CS_OK) {
fprintf (stderr, "Could not stop tracking. Error %d\n", res);
return;
}
}
static void delete_object(confdb_handle_t handle, char * name_pt)
{
cs_error_t res;
hdb_handle_t obj_handle;
res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
if (res == CS_OK) {
res = confdb_object_destroy (handle, obj_handle);
if (res != CS_OK)
fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
} else {
char parent_name[OBJ_NAME_SIZE];
char key_name[OBJ_NAME_SIZE];
char key_value[OBJ_NAME_SIZE];
/* find the parent object */
get_parent_name(name_pt, parent_name);
get_key(name_pt, key_name, key_value);
res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
if (res != CS_OK) {
fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
exit (EXIT_FAILURE);
}
res = confdb_key_delete (handle,
obj_handle,
key_name,
strlen(key_name),
key_value,
strlen(key_value));
if (res != CS_OK)
fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
key_name, key_value, parent_name, res);
}
}
int main (int argc, char *argv[]) {
confdb_handle_t handle;
cs_error_t result;
int c;
action = ACTION_READ;
for (;;){
c = getopt (argc,argv,"hawcdtp:");
if (c==-1) {
break;
}
switch (c) {
case 'h':
return print_help();
break;
case 'a':
action = ACTION_PRINT_ALL;
break;
case 'p':
printf("%s:%d NOT Implemented yet.\n", __FUNCTION__, __LINE__);
return -1;
//return read_in_config_file();
break;
case 'c':
action = ACTION_CREATE;
break;
case 'd':
action = ACTION_DELETE;
break;
case 'w':
action = ACTION_WRITE;
break;
case 't':
action = ACTION_TRACK;
break;
default :
action = ACTION_READ;
break;
}
}
if (argc == 1) {
action = ACTION_PRINT_DEFAULT;
return print_all();
} else if (action == ACTION_PRINT_ALL) {
return print_all();
} else if (optind >= argc) {
fprintf(stderr, "Expected an object path after options\n");
exit(EXIT_FAILURE);
}
result = confdb_initialize (&handle, &callbacks);
if (result != CS_OK) {
fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
exit (EXIT_FAILURE);
}
while (optind < argc) {
switch (action) {
case ACTION_READ:
read_object(handle, argv[optind++]);
break;
case ACTION_WRITE:
write_key(handle, argv[optind++]);
break;
case ACTION_CREATE:
create_object(handle, argv[optind++]);
break;
case ACTION_DELETE:
delete_object(handle, argv[optind++]);
break;
case ACTION_TRACK:
track_object(handle, argv[optind++]);
break;
}
}
if (action == ACTION_TRACK) {
listen_for_object_changes(handle);
stop_tracking(handle);
}
result = confdb_finalize (handle);
if (result != CS_OK) {
fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
exit(EXIT_FAILURE);
}
return 0;
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Feb 26, 8:57 AM (1 d, 2 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1465226
Default Alt Text
(37 KB)

Event Timeline