Esempio n. 1
0
/* --------------------------------
 * pcp_terminate_pgpool - send terminate packet
 *
 * return 0 on success, -1 otherwise
 * --------------------------------
 */
int
pcp_terminate_pgpool(char mode)
{
	int wsize;

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

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

	return 0;
}
Esempio n. 2
0
/*
 * Wrapper around pcp_flush which throws FATAL error when pcp_flush fails
 */
static void
do_pcp_flush(PCP_CONNECTION *frontend)
{
	if (pcp_flush(frontend) < 0)
		ereport(FATAL,
			(errmsg("failed to flush data to client"),
				 errdetail("pcp_flush failed with error : \"%s\"",strerror(errno))));
}
Esempio n. 3
0
File: pcp.c Progetto: ysd001/pgpool2
static int
PCPFlush(PCPConnInfo* pcpConn)
{
	int ret = pcp_flush(pcpConn->pcpConn);
	if (ret)
		pcp_internal_error(pcpConn,
						   "ERROR: sending data to backend failed with error \"%s\"",strerror(errno));
	return ret;
}
Esempio n. 4
0
int send_to_pcp_frontend(char* data, int len, bool flush)
{
	int ret;
	if (processType != PT_PCP_WORKER || pcp_frontend == NULL)
		return -1;
	ret = pcp_write(pcp_frontend, data, len);
	if (flush && !ret)
		ret = pcp_flush(pcp_frontend);
	return ret;
}
Esempio n. 5
0
/* --------------------------------
 * pcp_disconnect - close connection to pgpool
 * --------------------------------
 */
void
pcp_disconnect(void)
{
	int wsize;

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

	pcp_write(pc, "X", 1);
	wsize = htonl(sizeof(int));
	pcp_write(pc, &wsize, sizeof(int));
	if (pcp_flush(pc) < 0)
	{
		/* backend had closed connection already */
	}
	if (debug) fprintf(stderr, "DEBUG: send: tos=\"X\", len=%d\n", (int) sizeof(int));

	pcp_close(pc);
	pc = NULL;
}
Esempio n. 6
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;
}
Esempio n. 7
0
/* --------------------------------
 * pcp_process_info - get information of node pointed by given argument
 *
 * return structure of process information on success, -1 otherwise
 * --------------------------------
 */
