コード例 #1
0
ファイル: htxd_daemon.c プロジェクト: open-power/HTX
void *htxd_command_thraead_handler(void *ptr_socket)
{

	char *		command_buffer		= NULL;
	htxd_command	command_object;
	int		command_return_code	= 0;
	int		client_socket;
	char *		command_result		= NULL;
	char		trace_string[256];
	int		result			= 0;


	client_socket = *( (int *)ptr_socket);	
	free(ptr_socket);
	/* receive the incoming command */
	HTXD_TRACE(LOG_OFF, "daemon receiving command");
	command_buffer = htxd_receive_command(client_socket);
	if(command_buffer == NULL)
	{
		sprintf(trace_string, "Error : htxd_receive_command: returned NULL");
		HTXD_TRACE(LOG_ON, trace_string);
		return NULL;
	}

	HTXD_TRACE(LOG_OFF, "command received start<<");
	HTXD_TRACE(LOG_OFF, command_buffer);
	HTXD_TRACE(LOG_OFF, ">> command received end");

	if(htxd_verify_command_buffer_format(command_buffer) != 0) {
		HTXD_TRACE(LOG_ON, "htxd_start_daemon: htxd_verify_command_buffer_format failed for the command buffer");
		HTXD_TRACE(LOG_ON, "further processing of the command will be ignored");

		sprintf(trace_string, "Error while receiving command, errno <%d>", errno);
		result = htxd_send_response(client_socket, trace_string, 1, -1);
		if(result == -1) {
			HTXD_TRACE(LOG_ON, "1. htxd_send_response() is returned -1, ignoring further processing of the command");
		}
		HTXD_TRACE(LOG_ON, trace_string);
		close(client_socket);
		if(command_buffer != NULL) {
			free(command_buffer);
			command_buffer = NULL;
		} 
		
		return NULL;
	} 

	htxd_update_command_object(command_buffer, &command_object);

	if(command_buffer != NULL) {
		free(command_buffer);
		command_buffer = NULL;
	} 

	/* process the received command */
	HTXD_TRACE(LOG_OFF, "daemon start processing command");
	command_return_code = htxd_process_command(&command_result, &command_object);

	/* handling if command did not generate result buffer */		
	if(command_result == NULL) {
		command_result = malloc(HTX_ERR_MESSAGE_LENGTH);
		if(command_result == NULL) {
			sprintf(trace_string, "Error : malloc(%d) failed with errno = <%d> while allocating error message", HTX_ERR_MESSAGE_LENGTH, errno);
			HTXD_TRACE(LOG_ON, trace_string); 
			exit(1);
		}
		strcpy(command_result, "No result is generated by the command");
	}

	HTXD_TRACE(LOG_OFF, "command result start<<");
	HTXD_TRACE(LOG_OFF, command_result);
	HTXD_TRACE(LOG_OFF, ">> command result end");

	/* send back command result to client */
	HTXD_TRACE(LOG_OFF, "daemon sending the result to client");
	result = htxd_send_response(client_socket, command_result, command_object.command_type, command_return_code);
	if(result == -1)
	{
		sprintf(trace_string, "Error : htxd_send_response returned with -1");
		HTXD_TRACE(LOG_ON, trace_string); 
		return NULL;
	}
	
	if(command_result != 0) {
		free(command_result);
	}

	close(client_socket);

	htxd_exit_command_thread();	

	return NULL;
}
コード例 #2
0
ファイル: htxd_daemon.c プロジェクト: mikey/HTX
/* daemon live here, receives command, process it, send back result */
int htxd_start_daemon(htxd *htxd_instance)
{

	int					result				= 0;
	int					socket_fd;
	struct sockaddr_in	local_address;
	struct sockaddr_in	client_address;
	socklen_t			address_length;
	int					new_fd;
	char *				command_buffer		= NULL;
	char *				command_result		= NULL;
	int					command_return_code	= 0;
	char			trace_string[256];


	HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_start_daemon");

	init_option_list();
	htxd_autostart(htxd_instance);  /* try autostart if find the autostart flag file */

	socket_fd = htxd_create_socket();

	result = htxd_set_socket_option(socket_fd);

	local_address.sin_family = AF_INET;
	local_address.sin_port = htons (htxd_instance->port_number);
	local_address.sin_addr.s_addr = INADDR_ANY;
	memset (&(local_address.sin_zero), '\0', 8);

	result = htxd_bind_socket(socket_fd, &local_address, htxd_instance->port_number);

	result = htxd_listen_socket(socket_fd);

	HTXD_TRACE(LOG_ON, "starting daemon main loop");
	do  /* this loop make the daemon live */
	{
		do  /* this loop listens for incomming messages */
		{
			HTXD_TRACE(LOG_OFF, "daemon wating for command");
			result = htxd_select(socket_fd);
			if(htxd_shutdown_flag == TRUE) {
				break;
			}
		}while( (result == -1) && (errno == EINTR) );
		if(htxd_shutdown_flag == TRUE) {
			break;
		}

		new_fd = htxd_accept_connection(socket_fd, &client_address, &address_length);
		if(new_fd == -1)
		{
			if(htxd_shutdown_flag == TRUE) {
				break;
			}
			HTXD_TRACE(LOG_OFF, "select time out");
			continue;
		}

		HTXD_TRACE(LOG_OFF, "found a command for receiving");
	
		if(htxd_is_profile_initialized(htxd_instance) != TRUE) {
			HTXD_TRACE(LOG_ON, "initialize HTX profile details");
			htxd_init_profile(&(htxd_instance->p_profile));
			/* htxd_display_profile(htxd_instance->p_profile);  */ /* To DEBUG */
			register_signal_handlers();	
		}

		/* receive the incomming command */
		HTXD_TRACE(LOG_OFF, "daemon receiving command");
		command_buffer = htxd_receive_command(new_fd);
		if(command_buffer == NULL)
		{
			return -1;
		}

		HTXD_TRACE(LOG_OFF, "command received start<<");
		HTXD_TRACE(LOG_OFF, command_buffer);
		HTXD_TRACE(LOG_OFF, ">> command received end");

		htxd_update_command_object(command_buffer);

		if(command_buffer != NULL) {
			free(command_buffer);
			command_buffer = NULL;
		} 

		/* process the received command */
		HTXD_TRACE(LOG_OFF, "daemon start processing command");
		command_return_code = htxd_process_command(&command_result);

		/* handling if command did not generate result buffer */		
		if(command_result == NULL) {
			command_result = malloc(HTX_ERR_MESSAGE_LENGTH);
			if(command_result == NULL) {
				sprintf(trace_string, "Error : malloc(%d) failed with errno = <%d> while allocating error message", HTX_ERR_MESSAGE_LENGTH, errno);
				HTXD_TRACE(LOG_ON, trace_string); 
				exit(1);
			}
			strcpy(command_result, "No result is generated by the command");
		}

		HTXD_TRACE(LOG_OFF, "command result start<<");
		HTXD_TRACE(LOG_OFF, command_result);
		HTXD_TRACE(LOG_OFF, ">> command result end");

		/* send back command result to client */
		HTXD_TRACE(LOG_OFF, "daemon sending the result to client");
		result = htxd_send_response(new_fd, command_result, command_return_code);
		if(result == -1)
		{
			return result;
		}
		
		if(command_result != 0) {
			free(command_result);
		}
		close(new_fd);

	} while(htxd_shutdown_flag == FALSE);

	if(htxd_get_ecg_list_length(htxd_instance->p_ecg_manager) > 0)
	{
		/* shutdown all running ecgs and return */	
		// htxd_shutdown_all_running_ecgs();
	}

	htxd_shutdown_all_mdt();

	HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_start_daemon");

	return result;
}