int main(int argc, char *argv[]) { confdb_handle_t handle; int result; hdb_handle_t cluster_handle; const char *clusterroot = "cluster"; char basedn[1024]; if (argc == 1) { fprintf(stderr, "usage: \n"); fprintf(stderr, " %s <dn> [<objdb root>]\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " eg: \n"); fprintf(stderr, " %s dc=mycompany,dc=com\n", argv[0]); fprintf(stderr, " %s dc=mycompany,dc=com rhcluster\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "objdb root defaults to 'cluster'\n"); fprintf(stderr, "\n"); return 0; } if (argc > 2) { clusterroot = argv[2]; } result = confdb_initialize (&handle, &callbacks); if (result != CS_OK) { printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result); exit (1); } /* Find the starting object ... this should be a param */ 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, clusterroot, strlen(clusterroot), &cluster_handle); if (result != CS_OK) { printf ("Could not object_find \"cluster\": %d\n", result); exit (1); } sprintf(basedn, "name=%s,%s", clusterroot, argv[1]); /* Print a header */ printf("# This file was generated by confdb2ldif, from an existing cluster configuration\n"); printf("#\n"); /* Print the configuration */ print_config_tree(handle, cluster_handle, clusterroot, basedn); result = confdb_finalize (handle); return (0); }
static int detect_protocol(void) { confdb_handle_t handle; hdb_handle_t totem_handle; char key_value[256]; size_t value_len; int rv, proto = -1; confdb_callbacks_t callbacks = { .confdb_key_change_notify_fn = NULL, .confdb_object_create_change_notify_fn = NULL, .confdb_object_delete_change_notify_fn = NULL }; rv = confdb_initialize(&handle, &callbacks); if (rv != CS_OK) { log_error("confdb_initialize error %d", rv); return -1; } rv = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE); if (rv != CS_OK) { log_error("confdb_object_find_start error %d", rv); goto out; } rv = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle); if (rv != CS_OK) { log_error("confdb_object_find error %d", rv); goto out; } rv = confdb_key_get(handle, totem_handle, "rrp_mode", strlen("rrp_mode"), key_value, &value_len); if (rv != CS_OK) { log_error("confdb_key_get error %d", rv); goto out; } key_value[value_len] = '\0'; log_debug("totem/rrp_mode = '%s'", key_value); if (!strcmp(key_value, "none")) proto = PROTO_TCP; else proto = PROTO_SCTP; out: confdb_finalize(handle); return proto; }
static confdb_handle_t config_find_init(confdb_handle_t config) { cs_error_t rc = CS_OK; confdb_handle_t local_handle = OBJECT_PARENT_HANDLE; rc = confdb_object_find_start(config, local_handle); if (rc == CS_OK) { return local_handle; } else { crm_err("Couldn't create search context: %d", rc); } return 0; }
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, sizeof (tmp_name)); tmp_name[sizeof (tmp_name) - 1] = '\0'; 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); } if (debug) printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt); 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); } }
/* * Caller should free the returned string */ static const char *get_quorum_type(void) { const char *qtype = NULL; int result; char buf[255]; size_t namelen = sizeof(buf); hdb_handle_t quorum_handle; confdb_handle_t confdb_handle; confdb_callbacks_t callbacks = { .confdb_key_change_notify_fn = NULL, .confdb_object_create_change_notify_fn = NULL, .confdb_object_delete_change_notify_fn = NULL }; if (confdb_initialize(&confdb_handle, &callbacks) != CS_OK) { errno = EINVAL; return NULL; } result = confdb_object_find_start(confdb_handle, OBJECT_PARENT_HANDLE); if (result != CS_OK) goto out; result = confdb_object_find(confdb_handle, OBJECT_PARENT_HANDLE, (void *)"quorum", strlen("quorum"), &quorum_handle); if (result != CS_OK) goto out; result = confdb_key_get(confdb_handle, quorum_handle, (void *)"provider", strlen("provider"), buf, &namelen); if (result != CS_OK) goto out; if (namelen >= sizeof(buf)) { namelen = sizeof(buf) - 1; } buf[namelen] = '\0'; /* If strdup returns NULL then we just assume no quorum provider ?? */ qtype = strdup(buf); out: confdb_finalize(confdb_handle); return qtype; }
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, sizeof (tmp_name)); tmp_name[sizeof (tmp_name) - 1] = '\0'; 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; }
/* * return 0 on success * return -1 on errors */ static int path_dive(confdb_handle_t handle, hdb_handle_t *query_handle, char *current_query, int tokens) { char *pos = NULL, *next = NULL; int i; hdb_handle_t new_obj_handle; confdb_value_types_t type; pos = current_query + 1; for (i = 1; i <= tokens; i++) { if (confdb_object_find_start(handle, *query_handle) != CS_OK) goto fail; next = pos + strlen(pos) + 1; if (!strstr(pos, "[")) { /* straight path diving */ if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; else { confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } } else { /* * /something[int]/ or /something[@foo="bar"]/ * start and end will identify [] * middle will point to the inside request */ char *start = NULL, *middle = NULL, *end = NULL; char *key_value = NULL; size_t valuelen; char data[PATH_MAX]; size_t datalen = 0; /* * those ones should be always good because * the tokenizer takes care of them */ start = strstr(pos, "["); if (!start) goto fail; end = strstr(pos, "]"); if (!end) goto fail; middle = start + 1; memset(start, 0, 1); memset(end, 0, 1); if (!strcmp(pos, "child::*")) { int val, j; val = atoi(middle); if (val < 1) goto fail; if (confdb_object_iter_start (handle, *query_handle) != CS_OK) goto fail; for (j = 1; j <= val; j++) { if (confdb_object_iter (handle, *query_handle, &new_obj_handle, data, &datalen) != CS_OK) goto fail; } confdb_object_iter_destroy(handle, *query_handle); confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } else if (!strstr(middle, "@")) { /* lookup something with index num = int */ int val, j; val = atoi(middle); if (val < 1) goto fail; for (j = 1; j <= val; j++) { if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; } confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } else { /* lookup something with obj foo = bar */ char *equal = NULL, *value = NULL, *tmp = NULL; int goout = 0; equal = strstr(middle, "="); if (!equal) goto fail; memset(equal, 0, 1); value = strstr(equal + 1, "\""); if (!value) goto fail; value = value + 1; tmp = strstr(value, "\""); if (!tmp) goto fail; memset(tmp, 0, 1); middle = strstr(middle, "@") + 1; if (!middle) goto fail; // middle points to foo // value to bar memset(data, 0, PATH_MAX); while (!goout) { if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; else { key_value = NULL; if (confdb_key_get_typed2 (handle, new_obj_handle, middle, (void **) &key_value, &valuelen, &type) == CS_OK) { if (!strcmp (key_value, value)) goout = 1; free(key_value); key_value = NULL; } } } free(key_value); key_value = NULL; confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } } pos = next; } return 0; fail: errno = EINVAL; return -1; }
/** * _ccs_get_xpathlite * @handle: * @connection_handle: * @query: * @list: 1 to operate in list fashion * * This function will allocate space for the value that is the result * of the given query. It is the user's responsibility to ensure that * the data returned is freed. * * Returns: char * to result or NULL in case of failure. */ char *_ccs_get_xpathlite(confdb_handle_t handle, hdb_handle_t connection_handle, const char *query, int list) { char current_query[PATH_MAX]; char *datapos, *rtn = NULL; char previous_query[PATH_MAX]; hdb_handle_t list_handle = 0; hdb_handle_t query_handle = 0; int prev = 0, is_oldlist = 0; int tokens, i; memset(current_query, 0, PATH_MAX); strncpy(current_query, query, PATH_MAX - 1); memset(previous_query, 0, PATH_MAX); datapos = current_query + 1; prev = get_previous_query(handle, connection_handle, previous_query, &list_handle); if (list && !prev && !strcmp(current_query, previous_query)) { query_handle = list_handle; is_oldlist = 1; } else { reset_iterator(handle, connection_handle); query_handle = OBJECT_PARENT_HANDLE; } if (confdb_object_find_start(handle, query_handle) != CS_OK) { errno = ENOENT; goto fail; } tokens = tokenizer(current_query); if (tokens < 1) goto fail; for (i = 1; i < tokens; i++) datapos = datapos + strlen(datapos) + 1; if (!is_oldlist) if (path_dive(handle, &query_handle, current_query, tokens - 1) < 0) /* path dive can mangle tokens */ goto fail; if (get_data (handle, connection_handle, query_handle, &list_handle, &rtn, datapos, list, is_oldlist) < 0) goto fail; if (list) if (set_previous_query (handle, connection_handle, (char *)query, list_handle)) goto fail; return rtn; fail: return NULL; }
static int get_data(confdb_handle_t handle, hdb_handle_t connection_handle, hdb_handle_t query_handle, hdb_handle_t *list_handle, char **rtn, char *curpos, int list, int is_oldlist) { int cmp; char data[PATH_MAX]; char *resval; char *keyval; hdb_handle_t new_obj_handle; unsigned int value = 0; confdb_value_types_t type; size_t datalen = 0, keyvallen = PATH_MAX; memset(data, 0, PATH_MAX); // we need to handle child::*[int value] in non list mode. cmp = strcmp(curpos, "child::*"); if (cmp >= 0) { char *start = NULL, *end = NULL; // a pure child::* request should come down as list if (!cmp && !list) goto fail; if (confdb_object_iter_start(handle, query_handle) != CS_OK) goto fail; if (!is_oldlist) *list_handle = query_handle; if (cmp) { start = strstr(curpos, "["); if (!start) goto fail; start = start + 1; end = strstr(start, "]"); if (!end) goto fail; memset(end, 0, 1); value = atoi(start); if (value <= 0) goto fail; } else { if (confdb_key_increment (handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &value) != CS_OK) value = 1; } while (value != 0) { memset(data, 0, PATH_MAX); if (confdb_object_iter (handle, query_handle, &new_obj_handle, data, &datalen) != CS_OK) { reset_iterator(handle, connection_handle); goto fail; } value--; } resval = malloc(datalen + 2); if (!resval) goto fail; snprintf(resval, datalen + 2, "%s=", data); *rtn = resval; } else if (!strncmp(curpos, "@*", strlen("@*"))) { // this query makes sense only if we are in list mode if (!list) goto fail; if (confdb_key_iter_start(handle, query_handle) != CS_OK) goto fail; *list_handle = query_handle; if (confdb_key_increment (handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &value) != CS_OK) value = 1; while (value != 0) { memset(data, 0, PATH_MAX); keyval = NULL; if (confdb_key_iter_typed2 (handle, query_handle, data, (void **)&keyval, &keyvallen, &type) != CS_OK) { reset_iterator(handle, connection_handle); goto fail; } value--; if (value != 0) { free(keyval); keyval = NULL; } } datalen = strlen(data); resval = malloc(datalen + keyvallen + 2); if (!resval) goto fail; snprintf(resval, datalen + keyvallen + 2, "%s=%s", data, keyval); *rtn = resval; free(keyval); } else { /* pure data request */ char *query; // this query doesn't make sense in list mode if (list) goto fail; if (confdb_object_find_start(handle, query_handle) != CS_OK) goto fail; query = strstr(curpos, "@"); if (!query) goto fail; query = query + 1; keyval = NULL; if (confdb_key_get_typed2 (handle, query_handle, query, (void **)&keyval, &keyvallen, &type) != CS_OK) goto fail; *rtn = keyval; } return 0; fail: errno = EINVAL; return -1; }
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); }
static void create_object_key(confdb_handle_t handle, char *name_pt) { char * obj_name_pt; char * new_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; char parent_name[OBJ_NAME_SIZE]; char key_name[OBJ_NAME_SIZE]; char key_value[OBJ_NAME_SIZE]; get_parent_name(name_pt, parent_name); get_key(name_pt, key_name, key_value); strncpy (tmp_name, parent_name, sizeof (tmp_name)); tmp_name[sizeof (tmp_name) - 1] = '\0'; obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt); /* * Create parent object tree */ 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); } new_obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt); res = confdb_object_find(handle, parent_object_handle, obj_name_pt, strlen (obj_name_pt), &obj_handle); if (res != CS_OK || new_obj_name_pt == NULL) { if (validate_name(obj_name_pt) != CS_OK) { fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n", obj_name_pt); exit(EXIT_FAILURE); } if (debug) printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt); 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 = new_obj_name_pt; } /* * Create key */ res = confdb_key_create_typed (handle, obj_handle, key_name, key_value, strlen(key_value), CONFDB_VALUETYPE_STRING); if (res != CS_OK) { fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res); } }
static cs_error_t sam_confdb_register (void) { const char *obj_name; cs_error_t err; confdb_handle_t confdb_handle; hdb_handle_t resource_handle, process_handle, pid_handle, obj_handle; hdb_handle_t *res_handle; char tmp_obj[PATH_MAX]; int i; if ((err = confdb_initialize (&confdb_handle, NULL)) != CS_OK) { return (err); } for (i = 0; i < 3; i++) { switch (i) { case 0: obj_name = "resources"; obj_handle = OBJECT_PARENT_HANDLE; res_handle = &resource_handle; break; case 1: obj_name = "process"; obj_handle = resource_handle; res_handle = &process_handle; break; case 2: if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, getpid ()) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", getpid ()); } obj_name = tmp_obj; obj_handle = process_handle; res_handle = &pid_handle; break; } if ((err = confdb_object_find_start (confdb_handle, obj_handle)) != CS_OK) { goto finalize_error; } if ((err = confdb_object_find (confdb_handle, obj_handle, obj_name, strlen (obj_name), res_handle)) != CS_OK) { if (err == CONFDB_ERR_ACCESS) { /* * Try to create object */ if ((err = confdb_object_create (confdb_handle, obj_handle, obj_name, strlen (obj_name), res_handle)) != CS_OK) { goto finalize_error; } } else { goto finalize_error; } } else { if ((err = confdb_object_find_destroy (confdb_handle, obj_handle)) != CS_OK) { goto finalize_error; } } } sam_internal_data.confdb_pid_handle = pid_handle; sam_internal_data.confdb_handle = confdb_handle; if ((err = sam_confdb_update_key (SAM_CONFDB_KEY_RECOVERY, NULL)) != CS_OK) { goto destroy_finalize_error; } if ((err = sam_confdb_update_key (SAM_CONFDB_KEY_HC_PERIOD, NULL)) != CS_OK) { goto destroy_finalize_error; } return (CS_OK); destroy_finalize_error: sam_confdb_destroy_pid_obj (); finalize_error: confdb_finalize (confdb_handle); return (err); }
/* * Test confdb integration + quit policy */ static int test8 (pid_t pid, pid_t old_pid, int test_n) { confdb_handle_t cdb_handle; cs_error_t err; hdb_handle_t res_handle, proc_handle, pid_handle; size_t value_len; uint64_t tstamp1, tstamp2; int32_t msec_diff; char key_value[256]; unsigned int instance_id; char tmp_obj[PATH_MAX]; confdb_value_types_t cdbtype; err = confdb_initialize (&cdb_handle, NULL); if (err != CS_OK) { printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err); return (1); } printf ("%s test %d\n", __FUNCTION__, test_n); if (test_n == 2) { /* * Object should not exist */ printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__); err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err == CS_OK) { printf ("Could find object \"%s\": %d.\n", tmp_obj, err); return (2); } } if (test_n == 1 || test_n == 2) { printf ("%s: initialize\n", __FUNCTION__); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CONFDB); if (err != CS_OK) { fprintf (stderr, "Can't initialize SAM API. Error %d\n", err); return 2; } printf ("%s: register\n", __FUNCTION__); err = sam_register (&instance_id); if (err != CS_OK) { fprintf (stderr, "Can't register. Error %d\n", err); return 2; } err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"recovery\" key: %d.\n", err); return (2); } if (value_len != strlen ("quit") || memcmp (key_value, "quit", value_len) != 0) { printf ("Recovery key \"%s\" is not \"watchdog\".\n", key_value); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: start\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) { printf ("State key is not \"running\".\n"); return (2); } printf ("%s iid %d: stop\n", __FUNCTION__, instance_id); err = sam_stop (); if (err != CS_OK) { fprintf (stderr, "Can't stop hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id); sleep (5); err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) { printf ("State key is not \"running\".\n"); return (2); } if (test_n == 2) { printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id); sleep (5); return (2); } else { printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id); err = sam_hc_send (); if (err != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", err); return 2; } err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp1, &value_len, &cdbtype); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id); sleep (1); err = sam_hc_send (); if (err != CS_OK) { fprintf (stderr, "Can't send hc. Error %d\n", err); return 2; } sleep (1); err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp2, &value_len, &cdbtype); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC; if (msec_diff < 500 || msec_diff > 2000) { printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff); return (2); } printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id); err = sam_stop (); if (err != CS_OK) { fprintf (stderr, "Can't stop hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id); return (0); } } if (test_n == 3) { printf ("%s Testing if status is failed\n", __FUNCTION__); /* * Previous should be FAILED */ err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) { printf ("State key is not \"failed\".\n"); return (2); } return (0); } return (2); }
/* * Test quorum */ static int test7 (void) { confdb_handle_t cdb_handle; cs_error_t err; hdb_handle_t quorum_handle; size_t value_len; char key_value[256]; unsigned int instance_id; pthread_t kill_thread; err = confdb_initialize (&cdb_handle, NULL); if (err != CS_OK) { printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err); return (1); } err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d. Test skipped\n", err); return (1); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &quorum_handle); if (err != CS_OK) { printf ("Could not object_find \"quorum\": %d. Test skipped\n", err); return (1); } err = confdb_key_get(cdb_handle, quorum_handle, "provider", strlen("provider"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"provider\" key: %d. Test skipped\n", err); return (1); } if (!(value_len - 1 == strlen ("testquorum") && memcmp (key_value, "testquorum", value_len - 1) == 0)) { printf ("Provider is not testquorum. Test skipped\n"); return (1); } /* * Set to not quorate */ err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0")); if (err != CS_OK) { printf ("Can't create confdb key. Error %d\n", err); return (2); } printf ("%s: initialize\n", __FUNCTION__); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART); if (err != CS_OK) { fprintf (stderr, "Can't initialize SAM API. Error %d\n", err); return 2; } printf ("%s: register\n", __FUNCTION__); err = sam_register (&instance_id); if (err != CS_OK) { fprintf (stderr, "Can't register. Error %d\n", err); return 2; } if (instance_id == 1) { /* * Sam start should block forever, but 10s for us should be enough */ pthread_create (&kill_thread, NULL, test7_thread, NULL); printf ("%s iid %d: start - should block forever (waiting 5s)\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id); return (2); } if (instance_id == 2) { /* * Set to quorate */ err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "1", strlen("1")); if (err != CS_OK) { printf ("Can't create confdb key. Error %d\n", err); return (2); } printf ("%s iid %d: start\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } /* * Set corosync unquorate */ err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0")); if (err != CS_OK) { printf ("Can't create confdb key. Error %d\n", err); return (2); } printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id); sleep (3); printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id); return (2); } if (instance_id == 3) { return (0); } return (2); }
/* * Test confdb integration + restart policy */ static int test9 (pid_t pid, pid_t old_pid, int test_n) { confdb_handle_t cdb_handle; cs_error_t err; hdb_handle_t res_handle, proc_handle, pid_handle; size_t value_len; char key_value[256]; unsigned int instance_id; char tmp_obj[PATH_MAX]; err = confdb_initialize (&cdb_handle, NULL); if (err != CS_OK) { printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err); return (1); } printf ("%s test %d\n", __FUNCTION__, test_n); if (test_n == 1) { printf ("%s: initialize\n", __FUNCTION__); err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CONFDB); if (err != CS_OK) { fprintf (stderr, "Can't initialize SAM API. Error %d\n", err); return 2; } printf ("%s: register\n", __FUNCTION__); err = sam_register (&instance_id); if (err != CS_OK) { fprintf (stderr, "Can't register. Error %d\n", err); return 2; } printf ("%s: iid %d\n", __FUNCTION__, instance_id); if (instance_id < 3) { err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"recovery\" key: %d.\n", err); return (2); } if (value_len != strlen ("restart") || memcmp (key_value, "restart", value_len) != 0) { printf ("Recovery key \"%s\" is not \"restart\".\n", key_value); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) { printf ("State key is not \"stopped\".\n"); return (2); } printf ("%s iid %d: start\n", __FUNCTION__, instance_id); err = sam_start (); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) { printf ("State key is not \"running\".\n"); return (2); } printf ("%s iid %d: waiting for kill\n", __FUNCTION__, instance_id); sleep (10); return (2); } if (instance_id == 3) { printf ("%s iid %d: mark failed\n", __FUNCTION__, instance_id); if (err != CS_OK) { fprintf (stderr, "Can't start hc. Error %d\n", err); return 2; } err = sam_mark_failed (); if (err != CS_OK) { fprintf (stderr, "Can't mark failed. Error %d\n", err); return 2; } sleep (10); return (2); } return (2); } if (test_n == 2) { printf ("%s Testing if status is failed\n", __FUNCTION__); /* * Previous should be FAILED */ err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle); if (err != CS_OK) { printf ("Could not object_find \"resources\": %d.\n", err); return (2); } err = confdb_object_find_start(cdb_handle, res_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle); if (err != CS_OK) { printf ("Could not object_find \"process\": %d.\n", err); return (2); } if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) { snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid); } err = confdb_object_find_start(cdb_handle, proc_handle); if (err != CS_OK) { printf ("Could not start object_find %d.\n", err); return (2); } err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle); if (err != CS_OK) { printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err); return (2); } err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len); if (err != CS_OK) { printf ("Could not get \"state\" key: %d.\n", err); return (2); } if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) { printf ("State key is not \"failed\".\n"); return (2); } return (0); } return (2); }