summaryrefslogtreecommitdiffstats
path: root/binding/task-manager-binding.c
blob: 023fae4a080fc5f142eb4c1386f4d4979eaa07e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <proc/readproc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <proc/sysinfo.h>
#include <math.h>
#include <unistd.h>
#include <json-c/json.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>

struct pstat {
	long long unsigned int utime_ticks;
	long long unsigned int cutime_ticks;
	long long unsigned int stime_ticks;
	long long unsigned int cstime_ticks;
	long unsigned int cpu_total_time;
};

struct cpu_percentage {
	double ucpu_usage;
	double scpu_usage;
};

struct process_container {
	char *process_name;
	int euid;
	struct pstat pstat_values;
};

void get_process_list(afb_req_t request);
int fill_pstat(proc_t *proc_info, struct pstat *pstat);
void cpu_calculate(struct pstat*, struct pstat*, struct cpu_percentage *perc);
void fill_process_container(char* process_name, int euid, struct pstat *pstat_values, struct process_container *pc);

void get_process_list(afb_req_t request){

	struct pstat last_pstat_values, cur_pstat_values;
	struct cpu_percentage cpu_usage;
	struct process_container *process_obj, *elem = NULL;
	struct process_container *object_container[65535]; // array holding process objects
	struct json_object *ret_json, *json_array, *json_obj;
	ret_json = json_object_new_object();
	json_array = json_object_new_array();
	char state_str[2] = "\0";
	int ret;

	PROCTAB* proc = openproc(PROC_FILLMEM | PROC_FILLSTAT);
	if (!proc){
		AFB_REQ_ERROR(request, "Unable to open /proc!");
		afb_req_reply(request, NULL, "Failed", "Error processing arguments.");
		return;
	}

	memset(object_container, 0, sizeof(*object_container));
	proc_t* proc_info;
	while ((proc_info = readproc(proc, NULL)) != NULL) {
		ret = fill_pstat(proc_info, &last_pstat_values);
		if (ret < 0) {
			AFB_REQ_ERROR(request, "fill_pstat failed");
			continue;
		}
		process_obj = malloc(sizeof(*process_obj));
		if (process_obj == NULL) {
			AFB_REQ_ERROR(request, "allocation of process_obj failed");
			continue;
		}
		fill_process_container(proc_info->cmd, proc_info->euid, &last_pstat_values, process_obj);
		object_container[proc_info->tid] = process_obj;
		freeproc(proc_info);
	}

	sleep(1);

	closeproc(proc);
	proc = openproc(PROC_FILLMEM | PROC_FILLSTAT);
	if (!proc){
		AFB_REQ_ERROR(request, "Unable to open /proc!");
		afb_req_reply(request, NULL, "Failed", "Error processing arguments.");
		return;
	}

	while ((proc_info = readproc(proc, NULL)) != NULL) {
		ret = fill_pstat(proc_info, &cur_pstat_values);
		if (ret < 0) {
			AFB_REQ_ERROR(request, "fill_pstat failed");
			continue;
		}
		elem = object_container[proc_info->tid];
		if (elem) {
			cpu_calculate(&elem->pstat_values, &cur_pstat_values, &cpu_usage);
			json_obj = json_object_new_object();
			json_object_object_add(json_obj, "cmd", json_object_new_string(proc_info->cmd));
			json_object_object_add(json_obj, "tid", json_object_new_int(proc_info->tid));
			json_object_object_add(json_obj, "euid", json_object_new_int(proc_info->euid));
			json_object_object_add(json_obj, "scpu", json_object_new_double(cpu_usage.scpu_usage));
			json_object_object_add(json_obj, "ucpu", json_object_new_double(cpu_usage.ucpu_usage));
			json_object_object_add(json_obj, "resident_mem", json_object_new_double((proc_info->resident * getpagesize())/ pow(1024, 2)));
			state_str[0] = proc_info->state;
			json_object_object_add(json_obj, "state", json_object_new_string(state_str));
			json_object_array_add(json_array, json_obj);
		}
		freeproc(proc_info);
	}
	json_object_object_add(ret_json, "msgType", json_object_new_string("processList"));
	json_object_object_add(ret_json, "processes", json_array);
	afb_req_reply(request, ret_json, NULL, NULL);

	closeproc(proc);
}

