1/************************************************************************
2Copyright (c) 2015, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7    * Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above
10      copyright notice, this list of conditions and the following
11      disclaimer in the documentation and/or other materials provided
12      with the distribution.
13    * Neither the name of The Linux Foundation nor the names of its
14      contributors may be used to endorse or promote products derived
15      from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28************************************************************************/
29
30/**
31 * @file datatop_value_only_poll.c
32 * @brief Adds ability for data collection from files with only values
33 *
34 * File contains methods for searching and polling data from
35 * value_only files, meaning a file with a single line, containing
36 * only values.
37 */
38
39#include <stdio.h>
40#include <string.h>
41#include <stdlib.h>
42#include "datatop_interface.h"
43#include "datatop_fileops.h"
44#include "datatop_str.h"
45
46#define DTOP_SINGLE_SIZE 8192
47#define DTOP_SINGLE_LINE (DTOP_SINGLE_SIZE>>2)
48
49/**
50 * @brief Stores the data collected from a value_only files.
51 *
52 * @param dpg Struct that polled data is added to.
53 * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful.
54 * @return DTOP_POLL_OK - Poll of dpg successful.
55 */
56int dtop_value_only_poll(struct dtop_data_point_gatherer *dpg)
57{
58	char *data;
59	int line_len;
60	char line[DTOP_SINGLE_LINE];
61	int read;
62	struct dt_procdict dict;
63	int j;
64	FILE *check = fopen(dpg->file, "r");
65	if (check) {
66		fclose(check);
67		read = dt_read_file(dpg->file, &data, DTOP_SINGLE_SIZE);
68	} else {
69		return DTOP_POLL_IO_ERR;
70	}
71
72	if (read == 0 || data == 0)
73		return DTOP_POLL_IO_ERR;
74
75	line_len = dt_read_line(line, DTOP_SINGLE_LINE, data,
76					   DTOP_SINGLE_SIZE, 0);
77
78	/* Stores dp values in dictionary */
79	dt_single_line_parse(line, line_len, &dict);
80
81	/* Assigns the dp value to the dp struct */
82	for (j = 0; j < dpg->data_points_len; j++)
83		dtop_store_dp(&(dpg->data_points[j]), dict.val[j]);
84
85	dt_free(&data);
86	return DTOP_POLL_OK;
87}
88
89/**
90 * @brief Frees dynamically allocated single line dpg.
91 *
92 * Frees the memory of the dpg along with it's data_points
93 * and other malloc'd memory no longer needed.
94 *
95 * @param dpg Dpg to deconstruct and deallocate memory for.
96 */
97void dtop_value_only_dpg_deconstructor
98			(struct dtop_data_point_gatherer *dpset)
99{
100	int i;
101	for (i = 0; i < dpset->data_points_len; i++)
102		free(dpset->data_points[i].name);
103	free(dpset->data_points);
104	free(dpset->file);
105	free(dpset);
106}
107
108/**
109 * @brief Creates a dpg for a single line file.
110 *
111 * Dynamically allocates memory for dpg which is then added to a linked list
112 * via the dtop_register(dpg) function call.
113 *
114 * @param name Name of file dpg represents.
115 * @param data_points dtop_data_point struct that dpg points to.
116 * @param dp_count Number of data_points in data_points array
117 */
118static void construct_value_only_dpg(char *name, struct dtop_data_point
119		*data_points, int dp_count)
120{
121	struct dtop_data_point_gatherer *dpg = malloc
122		(sizeof(struct dtop_data_point_gatherer));
123	dpg->prefix = name;
124	dpg->file = name;
125	dpg->poll = dtop_value_only_poll;
126	dpg->data_points = data_points;
127	dpg->data_points_len = dp_count;
128	dpg->deconstruct = dtop_value_only_dpg_deconstructor;
129
130	dtop_register(dpg);
131}
132
133/**
134 * @brief Scans a single line file in order to autodetect dps.
135 *
136 * Searches through a file that contains a single line and only
137 * values in order to detect and create dp's/
138 *
139 * @param name Name of file to scan.
140 */
141int dtop_value_only_search(char *name)
142{
143	int i;
144	char *data;
145	char line[DTOP_SINGLE_LINE];
146	int line_len;
147	int read;
148	struct dt_procdict dict;
149	struct dtop_data_point *data_points;
150
151
152	read = dt_read_file(name, &data, DTOP_SINGLE_SIZE);
153	if (read == 0 || data == 0) {
154		free(name);
155		return DTOP_POLL_IO_ERR;
156	}
157
158	line_len = dt_read_line(line,
159				DTOP_SINGLE_LINE, data,
160				DTOP_SINGLE_SIZE, 0);
161
162	/* Stores dp values in dictionary */
163	dt_single_line_parse(line, line_len, &dict);
164
165	data_points = malloc(dict.max * sizeof(struct dtop_data_point));
166
167	/* Creates a dtop_data_point struct for each dp found in the file */
168	for (i = 0; i < dict.max; i++) {
169		char *newname = malloc(sizeof(10));
170		if (dict.val[i][0] == '-')
171			data_points[i].type = DTOP_LONG;
172		else
173			data_points[i].type = DTOP_ULONG;
174		data_points[i].name = malloc(sizeof(10));
175		if (dict.max > 1)
176			snprintf(newname, sizeof(10), "[%d]:", i);
177		else
178			strlcpy(newname, "", sizeof(10));
179		strlcpy(data_points[i].name, newname, sizeof(10));
180		free(newname);
181		data_points[i].prefix = NULL;
182		data_points[i].skip = DO_NOT_SKIP;
183		data_points[i].initial_data_populated = NOT_POPULATED;
184	}
185
186	/* Calls dpg constructor, dpg will point to the dp struct */
187	construct_value_only_dpg(name, data_points, dict.max);
188
189	dt_free(&data);
190	return DTOP_POLL_OK;
191}
192
193/**
194 * @brief Calls dtop_search for files with a single line and only values.
195 */
196void dtop_value_only_init(char *name)
197{
198	char *file = malloc(strlen(name) + 1);
199	strlcpy(file, name, strlen(name) + 1);
200	dtop_value_only_search(file);
201}
202