-
Notifications
You must be signed in to change notification settings - Fork 2
/
intel-power-control-helper.c
294 lines (256 loc) · 6.62 KB
/
intel-power-control-helper.c
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
const char* drmbasepath = "/sys/class/drm";
const char* cpubasepath = "/sys/devices/system/cpu";
const char* backlightpath = "/sys/class/backlight/intel_backlight";
const size_t bufsize = 10;
int toggleCPU( const char* cpu)
{
#ifdef DEBUG
printf("call toggleCPU(%s)\n", cpu);
#endif
const char* fn = "online";
size_t buflen = strlen(cpubasepath) + strlen(cpu) + strlen(fn) + 3;
char* buf = calloc( sizeof(char), buflen);
snprintf( buf, buflen, "%s/%s/%s", cpubasepath, cpu, fn);
FILE* f1 = fopen( buf, "r");
if( !f1)
{
printf( "Could not open '%s'\n", buf);
free( buf);
return 1;
}
char state = 0;
size_t read = fread( &state, sizeof(char), 1, f1);
fclose(f1);
if( read == 0)
{
printf( "Could not read from '%s'\n", buf);
free( buf);
return 1;
}
char newState = (char)(((int)'0')+(((int)(state-'0')+1)%2));
FILE* f2 = fopen( buf, "w");
if( !f2)
{
printf( "Could not open '%s'\n", buf);
free( buf);
return 1;
}
size_t written = fwrite( &newState, sizeof(char), 1, f2);
fclose( f2);
free( buf);
if( written != 1)
{
printf( "Error writing byte to file, got %lu\n", written);
return 1;
}
return 0;
}
int setMHz( const char* gpu, const char* fn, const char* val)
{
#ifdef DEBUG
printf("call setMHz(%s,%s,%s)\n", gpu, fn, val);
#endif
size_t buflen = strlen(drmbasepath) + strlen(gpu) + strlen(fn) + 3;
char* buf = calloc( sizeof(char), buflen);
snprintf( buf, buflen, "%s/%s/%s", drmbasepath, gpu, fn);
FILE* f = fopen( buf, "w");
if( !f)
{
printf( "Could not open '%s'\n", buf);
free( buf);
return 1;
}
size_t written = fwrite( val, sizeof(char), strlen(val), f);
fclose( f);
free( buf);
if( written != strlen(val))
{
printf( "Error writing %lu bytes to file, got %lu\n", strlen(val), written);
return 1;
}
return 0;
}
int setBrightness( const char* val)
{
const char* fn = "brightness";
size_t buflen = strlen(backlightpath) + strlen(fn) + 2;
char* buf = calloc( sizeof(char), buflen);
snprintf( buf, buflen, "%s/%s", backlightpath, fn);
FILE* f = fopen( buf, "w");
if( !f)
{
printf( "Could not open '%s'\n", buf);
free( buf);
return 1;
}
size_t written = fwrite( val, sizeof(char), strlen(val), f);
fclose( f);
free( buf);
if( written != strlen(val))
{
printf( "Error writing %lu bytes to file, got %lu\n", strlen(val), written);
return 1;
}
return 0;
}
void checkPath(const char* arg, const char* val)
{
if(strchr(val,'.')!=0) goto ABORT;
if(strchr(val,'/')!=0) goto ABORT;
return;
ABORT:
printf("invalid value for argument '%s': %s\n", arg, val);
abort();
}
void checkNumber(const char* arg, const char* val)
{
char* endptr = NULL;
/* check first if value starts with digit */
if(!isdigit(*val)) goto ABORT;
/* try conversion and check errno */
errno = 0;
strtol(val,&endptr,10);
if(errno != 0) goto ABORT;
/* check if conversion ended prematurely */
if(strlen(val)!=(endptr-val)) goto ABORT;
return;
ABORT:
printf("invalid value for argument '%s': %s\n", arg, val);
abort();
}
void help()
{
printf("usage: intel-power-manager-helper options\n");
printf(" -h --help print this text and exit\n");
printf(" -c --cpu N toggle CPU state for CPU N\n");
printf(" -g --gpu N select GPU N\n");
printf(" -l --min N set minimum GPU clock to N (requires -g)\n");
printf(" -u --max N set maximum GPU clock to N (requires -g)\n");
printf(" -s --bst N set boost GPU clock to N (requires -g)\n");
printf(" -b --brightness N set brightness to N\n");
}
int main( int argc, char** argv)
{
uid_t euid = geteuid();
if(euid != 0) {
printf("intel-power-control-helper: insufficient privileges\n");
return 1;
}
const char* minfn = "gt_min_freq_mhz";
const char* maxfn = "gt_max_freq_mhz";
const char* bstfn = "gt_boost_freq_mhz";
char cpu[bufsize+1], gpu[bufsize+1], min[bufsize+1], max[bufsize+1],
bst[bufsize+1], bn[bufsize+1];
memset( cpu, 0, bufsize+1);
memset( gpu, 0, bufsize+1);
memset( min, 0, bufsize+1);
memset( max, 0, bufsize+1);
memset( bst, 0, bufsize+1);
memset( bn, 0, bufsize+1);
int setFreq = 0;
int c;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
{"cpu", required_argument, 0, 'c'},
{"gpu", required_argument, 0, 'g'},
{"min", required_argument, 0, 'l'},
{"max", required_argument, 0, 'u'},
{"bst", required_argument, 0, 's'},
{"brightness", required_argument, 0, 'b'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "c:g:l:u:s:b:h",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'c':
#ifdef DEBUG
printf("cpu: %s\n", optarg);
#endif
strncpy(cpu, optarg, bufsize);
checkPath("cpu",cpu);
if( toggleCPU( cpu) != 0) abort();
break;
case 'g':
#ifdef DEBUG
printf("gpu: %s\n", optarg);
#endif
strncpy(gpu, optarg, bufsize);
checkPath("gpu",gpu);
break;
case 'l':
#ifdef DEBUG
printf("minimum: %s\n", optarg);
#endif
strncpy(min, optarg, bufsize);
checkNumber("min", min);
setFreq = 1;
break;
case 'u':
#ifdef DEBUG
printf("maximum: %s\n", optarg);
#endif
strncpy(max, optarg, bufsize);
checkNumber("max", max);
setFreq = 1;
break;
case 's':
#ifdef DEBUG
printf("boost: %s\n", optarg);
#endif
strncpy(bst, optarg, bufsize);
checkNumber("bst", bst);
setFreq = 1;
break;
case 'b':
#ifdef DEBUG
printf("set brightness: %s\n", optarg);
#endif
strncpy(bn, optarg, bufsize);
checkNumber("brightness", bn);
if( setBrightness(bn) != 0) abort();
break;
case 'h':
help();
exit(0);
break;
case '?':
abort();
break;
default:
#ifdef DEBUG
printf("Unknown command line flag '%c'\n", c);
#endif
abort();
}
}
if(setFreq)
{
if( !*gpu )
{
printf( "Required argument missing: -g/--gpu\n");
abort();
}
if( *min && setMHz( gpu, minfn, min) != 0) abort();
if( *max && setMHz( gpu, maxfn, max) != 0) abort();
if( *bst && setMHz( gpu, bstfn, bst) != 0) abort();
exit(0);
}
return 0;
}