ProcessInfo *
pcp_process_info(int pid, int *array_size)
{
	int wsize;
	char process_id[16];
	char tos;
	char *buf = NULL;
	int rsize;

	ProcessInfo *process_info = NULL;
	ConnectionInfo *conn_info = NULL;
	int ci_size = 0;
	int offset = 0;

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

	snprintf(process_id, sizeof(process_id), "%d", pid);

	pcp_write(pc, "P", 1);
	wsize = htonl(strlen(process_id)+1 + sizeof(int));
	pcp_write(pc, &wsize, sizeof(int));
	pcp_write(pc, process_id, strlen(process_id)+1);
	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=\"P\", 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 == 'p')
		{
			char *index;

			if (strcmp(buf, "ArraySize") == 0)
			{
				index = (char *) memchr(buf, '\0', rsize) + 1;
				if (index != NULL)
					ci_size = atoi(index);

				*array_size = ci_size;

				process_info = (ProcessInfo *)malloc(sizeof(ProcessInfo) * ci_size);
				if (process_info == NULL)
				{
					free(buf);
					errorcode = NOMEMERR;
					return NULL;
				}

				conn_info = (ConnectionInfo *)malloc(sizeof(ConnectionInfo) * ci_size);
				if  (conn_info == NULL)
				{
					free(buf);
					free(process_info);
					errorcode = NOMEMERR;
					return NULL;
				}

				continue;
			}
			else if (strcmp(buf, "ProcessInfo") == 0)
			{
				process_info[offset].connection_info = &conn_info[offset];

				index = (char *) memchr(buf, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].pid = atoi(index);
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					strcpy(process_info[offset].connection_info->database, index);
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					strcpy(process_info[offset].connection_info->user, index);
			
				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].start_time = atol(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->create_time = atol(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->major = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->minor = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->counter = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->backend_id = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->pid = atoi(index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					process_info[offset].connection_info->connected = atoi(index);

				offset++;
			}
			else if (strcmp(buf, "CommandComplete") == 0)
			{
				free(buf);
				return process_info;
			}
			else
			{
				/* never reached */
			}
		}
	}

	free(buf);
	return NULL;
}
Esempio n. 8
0
/* --------------------------------
 * pcp_node_count - get number of nodes currently connected to pgpool
 *
 * return array of pids on success, NULL otherwise
 * --------------------------------
 */
int *
pcp_process_count(int *pnum)
{
	char tos;
	char *buf = NULL;
	int wsize;
	int rsize;
	
	if (pc == NULL)
	{
		if (debug) fprintf(stderr, "DEBUG: connection does not exist\n");
		errorcode = NOCONNERR;
		return NULL;
	}

	pcp_write(pc, "N", 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=\"N\", len=%d\n", ntohl(wsize));

	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 == 'n')
	{
		if (strcmp(buf, "CommandComplete") == 0)
		{
			int process_count;
			int *process_list = NULL;
			char *index = NULL;
			int i;

			index = (char *) memchr(buf, '\0', rsize) + 1;
			process_count = atoi(index);

			process_list = (int *)malloc(sizeof(int) * process_count);
			if (process_list == NULL)
			{
				free(buf);
				errorcode = NOMEMERR;
				return NULL;
			}

			for (i = 0; i < process_count; i++)
			{
				index = (char *) memchr(index, '\0', rsize) + 1;
				process_list[i] = atoi(index);
			}

			*pnum = process_count;
			free(buf);
			return process_list;
		}
	}

	free(buf);
	return NULL;
}
Esempio n. 9
0
/* --------------------------------
 * pcp_node_info - get information of node pointed by given argument
 *
 * return structure of node information on success, -1 otherwise
 * --------------------------------
 */
BackendInfo *
pcp_node_info(int nid)
{
	int wsize;
	char node_id[16];
	char tos;
	char *buf = NULL;
	int rsize;

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

	snprintf(node_id, sizeof(node_id), "%d", nid);

	pcp_write(pc, "I", 1);
	wsize = htonl(strlen(node_id)+1 + sizeof(int));
	pcp_write(pc, &wsize, sizeof(int));
	pcp_write(pc, node_id, strlen(node_id)+1);
	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=\"I\", len=%d\n", ntohl(wsize));

	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);
		errorcode = BACKENDERR;
		free(buf);
		return NULL;
	}
	else if (tos == 'i')
	{
		if (strcmp(buf, "CommandComplete") == 0)
		{
			char *index = NULL;
			BackendInfo* backend_info = NULL;

			backend_info = (BackendInfo *)malloc(sizeof(BackendInfo));
			if (backend_info == NULL)
			{
				errorcode = NOMEMERR;
				free(buf);
				return NULL;
			}

 			index = (char *) memchr(buf, '\0', rsize) + 1;
			if (index != NULL)
				strcpy(backend_info->backend_hostname, index);

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

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

			index = (char *) memchr(index, '\0', rsize) + 1;
			if (index != NULL)
				backend_info->backend_weight = atof(index);

			free(buf);
			return backend_info;
		}
	}

	free(buf);
	return NULL;
}
Esempio n. 10
0
/* --------------------------------
 * pcp_node_count - get number of nodes currently connected to pgpool
 *
 * return array of node IDs on success, -1 otherwise
 * --------------------------------
 */
