Example #1
0
int cmd_recv_stream_2_1(struct relay_connection *conn,
		struct relay_stream *stream)
{
	int ret;
	struct lttcomm_relayd_add_stream stream_info;

	assert(conn);
	assert(stream);

	ret = cmd_recv(conn->sock, &stream_info, sizeof(stream_info));
	if (ret < 0) {
		ERR("Unable to recv stream version 2.1");
		goto error;
	}

	stream->path_name = create_output_path(stream_info.pathname);
	if (stream->path_name == NULL) {
		PERROR("Path name allocation");
		ret = -ENOMEM;
		goto error;
	}

	stream->channel_name = strdup(stream_info.channel_name);
	if (stream->channel_name == NULL) {
		ret = -errno;
		PERROR("Path name allocation");
		goto error;
	}
	ret = 0;

error:
	return ret;
}
Example #2
0
int main(int argc, char *argv[])
{
	//check arguments
	if(argc < 2 || argc > 3)
	{
		printf("evoscan_logworks_conv <version %s>\n", version);
		printf("[email protected], @640774n6\n\n");
		printf("usage: evoscan_logworks_conv <input csv path> <output dif path (optional)>\n");
		return 1;
	}

	char *input_path = argv[1];
	char *generated_output_path = create_output_path(input_path);
	char *output_path = (argc == 3) ? argv[2] : generated_output_path;
	
	if(!strcmp(input_path, output_path))
	{
		printf("error: input and output path must be different\n");
		free(generated_output_path);
		return 1;
	}
	
	//open file pointers
	FILE *input_file = fopen(input_path, "rb");
	if(!input_file)
	{
		printf("error: failed to open input @ %s\n", input_path);
		free(generated_output_path);
		return 1;
	}
	
	FILE *output_file = fopen(output_path, "wb");
	if(!output_path)
	{
		printf("error: failed to open output @ %s\n", output_path);
		free(generated_output_path);
		fclose(input_file);
		return 1;
	}
	
	FILE *tmp_file = tmpfile();
	if(!tmp_file)
	{
		printf("error: failed to open tmp file\n");
		free(generated_output_path);
		fclose(input_file);
		fclose(output_file);
		return 1;
	}
	
	//initialize variables
	fields = NULL;
	field_count = 0;
	used_fields = NULL;
	used_fields_min = NULL;
	used_fields_max = NULL;
	used_field_count = 0;
	total_sample_time = 0.0;
	total_sample_count = 0;
	row_sample_count = 0;
	csv_col = 0;
	csv_row = 0;
	
	//create csv parser
	struct csv_parser parser;
	unsigned char options = (CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);
	csv_init(&parser, options);
	
	//main parse loop
	size_t length = 0;
	char buffer[1024];
	while((length = fread(buffer, 1, 1024, input_file)) > 0)
	{
		//parse csv and handle with callbacks
		if(csv_parse(&parser, buffer, length, csv_process_col, csv_process_row, tmp_file) != length)
		{
			printf("error: failed to read from input @ %s\n", input_path);
			free(generated_output_path);
			fclose(input_file);
			fclose(output_file);
			fclose(tmp_file);
			csv_free(&parser);
			remove(output_path);
			return 1;
		}
	}
	
	//write output header
	fprintf(output_file, "TABLE\r\n0,1\r\n\"EXCEL\"\r\n");
	fprintf(output_file, "VECTORS\r\n0,%d\r\n\"\"\r\n", (total_sample_count + 13));
	fprintf(output_file, "TUPLES\r\n0,%d\r\n\"\"\r\n", (used_field_count + 1));
	fprintf(output_file, "DATA\r\n0,0\r\n\"\"\r\n");
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Input Description\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"\"\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Stochiometric:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"\"\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"From Device:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"(EVOSCAN%d)\"\r\n", (i + 1)); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Name:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"%s\"\r\n", used_fields[i]); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Unit:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"%.3s\"\r\n", used_fields[i]); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Range:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "0,%d\r\nV\r\n", used_fields_min[i]); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"equiv(Sample):\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "0,0\r\nV\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"to:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "0,%d\r\nV\r\n", used_fields_max[i] + 1); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"equiv(Sample):\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "0,4096\r\nV\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Color:\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "0,%d\r\nV\r\n", color_for_index(i)); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"-End-\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"\"\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Session 1\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"\"\r\n"); }
	fprintf(output_file, "-1,0\r\nBOT\r\n");
	
	fprintf(output_file, "1,0\r\n\"Time(sec)\"\r\n");
	for(int i = 0; i < used_field_count; i++)
	{ fprintf(output_file, "1,0\r\n\"%s (%.3s)\"\r\n", used_fields[i], used_fields[i]); }
	
	//append tmp to the output
	fseek(tmp_file, 0, SEEK_SET);
	while((length = fread(buffer, 1, 1024, tmp_file)) > 0)
	{ fwrite(buffer, sizeof(char), length, output_file); }
	
	//write footer
	fprintf(output_file, "-1,0\r\nEOD\r\n");
	
	//free generated output path
	free(generated_output_path);
	
	//free fields
	for(int i = 0; i < field_count; i++)
	{ free(fields[i]); }
	free(fields);
	free(used_fields);
	free(used_fields_min);
	free(used_fields_max);
	
	//close file pointers
	fclose(input_file);
	fclose(output_file);
	fclose(tmp_file);
	
	//free parser
	csv_free(&parser);
	
	return 0;
}
Example #3
0
/*
 * cmd_recv_stream_2_11 allocates path_name and channel_name.
 */