void kill_process(afb_req_t request)
{
	json_object *queryJ = afb_req_json(request);
	int tid = json_object_get_int(queryJ);
	int ret;

	AFB_REQ_INFO(request, "killing %d\n", tid);
	/* XXX: add checks */
	ret = kill(tid, SIGTERM);
	if (ret < 0)
		afb_req_reply_f(request, NULL, "Failed", "Error %d", errno);
	/* we don't signal success, there's no use for it */
}

void get_extra_info(afb_req_t request)
{
	struct json_object *ret_json, *json_obj;
	json_object *req = afb_req_json(request);
	int tid = json_object_get_int(req);

	ret_json = json_object_new_object();
	json_obj = json_object_new_object();

	char path[32];
	char str_val[256];
	char param[80];

	sprintf(path, "/proc/%d/sched", tid);
	FILE *fsched = fopen(path, "r");

	if (fsched == NULL) {
		afb_req_reply(request, NULL, "Failed", "Error processing arguments.");
		return;
	}
	else {
		json_object_object_add(json_obj, "tid", json_object_new_int(tid));

		fscanf(fsched, "%255s", str_val);
		json_object_object_add(json_obj, "cmd", json_object_new_string(str_val));

		fscanf(fsched, "%*s"); // the '-' line
		while (!feof(fsched)) {
			fscanf(fsched, "%79s%*s%255s", param, str_val);
			if (strstr(param, "exec_start"))
				json_object_object_add(json_obj, "exec_start", json_object_new_string(str_val));
			else if (strstr(param, "vruntime"))
				json_object_object_add(json_obj, "vruntime", json_object_new_string(str_val));
			else if (strstr(param, "sum_exec_runtime"))
				json_object_object_add(json_obj, "sum_exec_runtime", json_object_new_string(str_val));
			else if (strstr(param, "prio"))
				json_object_object_add(json_obj, "prio", json_object_new_string(str_val));
		}
	}
	fclose(fsched);

	json_object_object_add(ret_json, "msgType", json_object_new_string("extraInfo"));
	json_object_object_add(ret_json, "info", json_obj);

	afb_req_reply(request, ret_json, NULL, NULL);
}

void get_load_avg(afb_req_t request)
{
	struct json_object *ret_json, *json_obj;
		float value = 1.;

	ret_json = json_object_new_object();
	json_obj = json_object_new_object();

	FILE *floadAvg = fopen("/proc/loadavg", "r");

	if (floadAvg == NULL) {
		afb_req_reply(request, NULL, "Failed", "Error processing arguments.");
		return;
	}
	else {
		fscanf(floadAvg, "%4f%4*f%4*f", &value);

		json_object_object_add(json_obj, "value", json_object_new_double(value));
		json_object_object_add(json_obj, "str", json_object_new_string("Load"));

		json_object_object_add(ret_json, "msgType", json_object_new_string("loadAvgInfo"));
		json_object_object_add(ret_json, "loadInfo", json_obj);
	}
	fclose(floadAvg);

	afb_req_reply(request, ret_json, NULL, NULL);
}

void get_netstat(afb_req_t request)
{
	struct json_object *ret_json, *json_obj;
	char names_str[1024], values_str[1024];
	char *pnames = names_str, *pvalues = values_str;

	FILE *fnetstat = fopen("/proc/net/netstat", "r");

	if (fnetstat == NULL) {
		afb_req_reply(request, NULL, "Failed", "Error processing arguments.");
		return;
	}

	do {
		pnames = fgets(pnames, 1024, fnetstat);
	} while (pnames && !feof(fnetstat) && strncmp(pnames, "IpExt: ", 7));
	if (feof(fnetstat)) {
		fclose(fnetstat);
		afb_req_reply(request, NULL, "Failed", "Parse error");
		return;
	}
	pvalues = fgets(pvalues, 1024, fnetstat);
	fclose(fnetstat);
	if (pvalues == NULL) {
		afb_req_reply(request, NULL, "Failed", "Parse error");
		return;
	}

	ret_json = json_object_new_object();
	json_obj = json_object_new_object();

	pnames += 7; pvalues += 7;
	while (pnames && pvalues) {
		char *name = strsep(&pnames, " ");
		char *value = strsep(&pvalues, " ");

		if (name && value) {
			AFB_REQ_INFO(request, "adding %s[%s]\n", name, value);
			json_object_object_add(json_obj, name, json_object_new_int(atoi(value)));
		}
	}
	json_object_object_add(ret_json, "msgType", json_object_new_string("netStatInfo"));
	json_object_object_add(ret_json, "netstat", json_obj);

	afb_req_reply(request, ret_json, NULL, NULL);
}

