#include "libcontrollers.h"
char fullpath[PATH_MAX];
int FLAG;
volatile int timer_expired = 0;
int retval;
unsigned int current_shares;
unsigned int total_shares;
unsigned int *shares_pointer;
struct dirent *dir_pointer;
* Function: scan_shares_file()
* This function scans all the shares files under the mountpoint
* of the controller and returns the total added shares of all
* the groups (currently excludes default group) ??
*/
int scan_shares_files(unsigned int *shares_pointer)
{
struct stat statbuffer;
DIR *dp;
char *path_pointer;
* Check if we can get stat of the file
*/
if (lstat(fullpath, &statbuffer) < 0) {
error_function("Can not read stat for file ", fullpath);
return -1;
}
if (S_ISDIR(statbuffer.st_mode) == 0) {
* We run all user tasks in the created groups and not default groups. So
* exclude the shares of default group. FLAG to ensure dir_pointer is non NULL
*/
if ((FLAG == 1)
&& (strcmp(fullpath, "/dev/cpuctl/cpu.shares") != 0)
&& (strcmp(dir_pointer->d_name, "cpu.shares") == 0)) {
*shares_pointer += read_shares_file(fullpath);
}
return 0;
}
* Now it's a directory. let the path_pointer point to the end
* of fullpath to append new files names
*/
path_pointer = fullpath + strlen(fullpath);
*path_pointer++ = '/';
*path_pointer = 0;
if ((dp = opendir(fullpath)) == NULL) {
error_function("Can't open ", fullpath);
return -1;
}
* search all groups recursively and get total shares
*/
while ((dir_pointer = readdir(dp)) != NULL) {
if ((strcmp(dir_pointer->d_name, ".") == 0)
|| (strcmp(dir_pointer->d_name, "..") == 0))
continue;
FLAG = 1;
strcpy(path_pointer, dir_pointer->d_name);
if ((retval = scan_shares_files(shares_pointer)) != 0)
break;
}
* This directory is searched fully. let us go back to parent directory again
*/
path_pointer[-1] = 0;
if (closedir(dp) < 0) {
error_function("Could not close dir ", fullpath);
return -1;
}
return 0;
}
* Function: read_file ()
* This function is written keeping in mind the support
* to read other files also if required in future.
* Each file under a group contains some diff parameter/s
*/
int read_file(char *filepath, int action, unsigned int *value)
{
int num_line = 0;
FILE *fp;
int tmp;
size_t len;
char *target = NULL;
switch (action) {
case GET_SHARES:
tmp = read_shares_file(filepath);
if (tmp == -1)
return -1;
*value = (unsigned int)tmp;
break;
case GET_TASKS:
fp = fopen(filepath, "r");
if (fp == NULL) {
error_function("Could not open file", filepath);
return -1;
}
while (getline(&target, &len, fp) != -1)
num_line++;
free(target);
*value = (unsigned int)num_line;
if (fclose(fp)) {
error_function("Could not close file", filepath);
return -1;
}
break;
default:
error_function("Wrong action type passed to fun read_file for ",
filepath);
return -1;
}
return 0;
}
* Function: error_function()
* Prints error message and returns -1
*/
static inline void error_function(char *msg1, char *msg2)
{
fprintf(stdout, "ERROR: %s ", msg1);
fprintf(stdout, "%s\n", msg2);
}
* Reads shares value from a given shares file and writes them to
* the given pointer location. Returns 0 if success
*/
int read_shares_file(char *filepath)
{
FILE *fp;
unsigned int shares;
fp = fopen(filepath, "r");
if (fp == NULL) {
error_function("Could not open file", filepath);
return -1;
}
fscanf(fp, "%u", &shares);
if (fclose(fp)) {
error_function("Could not close file", filepath);
return -1;
}
return shares;
}
* writes value to shares file or pid to tasks file
*/
int write_to_file(char *file, const char *mode, unsigned int value)
{
FILE *fp;
fp = fopen(file, mode);
if (fp == NULL) {
error_function("in opening file for writing:", file);
return -1;
}
fprintf(fp, "%u\n", value);
fclose(fp);
return 0;
}
* signal handler for the new action
*/
void signal_handler_alarm(int signal)
{
timer_expired = 1;
}