Ejemplo n.º 1
0
static bool Execute_and_report_output (const char* executable, const char* client_host_name, const char* client_IP,
									   const uint16_t client_port, const char* event, TEventType event_type, const size_t payload_len,
									   const char** payload, const int *fd_to_close_in_child, const int fd_to_close_in_child_count)
{

	/* command <client IP> <client port> <event type> <event> <payload length> <payload 1> ...<payload n> */

	size_t i = 0;
	size_t len = 0;
	size_t buffer_len = 8 + payload_len;

	size_t line_buffer_len = 0;
	char* line_buffer = NULL;
	size_t line_len = 0;
	int linenr;
	bool eof_found;
	FILE *pipe_read, *pipe_write;

	char** argv = NULL;
	pid_t childpid = 0;

	int cmdres = 255;
	bool res = false;

	bool close_received = false;
	bool error_executing = false;
	bool timer_failed = false;
	bool message_line = false;

	/* command <client host name> <client IP> <client port> <event type> <event> <payload length> <payload 1> ...<payload n> */

	if (!Add_string_to_string_array (&argv, &len, &buffer_len, executable))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_string_to_string_array (&argv, &len, &buffer_len, client_host_name))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_string_to_string_array (&argv, &len, &buffer_len, client_IP))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_luint_to_string_array (&argv, &len, &buffer_len, client_port))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_luint_to_string_array (&argv, &len, &buffer_len, event_type))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_string_to_string_array (&argv, &len, &buffer_len, event))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!Add_luint_to_string_array (&argv, &len, &buffer_len, payload_len))
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	for (i = 0; i < payload_len; i++)
	{
		if (!Add_string_to_string_array (&argv, &len, &buffer_len, payload[i]))
		{
			Free_string_array (&argv, &len, &buffer_len);
			return false;
		}
	}

	errno = 0;

	line_buffer_len = LINE_BUFFER_SIZE;

	line_buffer = malloc (LINE_BUFFER_SIZE);

	bzero (line_buffer, LINE_BUFFER_SIZE);

	if (line_buffer == NULL)
	{
		Free_string_array (&argv, &len, &buffer_len);
		return false;
	}

	if (!popen_ext (argv[0], argv, true, &pipe_read, &pipe_write, NULL, false, false, false,
					&childpid, fd_to_close_in_child, fd_to_close_in_child_count))
	{
		Free_string_array (&argv, &len, &buffer_len);
		Free_string (&line_buffer);
		return false;
	}

	/* Set the buffering to lines for the write pipe end. */
	setlinebuf (pipe_write);

	linenr = 1;

	while (!close_received)
	{
		errno = 0;

		if (!Set_alarm_timer (EXECUTE_READ_WRITE_LINE_TIME_OUT))
		{
			timer_failed = true;
			break;
		}

		error_executing = (!Read_line (pipe_read, &line_buffer,
												&line_buffer_len, &line_len,
												&eof_found, false, false));

		if (!Set_alarm_timer (0.0))
		{
			timer_failed = true;
			break;
		}

		if (error_executing)
			break;

		if ( ( (line_len == strlen (CLOSE_MY_MSG)) &&
				(strncmp (line_buffer, CLOSE_MY_MSG, line_len) == 0)))
		{
			/* Child indicates termination. */
			close_received = true;
		}


		if (!close_received)
		{
			if (linenr == 1)
			{
				/* HELLO message expected from child. */

				if ( (line_len != strlen (HELLO_MY_MSG)) ||
						(strncmp (line_buffer, HELLO_MY_MSG, line_len) != 0))
				{
					/* Invalid HELLO message. */

					error_executing = true;
					break;
				}
			}
			else
			{
				/* PAYLOAD message expected from child, requesting a payload line. */

				if ( (line_len == strlen (PAYLOAD_MY_MSG)) &&
						(strncmp (line_buffer, PAYLOAD_MY_MSG, line_len) == 0) &&
						( (size_t) (linenr - 2) < payload_len))
				{
					if (!Set_alarm_timer (EXECUTE_READ_WRITE_LINE_TIME_OUT))
					{
						timer_failed = true;
						break;
					}

					if (!Write_line (pipe_write, payload[linenr - 2]))
					{
						error_executing = true;
						break;
					}

					if (!Set_alarm_timer (0.0))
					{
						timer_failed = true;
						break;
					}
				}
				else if (!message_line)
				{
					/* Child process sended an extra info line. */

					message_line = true;
				}
				else
				{
					/* Invalid message or no more payload lines. */

					error_executing = true;
					break;
				}
			}
		}

		Vlogit (tll_debug,
				LOG_COMMAND_OUTPUT,
				LOGLEVELNAME (tll_debug),
				PROCESS_P_C (g_daemonized, g_processor),
				getpid(), linenr, line_buffer);

		++linenr;

		if (!Set_alarm_timer (EXECUTE_READ_WRITE_LINE_TIME_OUT))
		{
			timer_failed = true;
			break;
		}
	}

	if (!Set_alarm_timer (0.0))
		timer_failed = true;

	Free_string (&line_buffer);

	if (error_executing || timer_failed)
	{
		/* The child process was executed but we failed to set a timer or
		to read from or to write to the process. */
		error_executing = true;

		/* Ask the child process to terminate. */
		kill (childpid, SIGTERM);
	}

	timer_failed = (!Set_alarm_timer (EXECUTE_WAIT_TIME_OUT));

	res = ( (!timer_failed) && pclose_ext (&pipe_read, &pipe_write, NULL, childpid, true, &cmdres));

	Set_alarm_timer (0.0);

	if (!res)
	{
		kill (childpid, SIGKILL);

		usleep (100000);

		waitpid (childpid, NULL, WNOHANG);

		if (pipe_read != NULL)
			fclose (pipe_read);

		if (pipe_write != NULL)
			fclose (pipe_write);
	}

	Free_string_array (&argv, &len, &buffer_len);

	return (!error_executing && res && (cmdres == 0));
}
Ejemplo n.º 2
0
int Process_new_client_connection (const int socket_fd, const char* client_host_name, const char* client_IP, const uint16_t client_port,
								   const char* password, const char* executable)
{
	bool auxres;
	char* line_buffer = NULL;
	size_t line_buffer_len = 0;
	size_t line_len = 0;
	FILE *input_lines, *output_lines;
	char cookie[5];
	char* password_latin;
	char hash[HASH_LEN + 1];
	bool eof_found;

	bool close_requested;
	TEventType event_type;

	char* event;
	char** payload;
	size_t payload_len, payload_buffer_len;

	bool isTCPEvents;
	size_t auxlen;


	errno = 0;


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Promoting socket to FILE...");

	errno = 0;

	/* Promote socket. */
	if ( ( (input_lines = fdopen (socket_fd, "r")) == NULL) ||
			( (output_lines = fdopen (socket_fd, "w")) == NULL))
		return ERROR_PROMOTING_SOCKET_TO_FILE;

	/* Set the buffering to lines for the output file. */
	setlinebuf (output_lines);

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success promoting socket to FILE.");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Trying to read the \"Magic word\".");

	errno = 0;

	if (!Read_line (input_lines, &line_buffer, &line_buffer_len,
							 &line_len, &eof_found, true, false))
		return ERROR_READING_MAGIC_WORD;

	errno = 0;

	auxres =
		( (line_len == strlen (MAGIC_WORD)) &&
		  (strncmp (line_buffer, MAGIC_WORD, line_len) == 0));

	Free_string (&line_buffer);

	if (!auxres)
		return ERROR_BAD_MAGIC_WORD;

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success reading the \"Magic word\".");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Generating and sending cookie for password...");

	errno = 0;

	if (!Generate_cookie (cookie, sizeof (cookie)))
		return ERROR_GENERATING_PASSWORD_COOKIE;

	if (!Write_line (output_lines,  cookie))
		return ERROR_WRITING_COOKIE;

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success generating and sending cookie for password.");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Calculating hash...");

	errno = 0;

	if (!Convert_between_latin15_and_current_locale (password, &password_latin, false))
		return ERROR_CALCULATING_HASH;

	errno = 0;

	auxres = Hash_cookied_password (password_latin, cookie, hash, sizeof (hash));

	Free_string (&password_latin);

	if (!auxres)
		return ERROR_CALCULATING_HASH;

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success calculating hash.");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Trying to read and match hash from client...");

	if (!Read_line (input_lines, &line_buffer, &line_buffer_len,
							 &line_len, &eof_found, false, false))
		return ERROR_READING_HASH;

	errno = 0;

	auxres = ( (line_len >= HASH_LEN) &&
			(strncasecmp (line_buffer + line_len - HASH_LEN, hash, HASH_LEN) == 0));

	isTCPEvents = (auxres && (line_len >= (HASH_LEN + strlen (TCP_EVENTS_MSG))) &&
				   (strncasecmp (line_buffer, TCP_EVENTS_MSG, strlen (TCP_EVENTS_MSG)) == 0));

	Free_string (&line_buffer);

	if (!auxres)
		return ERROR_HASH_DOES_NOT_MATCH;

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success reading and matching hash from client.");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Sending \"accept\" message...");

	errno = 0;

	if (!Write_line (output_lines, ACCEPT_MSG))
		return ERROR_WRITING_ACCEPT_MSG;

	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Success sending \"accept\" message.");
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	Vlogit (tll_debug,
			LOG_MESSAGE_PREAMBLE,
			LOGLEVELNAME (tll_debug),
			PROCESS_P_C (g_daemonized, g_processor),
			getpid(), "Processing events...");


	close_requested = false;
	event_type = tet_unknown;

	event = NULL;
	payload = NULL;
	payload_len = 0;
	payload_buffer_len = 0;

	line_buffer = NULL;

	do
	{
		if ( (!Read_line (input_lines, &line_buffer, &line_buffer_len,
								   &line_len, &eof_found, false, true)) && (!eof_found))
		{
			Free_string_array (&payload, &payload_len, &payload_buffer_len);
			Free_string (&line_buffer);

			/* If the program is signaled to terminate. */
			if (Is_signaled_to_terminate())
				return ERROR_INTERRUPTED;

			return ERROR_READING_EVENT;
		}
		else if (eof_found)
		{

			Free_string_array (&payload, &payload_len, &payload_buffer_len);
			Free_string (&line_buffer);

			break;
		}
		else if (line_buffer_len > MAX_EVENT_LINE_LEN)
		{
			Free_string_array (&payload, &payload_len, &payload_buffer_len);
			Free_string (&line_buffer);

			return ERROR_EVENT_LINE_TOO_LONG;
		}

		/* If the program is signaled to terminate. */
		if (Is_signaled_to_terminate())
			break;

		if (strncasecmp (line_buffer, CLOSE_MSG,
						 MINVAL (line_len, strlen (CLOSE_MSG))) == 0)
		{
			Vlogit (tll_debug,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_debug),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Received: <Close>");

			/* Close requested. */

			Free_string_array (&payload, &payload_len, &payload_buffer_len);
			Free_string (&line_buffer);

			close_requested = true;
		}
		else if (strncasecmp (line_buffer, PAYLOAD_MSG,
							  MINVAL (line_len, strlen (PAYLOAD_MSG))) == 0)
		{
			Vlogit (tll_debug,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_debug),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Received: <Payload>");

			/* Add to payload. */

			if (!Add_string_to_string_array (&payload, &payload_len, &payload_buffer_len,
											 line_buffer + strlen (PAYLOAD_MSG) + 1))
			{
				Free_string_array (&payload, &payload_len, &payload_buffer_len);
				Free_string (&line_buffer);

				return ERROR_ALLOCATING_MEMORY;
			}
		}
		else if ( (payload_len > 0) && (strncasecmp (payload[payload_len - 1], BUTTON_RELEASED_MSG,
										MINVAL (strlen (payload[payload_len - 1]), strlen (BUTTON_RELEASED_MSG))) == 0))
		{
			Vlogit (tll_debug,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_debug),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Received: <Enduring Event - Stop>");

			/* End enduring event. */

			event = line_buffer;
			event_type = tet_end_event;
		}
		else if ( (payload_len > 0) && (strncasecmp (payload[payload_len - 1], WITHOUT_RELEASE_MSG,
										MINVAL (strlen (payload[payload_len - 1]), strlen (WITHOUT_RELEASE_MSG))) == 0))
		{
			Vlogit (tll_debug,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_debug),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Received: <Enduring Event - Start>");

			/* Trigger enduring event. */

			event = line_buffer;
			event_type = tet_enduring_event;
		}
		else
		{
			Vlogit (tll_debug,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_debug),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Received: <Event>");

			/* Trigger event. */

			event = line_buffer;
			event_type = tet_event;
		}

		errno = 0;

		if ( (!Is_signaled_to_terminate()) && (!close_requested) && (event_type != tet_unknown) &&
				(executable != NULL) && (strlen (executable) > 0))
		{
			Vlogit (tll_info,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_info),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(),
					"Executing command...");

			Ignore_or_restore_SIGPIPE_signal (true);

			auxres = Execute_and_report_output (executable, client_host_name, client_IP, client_port,
												(event != NULL) ? event : EMPTY_STRING,
												event_type, payload_len, (const char**) payload,
												&socket_fd, 1);

			Ignore_or_restore_SIGPIPE_signal (false);

			if (!auxres)

			{
				Free_string_array (&payload, &payload_len, &payload_buffer_len);
				Free_string (&line_buffer);

				return ERROR_EXECUTING_COMMAND;
			}

			Vlogit (tll_info,
					LOG_MESSAGE_PREAMBLE,
					LOGLEVELNAME (tll_info),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), "Success executing command.");
		}
		else if ( (!close_requested) && (event_type != tet_unknown))
		{
			Vlogit (tll_info,
					LOG_EVENT_NO_COMMAND,
					LOGLEVELNAME (tll_info),
					PROCESS_P_C (g_daemonized, g_processor),
					getpid(), event);
		}

		Free_string (&line_buffer);

		if (event_type != tet_unknown)
		{
			Free_string_array (&payload, &payload_len, &payload_buffer_len);
			close_requested = false;
			event_type = tet_unknown;

			event = NULL;
		}
	}
	while (!Is_signaled_to_terminate() && !close_requested);

	Free_string_array (&payload, &payload_len, &payload_buffer_len);
	Free_string (&line_buffer);
	/* ************************************************************************************** */


	/* ************************************************************************************** */
	/* If the program is signaled to terminate. */
	if (Is_signaled_to_terminate())
		return ERROR_INTERRUPTED;
	else if (close_requested)
	{
		Vlogit (tll_info,
				LOG_MESSAGE_PREAMBLE,
				LOGLEVELNAME (tll_info),
				PROCESS_P_C (g_daemonized, g_processor),
				getpid(), "Client requested close.");
	}
	else
	{
		Vlogit (tll_info,
				LOG_MESSAGE_PREAMBLE,
				LOGLEVELNAME (tll_info),
				PROCESS_P_C (g_daemonized, g_processor),
				getpid(), "Client closed the conection.");
	}
	/* ************************************************************************************** */

	errno = 0;

	return ERROR_SUCESS;
}
Ejemplo n.º 3
0
int CData::Read_libsvm(std::string file_name)
{
    max_line_len=1024;
    FILE *fp = NULL;
	fp = fopen(file_name.c_str(),"r");
    line = (char*)malloc(sizeof(char)*max_line_len); 
    int elements = 0;

    if(fp == NULL){
        std::cout << "Unable to open file\t" << file_name << endl;
		exit(1);
        return 0;
    }

    while(Read_line(fp) != NULL){
        char *p = strtok(line, " \t"); //label value doesn't account
        while(1){
            p = strtok(NULL, " \t");
            if(p == NULL || *p== '\n')
                break;
            elements++;
        } 
        elements++;
        this->n++;
    }
    rewind(fp);
    
    y = Malloc(double, n);
    x = Malloc(struct CFeature_node *, n);
    x_space = Malloc(struct CFeature_node, elements);

    int inst_max_index, max_index = 0,j=0;
    char *label,*endptr,*idx, *val;
   // double label_f = 0.0;
    for(int i=0; i<n; i++){
        inst_max_index = 0; 
        Read_line(fp);
        x[i] = &x_space[j];
        label = strtok(line, " \t\n");
        if(label == NULL)
            std::cout<<i+1<<"line error"<<std::endl;
        double label_f = strtod(label, &endptr);
        y[i] = label_f;
		if (label_f > 0.0)
		{
			num_pos++;		
		}
		else
		{
			num_neg++;
		}
        if(endptr == label || *endptr != '\0')
            std::cout<<i+1<<"line error"<<std::endl;

        while(1){
           idx = strtok(NULL, ":");
           val = strtok(NULL, " \t");

           if(val == NULL)
               break;
           int f_no = 0;
           x_space[j].index = (int)strtol(idx, &endptr, 10);
           if(endptr == idx || f_no != 0 || *endptr != '\0' || x_space[j].index <= inst_max_index)
                std::cout<<i+1<<"line error"<<std::endl;
           else
               inst_max_index = x_space[j].index;

           f_no = 0;
           x_space[j].value = strtod(val, &endptr);
           if(endptr == val || f_no != 0 || (*endptr != '\0' && !isspace(*endptr)))
                std::cout<<i+1<<"line error"<<std::endl;

           ++j;
        }
        
        if(inst_max_index > max_index)
            max_index = inst_max_index;
        
        x_space[j++].index = -1;

    }
    
    this->d = max_index;
    fclose(fp);

    free(line); 
    return 1;
}