Esempio n. 1
0
/*
 * 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;
}
Esempio n. 2
0
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);
}