int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload,
		char **ret_path_name, char **ret_channel_name,
		uint64_t *tracefile_size, uint64_t *tracefile_count,
		uint64_t *trace_archive_id)
{
	int ret;
	struct lttcomm_relayd_add_stream_2_11 header;
	size_t header_len, received_names_size;
	struct lttng_buffer_view channel_name_view;
	struct lttng_buffer_view pathname_view;
	char *path_name = NULL;
	char *channel_name = NULL;

	header_len = sizeof(header);

	if (payload->size < header_len) {
		ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes",
				header_len, payload->size);
		ret = -1;
		goto error;
	}
	memcpy(&header, payload->data, header_len);

	header.channel_name_len = be32toh(header.channel_name_len);
	header.pathname_len = be32toh(header.pathname_len);
	header.tracefile_size = be64toh(header.tracefile_size);
	header.tracefile_count = be64toh(header.tracefile_count);
	header.trace_archive_id = be64toh(header.trace_archive_id);

	received_names_size = header.channel_name_len + header.pathname_len;
	if (payload->size < header_len + received_names_size) {
		ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes",
				header_len + received_names_size, payload->size);
		ret = -1;
		goto error;
	}

	/* Validate length against defined constant. */
	if (header.channel_name_len > DEFAULT_STREAM_NAME_LEN) {
		ret = -ENAMETOOLONG;
		ERR("Channel name too long");
		goto error;
	}
	if (header.pathname_len > LTTNG_NAME_MAX) {
		ret = -ENAMETOOLONG;
		ERR("Pathname too long");
		goto error;
	}

	/* Validate that names are (NULL terminated. */
	channel_name_view = lttng_buffer_view_from_view(payload, header_len,
			    header.channel_name_len);
	pathname_view = lttng_buffer_view_from_view(payload,
			header_len + header.channel_name_len, header.pathname_len);

	if (channel_name_view.data[channel_name_view.size - 1] != '\0') {
		ERR("cmd_recv_stream_2_11 channel_name is invalid (not NULL terminated)");
		ret = -1;
		goto error;
	}

	if (pathname_view.data[pathname_view.size - 1] != '\0') {
		ERR("cmd_recv_stream_2_11 patname is invalid (not NULL terminated)");
		ret = -1;
		goto error;
	}

	channel_name = strdup(channel_name_view.data);
	if (!channel_name) {
		ret = -errno;
		PERROR("Channel name allocation");
		goto error;
	}

	path_name = create_output_path(pathname_view.data);
	if (!path_name) {
		PERROR("Path name allocation");
		ret = -ENOMEM;
		goto error;
	}

	*tracefile_size = header.tracefile_size;
	*tracefile_count = header.tracefile_count;
	*trace_archive_id = header.trace_archive_id;
	*ret_path_name = path_name;
	*ret_channel_name = channel_name;
	/* Move ownership to caller */
	path_name = NULL;
	channel_name = NULL;
	ret = 0;
error:
	free(channel_name);
	free(path_name);
	return ret;
}