liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
/******************************************************************************/
/*                                                                            */
/* Copyright (c) International Business Machines  Corp., 2007                 */
/*                                                                            */
/* This program is free software;  you can redistribute it and/or modify      */
/* it under the terms of the GNU General Public License as published by       */
/* the Free Software Foundation; either version 2 of the License, or          */
/* (at your option) any later version.                                        */
/*                                                                            */
/* This program is distributed in the hope that it will be useful,            */
/* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
/* the GNU General Public License for more details.                           */
/*                                                                            */
/* You should have received a copy of the GNU General Public License          */
/* along with this program;  if not, write to the Free Software               */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
/*                                                                            */
/******************************************************************************/
 
/******************************************************************************/
/*                                                                            */
/* File:        libcontrollers.c                                              */
/*                                                                            */
/* Description: This file contains the definitions for the functions/apis     */
/*              for the controllers library. This library is used by the      */
/*              controllers testcases.                                        */
/*                                                                            */
/* Author:      Sudhir Kumar skumar@linux.vnet.ibm.com                        */
/*                                                                            */
/* History:                                                                   */
/* Created-     15/02/2008 -Sudhir Kumar <skumar@linux.vnet.ibm.com>          */
/*                                                                            */
/******************************************************************************/
 
#include "libcontrollers.h"
 
/*
 * 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) {    /* not a directory */
       /*
        * 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 in opening directory */
       error_function("Can't open ", fullpath);
       return -1;
   }
   /*
    * search all groups recursively and get total shares
    */
 
   while ((dir_pointer = readdir(dp)) != NULL) {    /* Error in reading directory */
       if ((strcmp(dir_pointer->d_name, ".") == 0)
           || (strcmp(dir_pointer->d_name, "..") == 0))
           continue;    /* ignore current and parent directory */
 
       FLAG = 1;
       strcpy(path_pointer, dir_pointer->d_name);    /* append name to fullpath */
 
       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;
   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 (fgets(target, LINE_MAX, fp) != NULL)
           num_line++;
       *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);
}
 
/* Function: read_shares_file()
 * 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;
}
 
/* Function: write_to_file()
 * 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;
}
 
/* Function: signal_handler_alarm()
 * signal handler for the new action
 */
 
void signal_handler_alarm(int signal)
{
   timer_expired = 1;
}