Пример #1
0
/* --------------------------------
 * pcp_systemdb_info - get information of system DB
 *
 * return structure of system DB information on success, -1 otherwise
 * --------------------------------
 */
SystemDBInfo *
pcp_systemdb_info(void)
{
	char tos;
	char *buf = NULL;
	int wsize;
	int rsize;
	SystemDBInfo *systemdb_info = NULL;
	int offset = 0;

	if (pc == NULL)
	{
		if (debug) fprintf(stderr, "DEBUG: connection does not exist\n");
		errorcode = NOCONNERR;
		return NULL;
	}

	pcp_write(pc, "S", 1);
	wsize = htonl(sizeof(int));
	pcp_write(pc, &wsize, sizeof(int));
	if (pcp_flush(pc) < 0)
	{
		if (debug) fprintf(stderr, "DEBUG: could not send data to backend\n");
		return NULL;
	}
	if (debug) fprintf(stderr, "DEBUG: send: tos=\"S\", len=%d\n", ntohl(wsize));

	while (1) {
		if (pcp_read(pc, &tos, 1))
			return NULL;
		if (pcp_read(pc, &rsize, sizeof(int)))
			return NULL;
		rsize = ntohl(rsize);
		buf = (char *)malloc(rsize);
		if (buf == NULL)
		{
			errorcode = NOMEMERR;
			return NULL;
		}
		if (pcp_read(pc, buf, rsize - sizeof(int)))
		{
			free(buf);
			return NULL;
		}
		if (debug) fprintf(stderr, "DEBUG: recv: tos=\"%c\", len=%d, data=%s\n", tos, rsize, buf);

		if (tos == 'e')
		{
			if (debug) fprintf(stderr, "DEBUG: command failed. reason=%s\n", buf);
			free(buf);
			errorcode = BACKENDERR;
			return NULL;
		}
		else if (tos == 's')
		{
			char *index;

			if (strcmp(buf, "SystemDBInfo") == 0)
			{
				systemdb_info = (SystemDBInfo *)malloc(sizeof(SystemDBInfo));
				if (systemdb_info == NULL)
				{
					free(buf);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(buf, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->hostname = strdup(index);
				if (systemdb_info->hostname == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}				
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->port = atoi(index);
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->user = strdup(index);
				if (systemdb_info->user == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->password = strdup(index);
				if (systemdb_info->password == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->schema_name = strdup(index);
				if (systemdb_info->schema_name == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->database_name = strdup(index);
				if (systemdb_info->database_name == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->dist_def_num = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					systemdb_info->system_db_status = atoi(index);

				if (systemdb_info->dist_def_num > 0)
				{
					systemdb_info->dist_def_slot = NULL;
					systemdb_info->dist_def_slot = (DistDefInfo *)malloc(sizeof(DistDefInfo) * systemdb_info->dist_def_num);
					if (systemdb_info->dist_def_slot == NULL)
					{
						free(buf);
						free_systemdb_info(systemdb_info);
						errorcode = NOMEMERR;
						return NULL;
					}
				}
			}
			else if (strcmp(buf, "DistDefInfo") == 0)
			{
				DistDefInfo *dist_def_info = NULL;
				int i;

				dist_def_info = (DistDefInfo *)malloc(sizeof(DistDefInfo));
				if (dist_def_info == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(buf, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->dbname = strdup(index);
				if (dist_def_info->dbname == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->schema_name = strdup(index);
				if (dist_def_info->schema_name == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->table_name = strdup(index);
				if (dist_def_info->table_name == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->dist_key_col_name = strdup(index);
				if (dist_def_info->dist_key_col_name == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->col_num = atoi(index);

				dist_def_info->col_list = NULL;
				dist_def_info->col_list = (char **)malloc(sizeof(char *) * dist_def_info->col_num);
				if (dist_def_info->col_list == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}
				for (i = 0; i < dist_def_info->col_num; i++)
				{
					index = (char *) memchr(index, '\0', rsize) + 1;
					if (index != NULL)
						dist_def_info->col_list[i] = strdup(index);
					if (dist_def_info->col_list[i] == NULL)
					{
						free(buf);
						free_systemdb_info(systemdb_info);
						errorcode = NOMEMERR;
						return NULL;
					}
				}
			
				dist_def_info->type_list = NULL;
				dist_def_info->type_list = (char **)malloc(sizeof(char *) * dist_def_info->col_num);
				if (dist_def_info->type_list == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}
				for (i = 0; i < dist_def_info->col_num; i++)
				{
					index = (char *) memchr(index, '\0', rsize) + 1;
					if (index != NULL)
						dist_def_info->type_list[i] = strdup(index);
					if (dist_def_info->type_list[i] == NULL)
					{
						free(buf);
						free_systemdb_info(systemdb_info);
						errorcode = NOMEMERR;
						return NULL;
					}
				}

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					dist_def_info->dist_def_func = strdup(index);
				if (dist_def_info->dist_def_func == NULL)
				{
					free(buf);
					free_systemdb_info(systemdb_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				memcpy(&systemdb_info->dist_def_slot[offset++], dist_def_info, sizeof(DistDefInfo));
			}
			else if (strcmp(buf, "CommandComplete") == 0)
			{
				free(buf);
				return systemdb_info;
			}
			else
			{
				/* never reached */
			}
		}
	}
	
	free(buf);
	return NULL;
}
Пример #2
0
int
main(int argc, char **argv)
{
	long timeout;
	char host[MAX_DB_HOST_NAMELEN];
	int port;
	char user[MAX_USER_PASSWD_LEN];
	char pass[MAX_USER_PASSWD_LEN];
	SystemDBInfo *systemdb_info;
	int i, j;
	int ch;

	while ((ch = getopt(argc, argv, "hd")) != -1) {
		switch (ch) {
		case 'd':
			pcp_enable_debug();
			break;

		case 'h':
		case '?':
		default:
			usage();
			exit(0);
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 5)
	{
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}

	timeout = atol(argv[0]);
	if (timeout < 0) {
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}

	if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN)
	{
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}
	strcpy(host, argv[1]);

	port = atoi(argv[2]);
	if (port <= 1024 || port > 65535)
	{
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}

	if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN)
	{
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}
	strcpy(user, argv[3]);

	if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN)
	{
		errorcode = INVALERR;
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}
	strcpy(pass, argv[4]);

	if (pcp_connect(host, port, user, pass))
	{
		pcp_errorstr(errorcode);
		myexit(errorcode);
	}

	if ((systemdb_info = pcp_systemdb_info()) == NULL)
	{
		pcp_errorstr(errorcode);
		pcp_disconnect();
		myexit(errorcode);
	} else {
		printf("%s %d %s %s %s %s %d %d\n", 
			   systemdb_info->hostname,
			   systemdb_info->port,
			   systemdb_info->user,
			   systemdb_info->password[0] == '\0' ? "''" : systemdb_info->password,
			   systemdb_info->schema_name,
			   systemdb_info->database_name,
			   systemdb_info->dist_def_num,
			   systemdb_info->system_db_status);

		for (i = 0; i < systemdb_info->dist_def_num; i++)
		{
			DistDefInfo *ddi = &systemdb_info->dist_def_slot[i];

			printf("%s %s %s %s %d ",
				   ddi->dbname,
				   ddi->schema_name,
				   ddi->table_name,
				   ddi->dist_key_col_name,
				   ddi->col_num);

			for (j = 0; j < ddi->col_num; j++)
				printf("%s ", ddi->col_list[j]);

			for (j = 0; j < ddi->col_num; j++)
				printf("%s ", ddi->type_list[j]);
			
			printf("%s\n", ddi->dist_def_func);
		}
		
		free_systemdb_info(systemdb_info);
	}

	pcp_disconnect();

	return 0;
}