int
pcp_node_count(void)
{
	char tos;
	char *buf = NULL;
	int wsize;
	int rsize;
	char *index = NULL;

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

	pcp_write(pc, "L", 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 -1;
	}
	if (debug) fprintf(stderr, "DEBUG: send: tos=\"L\", len=%d\n", ntohl(wsize));

	if (pcp_read(pc, &tos, 1))
		return -1;
	if (pcp_read(pc, &rsize, sizeof(int)))
		return -1;
	rsize = ntohl(rsize);
	buf = (char *)malloc(rsize);
	if (buf == NULL)
	{
		errorcode = NOMEMERR;
		return -1;
	}
	if (pcp_read(pc, buf, rsize - sizeof(int)))
	{
		free(buf);
		return -1;
	}

	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);
		errorcode = BACKENDERR;
	}
	else if (tos == 'l')
	{
		if (strcmp(buf, "CommandComplete") == 0)
		{
			index = (char *) memchr(buf, '\0', rsize) + 1;
			if (index != NULL)
			{
				int ret = atoi(index);
				free(buf);
				return ret;
			}
		}
	}

	free(buf);

	return -1;
}
Esempio n. 11
0
/* --------------------------------
 * pcp_authorize - authenticate with pgpool using username and password
 *
 * return 0 on success, -1 otherwise
 * --------------------------------
 */
static int
pcp_authorize(char *username, char *password)
{
	char tos;
	char *buf = NULL;
	int wsize;
	int rsize;
	char salt[4];
	char encrypt_buf[(MD5_PASSWD_LEN+1)*2];
	char md5[MD5_PASSWD_LEN+1];

	/* request salt */
	pcp_write(pc, "M", 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 -1;
	}

	if (pcp_read(pc, &tos, 1))
		return -1;
	if (pcp_read(pc, &rsize, sizeof(int)))
		return -1;
	rsize = ntohl(rsize);
	buf = (char *)malloc(rsize);
	if (buf == NULL)
	{
		errorcode = NOMEMERR;
		return -1;
	}
	if (pcp_read(pc, buf, rsize - sizeof(int)))
		return -1;
	memcpy(salt, buf, 4);
	free(buf);

	/* encrypt password */
	pool_md5_hash(password, strlen(password), md5);
	md5[MD5_PASSWD_LEN] = '\0';

	pool_md5_encrypt(md5, username, strlen(username),
					 encrypt_buf + MD5_PASSWD_LEN + 1);
	encrypt_buf[(MD5_PASSWD_LEN+1)*2-1] = '\0';

	pool_md5_encrypt(encrypt_buf+MD5_PASSWD_LEN+1, salt, 4,
					 encrypt_buf);
	encrypt_buf[MD5_PASSWD_LEN] = '\0';

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

	if (pcp_read(pc, &tos, 1))
		return -1;
	if (pcp_read(pc, &rsize, sizeof(int)))
		return -1;
	rsize = ntohl(rsize);
	buf = (char *)malloc(rsize);
	if (buf == NULL)
	{
		errorcode = NOMEMERR;
		return -1;
	}
	if (pcp_read(pc, buf, rsize - sizeof(int)))
		return -1;
	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);
		errorcode = BACKENDERR;
	}
	else if (tos == 'r')
	{
		if (strcmp(buf, "AuthenticationOK") == 0)
		{
			free(buf);
			return 0;
		}

		if (debug) fprintf(stderr, "DEBUG: authentication failed. reason=%s\n", buf);
		errorcode = AUTHERR;
	}
	free(buf);

	return -1;
}
Esempio n. 12
0
static int _pcp_promote_node(int nid, bool gracefully)
{
	int wsize;
	char node_id[16];
	char tos;
	char *buf = NULL;
	int rsize;
	char *sendchar;

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

	snprintf(node_id, sizeof(node_id), "%d", nid);

	if (gracefully)
	  sendchar = "j";
	else
	  sendchar = "J";

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

	if (pcp_read(pc, &tos, 1))
		return -1;
	if (pcp_read(pc, &rsize, sizeof(int)))
		return -1;
	rsize = ntohl(rsize);
	buf = (char *)malloc(rsize);
	if (buf == NULL)
	{
		errorcode = NOMEMERR;
		return -1;
	}
	if (pcp_read(pc, buf, rsize - sizeof(int)))
	{
		free(buf);
		return -1;
	}
	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);
		errorcode = BACKENDERR;
	}
	else if (tos == 'd')
	{
		/* strcmp() for success message, or fail */
		if(strcmp(buf, "CommandComplete") == 0)
		{
			free(buf);
			return 0;
		}
	}

	free(buf);
	return -1;
}
Esempio n. 13
0
/* --------------------------------
 * pcp_pool_status - return setup parameters and status
 *
 * returns and array of POOL_REPORT_CONFIG, NULL otherwise
 * --------------------------------
 */