int fill_pstat(proc_t *proc_info, struct pstat *pstat_values)
{
	long unsigned int cpu_time[9];

	pstat_values->utime_ticks = proc_info->utime;
	pstat_values->cutime_ticks = proc_info->cutime;
	pstat_values->stime_ticks = proc_info->stime;
	pstat_values->cstime_ticks = proc_info->cstime;

	FILE *fstat = fopen("/proc/stat", "r");
	if (fstat == NULL)
		return -1;

	memset(cpu_time, 0, sizeof(cpu_time));
	fscanf(fstat, "%*s %lu %lu %lu %lu %*lu %lu %lu %lu %lu %lu",
		&cpu_time[0], &cpu_time[1], &cpu_time[2], &cpu_time[3],
		&cpu_time[4], &cpu_time[5], &cpu_time[6], &cpu_time[7],
		&cpu_time[8]);

	fclose(fstat);

	/*
	 * Returns total CPU time. It is a sum of user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice
	 *
	 *
	 */
	pstat_values->cpu_total_time = 0;
	for(int i = 0; i < 9; i++)
		pstat_values->cpu_total_time += cpu_time[i];

	return 0;
}

void cpu_calculate(struct pstat *last_pstat_values, struct pstat *cur_pstat_values, struct cpu_percentage *cpu_values)
{

	long unsigned int total_time_diff = cur_pstat_values->cpu_total_time - last_pstat_values->cpu_total_time;

	cpu_values->ucpu_usage = 100.0 * (((cur_pstat_values->utime_ticks + cur_pstat_values->cutime_ticks)
		- (last_pstat_values->utime_ticks + last_pstat_values->cutime_ticks)) / (double) total_time_diff);

	cpu_values->scpu_usage = 100.0 * (((cur_pstat_values->stime_ticks + cur_pstat_values->cstime_ticks)
		- (last_pstat_values->stime_ticks + last_pstat_values->cstime_ticks)) / (double) total_time_diff);
}

void fill_process_container(char *process_name, int euid, struct pstat *pstat_values, struct process_container *pc)
{
	pc->process_name = process_name;
	pc->euid = euid;
	pc->pstat_values = *pstat_values;
}

static const afb_verb_t _afb_verbs_taskmanager[] = {
	{
		.verb = "get_process_list",
		.callback = get_process_list,
		.auth = NULL,
		.info = "Get an array of all processes currently running on the system",
		.session = AFB_SESSION_NONE
	},
	{
		.verb = "kill_process",
		.callback = kill_process,
		.auth = NULL,
		.info = "Kill the process specified by tid",
		.session = AFB_SESSION_NONE
	},
	{
		.verb = "get_extra_info",
		.callback = get_extra_info,
		.auth = NULL,
		.info = "Get exta info about current process",
		.session = AFB_SESSION_NONE
	},
	{
		.verb = "get_load_avg",
		.callback = get_load_avg,
		.auth = NULL,
		.info = "Get exta info about system load average",
		.session = AFB_SESSION_NONE
	},
	{
		.verb = "get_netstat",
		.callback = get_netstat,
		.auth = NULL,
		.info = "Get network (IP) statistics",
		.session = AFB_SESSION_NONE
	},
	{ .verb = NULL }
};


const afb_binding_t afbBindingExport = {
	.api = "taskmanager",
	.specification = NULL,
	.info = "Task Manager service",
	.verbs = _afb_verbs_taskmanager,
	.noconcurrency = 0
};