1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
2*49cdfc7eSAndroid Build Coastguard Worker /* */
3*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) International Business Machines Corp., 2007 */
4*49cdfc7eSAndroid Build Coastguard Worker /* */
5*49cdfc7eSAndroid Build Coastguard Worker /* This program is free software; you can redistribute it and/or modify */
6*49cdfc7eSAndroid Build Coastguard Worker /* it under the terms of the GNU General Public License as published by */
7*49cdfc7eSAndroid Build Coastguard Worker /* the Free Software Foundation; either version 2 of the License, or */
8*49cdfc7eSAndroid Build Coastguard Worker /* (at your option) any later version. */
9*49cdfc7eSAndroid Build Coastguard Worker /* */
10*49cdfc7eSAndroid Build Coastguard Worker /* This program is distributed in the hope that it will be useful, */
11*49cdfc7eSAndroid Build Coastguard Worker /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12*49cdfc7eSAndroid Build Coastguard Worker /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13*49cdfc7eSAndroid Build Coastguard Worker /* the GNU General Public License for more details. */
14*49cdfc7eSAndroid Build Coastguard Worker /* */
15*49cdfc7eSAndroid Build Coastguard Worker /* You should have received a copy of the GNU General Public License */
16*49cdfc7eSAndroid Build Coastguard Worker /* along with this program; if not, write to the Free Software */
17*49cdfc7eSAndroid Build Coastguard Worker /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18*49cdfc7eSAndroid Build Coastguard Worker /* */
19*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
22*49cdfc7eSAndroid Build Coastguard Worker /* */
23*49cdfc7eSAndroid Build Coastguard Worker /* File: libcontrollers.c */
24*49cdfc7eSAndroid Build Coastguard Worker /* */
25*49cdfc7eSAndroid Build Coastguard Worker /* Description: This file contains the definitions for the functions/apis */
26*49cdfc7eSAndroid Build Coastguard Worker /* for the controllers library. This library is used by the */
27*49cdfc7eSAndroid Build Coastguard Worker /* controllers testcases. */
28*49cdfc7eSAndroid Build Coastguard Worker /* */
29*49cdfc7eSAndroid Build Coastguard Worker /* Author: Sudhir Kumar [email protected] */
30*49cdfc7eSAndroid Build Coastguard Worker /* */
31*49cdfc7eSAndroid Build Coastguard Worker /* History: */
32*49cdfc7eSAndroid Build Coastguard Worker /* Created- 15/02/2008 -Sudhir Kumar <[email protected]> */
33*49cdfc7eSAndroid Build Coastguard Worker /* */
34*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************/
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #include "libcontrollers.h"
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker char fullpath[PATH_MAX];
39*49cdfc7eSAndroid Build Coastguard Worker int FLAG;
40*49cdfc7eSAndroid Build Coastguard Worker volatile int timer_expired = 0;
41*49cdfc7eSAndroid Build Coastguard Worker int retval;
42*49cdfc7eSAndroid Build Coastguard Worker unsigned int current_shares;
43*49cdfc7eSAndroid Build Coastguard Worker unsigned int total_shares;
44*49cdfc7eSAndroid Build Coastguard Worker unsigned int *shares_pointer;
45*49cdfc7eSAndroid Build Coastguard Worker struct dirent *dir_pointer;
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker /*
48*49cdfc7eSAndroid Build Coastguard Worker * Function: scan_shares_file()
49*49cdfc7eSAndroid Build Coastguard Worker * This function scans all the shares files under the mountpoint
50*49cdfc7eSAndroid Build Coastguard Worker * of the controller and returns the total added shares of all
51*49cdfc7eSAndroid Build Coastguard Worker * the groups (currently excludes default group) ??
52*49cdfc7eSAndroid Build Coastguard Worker */
scan_shares_files(unsigned int * shares_pointer)53*49cdfc7eSAndroid Build Coastguard Worker int scan_shares_files(unsigned int *shares_pointer)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker struct stat statbuffer;
56*49cdfc7eSAndroid Build Coastguard Worker DIR *dp;
57*49cdfc7eSAndroid Build Coastguard Worker char *path_pointer;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker /*
60*49cdfc7eSAndroid Build Coastguard Worker * Check if we can get stat of the file
61*49cdfc7eSAndroid Build Coastguard Worker */
62*49cdfc7eSAndroid Build Coastguard Worker if (lstat(fullpath, &statbuffer) < 0) {
63*49cdfc7eSAndroid Build Coastguard Worker error_function("Can not read stat for file ", fullpath);
64*49cdfc7eSAndroid Build Coastguard Worker return -1;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (S_ISDIR(statbuffer.st_mode) == 0) { /* not a directory */
68*49cdfc7eSAndroid Build Coastguard Worker /*
69*49cdfc7eSAndroid Build Coastguard Worker * We run all user tasks in the created groups and not default groups. So
70*49cdfc7eSAndroid Build Coastguard Worker * exclude the shares of default group. FLAG to ensure dir_pointer is non NULL
71*49cdfc7eSAndroid Build Coastguard Worker */
72*49cdfc7eSAndroid Build Coastguard Worker if ((FLAG == 1)
73*49cdfc7eSAndroid Build Coastguard Worker && (strcmp(fullpath, "/dev/cpuctl/cpu.shares") != 0)
74*49cdfc7eSAndroid Build Coastguard Worker && (strcmp(dir_pointer->d_name, "cpu.shares") == 0)) {
75*49cdfc7eSAndroid Build Coastguard Worker *shares_pointer += read_shares_file(fullpath);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker return 0;
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker /*
81*49cdfc7eSAndroid Build Coastguard Worker * Now it's a directory. let the path_pointer point to the end
82*49cdfc7eSAndroid Build Coastguard Worker * of fullpath to append new files names
83*49cdfc7eSAndroid Build Coastguard Worker */
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker path_pointer = fullpath + strlen(fullpath);
86*49cdfc7eSAndroid Build Coastguard Worker *path_pointer++ = '/';
87*49cdfc7eSAndroid Build Coastguard Worker *path_pointer = 0;
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if ((dp = opendir(fullpath)) == NULL) { /* Error in opening directory */
90*49cdfc7eSAndroid Build Coastguard Worker error_function("Can't open ", fullpath);
91*49cdfc7eSAndroid Build Coastguard Worker return -1;
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker /*
94*49cdfc7eSAndroid Build Coastguard Worker * search all groups recursively and get total shares
95*49cdfc7eSAndroid Build Coastguard Worker */
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker while ((dir_pointer = readdir(dp)) != NULL) { /* Error in reading directory */
98*49cdfc7eSAndroid Build Coastguard Worker if ((strcmp(dir_pointer->d_name, ".") == 0)
99*49cdfc7eSAndroid Build Coastguard Worker || (strcmp(dir_pointer->d_name, "..") == 0))
100*49cdfc7eSAndroid Build Coastguard Worker continue; /* ignore current and parent directory */
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker FLAG = 1;
103*49cdfc7eSAndroid Build Coastguard Worker strcpy(path_pointer, dir_pointer->d_name); /* append name to fullpath */
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker if ((retval = scan_shares_files(shares_pointer)) != 0)
106*49cdfc7eSAndroid Build Coastguard Worker break;
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker /*
110*49cdfc7eSAndroid Build Coastguard Worker * This directory is searched fully. let us go back to parent directory again
111*49cdfc7eSAndroid Build Coastguard Worker */
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker path_pointer[-1] = 0;
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker if (closedir(dp) < 0) {
116*49cdfc7eSAndroid Build Coastguard Worker error_function("Could not close dir ", fullpath);
117*49cdfc7eSAndroid Build Coastguard Worker return -1;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker return 0;
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker /*
123*49cdfc7eSAndroid Build Coastguard Worker * Function: read_file ()
124*49cdfc7eSAndroid Build Coastguard Worker * This function is written keeping in mind the support
125*49cdfc7eSAndroid Build Coastguard Worker * to read other files also if required in future.
126*49cdfc7eSAndroid Build Coastguard Worker * Each file under a group contains some diff parameter/s
127*49cdfc7eSAndroid Build Coastguard Worker */
128*49cdfc7eSAndroid Build Coastguard Worker
read_file(char * filepath,int action,unsigned int * value)129*49cdfc7eSAndroid Build Coastguard Worker int read_file(char *filepath, int action, unsigned int *value)
130*49cdfc7eSAndroid Build Coastguard Worker {
131*49cdfc7eSAndroid Build Coastguard Worker int num_line = 0;
132*49cdfc7eSAndroid Build Coastguard Worker FILE *fp;
133*49cdfc7eSAndroid Build Coastguard Worker int tmp;
134*49cdfc7eSAndroid Build Coastguard Worker size_t len;
135*49cdfc7eSAndroid Build Coastguard Worker char *target = NULL;
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker switch (action) {
138*49cdfc7eSAndroid Build Coastguard Worker case GET_SHARES:
139*49cdfc7eSAndroid Build Coastguard Worker tmp = read_shares_file(filepath);
140*49cdfc7eSAndroid Build Coastguard Worker if (tmp == -1)
141*49cdfc7eSAndroid Build Coastguard Worker return -1;
142*49cdfc7eSAndroid Build Coastguard Worker *value = (unsigned int)tmp;
143*49cdfc7eSAndroid Build Coastguard Worker break;
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker case GET_TASKS:
146*49cdfc7eSAndroid Build Coastguard Worker fp = fopen(filepath, "r");
147*49cdfc7eSAndroid Build Coastguard Worker if (fp == NULL) {
148*49cdfc7eSAndroid Build Coastguard Worker error_function("Could not open file", filepath);
149*49cdfc7eSAndroid Build Coastguard Worker return -1;
150*49cdfc7eSAndroid Build Coastguard Worker }
151*49cdfc7eSAndroid Build Coastguard Worker while (getline(&target, &len, fp) != -1)
152*49cdfc7eSAndroid Build Coastguard Worker num_line++;
153*49cdfc7eSAndroid Build Coastguard Worker free(target);
154*49cdfc7eSAndroid Build Coastguard Worker *value = (unsigned int)num_line;
155*49cdfc7eSAndroid Build Coastguard Worker if (fclose(fp)) {
156*49cdfc7eSAndroid Build Coastguard Worker error_function("Could not close file", filepath);
157*49cdfc7eSAndroid Build Coastguard Worker return -1;
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker break;
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker default:
162*49cdfc7eSAndroid Build Coastguard Worker error_function("Wrong action type passed to fun read_file for ",
163*49cdfc7eSAndroid Build Coastguard Worker filepath);
164*49cdfc7eSAndroid Build Coastguard Worker return -1;
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker return 0;
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker /*
170*49cdfc7eSAndroid Build Coastguard Worker * Function: error_function()
171*49cdfc7eSAndroid Build Coastguard Worker * Prints error message and returns -1
172*49cdfc7eSAndroid Build Coastguard Worker */
173*49cdfc7eSAndroid Build Coastguard Worker
error_function(char * msg1,char * msg2)174*49cdfc7eSAndroid Build Coastguard Worker static inline void error_function(char *msg1, char *msg2)
175*49cdfc7eSAndroid Build Coastguard Worker {
176*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "ERROR: %s ", msg1);
177*49cdfc7eSAndroid Build Coastguard Worker fprintf(stdout, "%s\n", msg2);
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker /* Function: read_shares_file()
181*49cdfc7eSAndroid Build Coastguard Worker * Reads shares value from a given shares file and writes them to
182*49cdfc7eSAndroid Build Coastguard Worker * the given pointer location. Returns 0 if success
183*49cdfc7eSAndroid Build Coastguard Worker */
184*49cdfc7eSAndroid Build Coastguard Worker
read_shares_file(char * filepath)185*49cdfc7eSAndroid Build Coastguard Worker int read_shares_file(char *filepath)
186*49cdfc7eSAndroid Build Coastguard Worker {
187*49cdfc7eSAndroid Build Coastguard Worker FILE *fp;
188*49cdfc7eSAndroid Build Coastguard Worker unsigned int shares;
189*49cdfc7eSAndroid Build Coastguard Worker fp = fopen(filepath, "r");
190*49cdfc7eSAndroid Build Coastguard Worker if (fp == NULL) {
191*49cdfc7eSAndroid Build Coastguard Worker error_function("Could not open file", filepath);
192*49cdfc7eSAndroid Build Coastguard Worker return -1;
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker fscanf(fp, "%u", &shares);
195*49cdfc7eSAndroid Build Coastguard Worker if (fclose(fp)) {
196*49cdfc7eSAndroid Build Coastguard Worker error_function("Could not close file", filepath);
197*49cdfc7eSAndroid Build Coastguard Worker return -1;
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker return shares;
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker
202*49cdfc7eSAndroid Build Coastguard Worker /* Function: write_to_file()
203*49cdfc7eSAndroid Build Coastguard Worker * writes value to shares file or pid to tasks file
204*49cdfc7eSAndroid Build Coastguard Worker */
205*49cdfc7eSAndroid Build Coastguard Worker
write_to_file(char * file,const char * mode,unsigned int value)206*49cdfc7eSAndroid Build Coastguard Worker int write_to_file(char *file, const char *mode, unsigned int value)
207*49cdfc7eSAndroid Build Coastguard Worker {
208*49cdfc7eSAndroid Build Coastguard Worker FILE *fp;
209*49cdfc7eSAndroid Build Coastguard Worker fp = fopen(file, mode);
210*49cdfc7eSAndroid Build Coastguard Worker if (fp == NULL) {
211*49cdfc7eSAndroid Build Coastguard Worker error_function("in opening file for writing:", file);
212*49cdfc7eSAndroid Build Coastguard Worker return -1;
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker fprintf(fp, "%u\n", value);
215*49cdfc7eSAndroid Build Coastguard Worker fclose(fp);
216*49cdfc7eSAndroid Build Coastguard Worker return 0;
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker
219*49cdfc7eSAndroid Build Coastguard Worker /* Function: signal_handler_alarm()
220*49cdfc7eSAndroid Build Coastguard Worker * signal handler for the new action
221*49cdfc7eSAndroid Build Coastguard Worker */
222*49cdfc7eSAndroid Build Coastguard Worker
signal_handler_alarm(int signal)223*49cdfc7eSAndroid Build Coastguard Worker void signal_handler_alarm(int signal)
224*49cdfc7eSAndroid Build Coastguard Worker {
225*49cdfc7eSAndroid Build Coastguard Worker timer_expired = 1;
226*49cdfc7eSAndroid Build Coastguard Worker }
227