POOL_REPORT_CONFIG*
pcp_pool_status(int *array_size)
{
	char tos;
	char *buf = NULL;
	int wsize;
	int rsize;
	POOL_REPORT_CONFIG *status = NULL;
	int ci_size = 0;
	int offset = 0;

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

	pcp_write(pc, "B", 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 pcp_pool_status: send: tos=\"B\", 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 == 'b')
		{
			char *index;

			if (strcmp(buf, "ArraySize") == 0)
			{
				index = (char *) memchr(buf, '\0', rsize) + 1;
				ci_size = ntohl(*((int *)index));

				*array_size = ci_size;

				status = (POOL_REPORT_CONFIG *) malloc(ci_size * sizeof(POOL_REPORT_CONFIG));

				continue;
			}
			else if (strcmp(buf, "ProcessConfig") == 0)
			{
				index = (char *) memchr(buf, '\0', rsize) + 1;
				if (index != NULL)
					strcpy(status[offset].name, index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					strcpy(status[offset].value, index);

				index = (char *) memchr(index, '\0', rsize) + 1;
				if (index != NULL)
					strcpy(status[offset].desc, index);

				offset++;
			}
			else if (strcmp(buf, "CommandComplete") == 0)
			{
				free(buf);
				return status;
			}
			else
			{
				/* never reached */
			}
		}
	}

	free(buf);
	return NULL;
}
Esempio n. 14
0
/* --------------------------------
 * pcp_watchdog_info - get information of watchdog
 *
 * return structure of watchdog information on success, -1 otherwise
 * --------------------------------
 */
WdInfo *
pcp_watchdog_info(int nid)
{
	int wsize;
	char wd_index[16];
	char tos;
	char *buf = NULL;
	int rsize;

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

	snprintf(wd_index, sizeof(wd_index), "%d", nid);

	pcp_write(pc, "W", 1);
	wsize = htonl(strlen(wd_index)+1 + sizeof(int));
	pcp_write(pc, &wsize, sizeof(int));
	pcp_write(pc, wd_index, strlen(wd_index)+1);
	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=\"W\", len=%d\n", ntohl(wsize));

	if (pcp_read(pc, &tos, 1))
		return NULL;	
	if (pcp_read(pc, &rsize, sizeof(int)))
		return NULL;	
	rsize = ntohl(rsize);
	buf = (char *)palloc(rsize);
	if (buf == NULL)
	{
		errorcode = NOMEMERR;
		return NULL;
	}
	if (pcp_read(pc, buf, rsize - sizeof(int)))
	{
		pfree(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);
		errorcode = BACKENDERR;
		pfree(buf);
		return NULL;
	}
	else if (tos == 'w')
	{
		if (strcmp(buf, "CommandComplete") == 0)
		{
			char *index = NULL;
			WdInfo* watchdog_info = NULL;

			watchdog_info = (WdInfo *)palloc(sizeof(WdInfo));
			if (watchdog_info == NULL)
			{
				errorcode = NOMEMERR;
				pfree(buf);
				return NULL;
			}

			index = (char *) memchr(buf, '\0', rsize) + 1;
			if (index != NULL)
				strlcpy(watchdog_info->hostname, index, sizeof(watchdog_info->hostname));

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

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

			index = (char *) memchr(index, '\0', rsize) + 1;
			if (index != NULL)
				watchdog_info->status = atof(index);

			pfree(buf);
			return watchdog_info;
		}
	}

	pfree(buf);
	return NULL;
}