Esempio n. 1
0
/* 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			client_socket_fd;
	htxd_thread		command_thread;



	HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_start_daemon");

	init_option_list();
	htxd_launch_autostart();

	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 */
	}

	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 */
	{
		if(htxd_shutdown_flag == TRUE) {
			break;
		}

		client_socket_fd = htxd_accept_connection(socket_fd, &client_address, &address_length);
		if(client_socket_fd == -1)
		{
			if(htxd_shutdown_flag == TRUE) {
				break;
			}
			HTXD_TRACE(LOG_ON, "htxd_accept_connection returned -1, ignoring and continue...");
			continue;
		}

		HTXD_TRACE(LOG_OFF, "found a command for receiving");
	
		memset(&command_thread, 0, sizeof(htxd_thread));
		command_thread.thread_function = htxd_command_thraead_handler;
		command_thread.thread_data = malloc(sizeof(int));
		(*((int *)command_thread.thread_data)) = client_socket_fd;
		command_thread.thread_stack_size = 10000000;
	
		htxd_create_command_thread(&command_thread);	


	} while(htxd_shutdown_flag == FALSE);

	htxd_shutdown_all_mdt();

	HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_start_daemon");

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