aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPA4WDH2023-05-21 11:01:50 +0200
committerPA4WDH2023-05-21 11:01:50 +0200
commitda1b103a50732804bec48429523011322eb1acf5 (patch)
tree1f1c73a56393a2b151f7751fc603ab017a05de71
parentAdd input validation (diff)
downloadcputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.tar.gz
cputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.tar.bz2
cputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.zip
Improve error handling
-rw-r--r--cpufreq.c6
-rw-r--r--cputemp2maxfreq.c30
-rw-r--r--failsafe.c15
3 files changed, 39 insertions, 12 deletions
diff --git a/cpufreq.c b/cpufreq.c
index d9e0e4e..75eb774 100644
--- a/cpufreq.c
+++ b/cpufreq.c
@@ -20,6 +20,7 @@ int cpufreq_set_str(char *parameter,char *value,int checkdelay)
{
int fd;
char sysfs_file[128];
+ int done=0;
DEBUG1_CPUFREQ("Set %s to %s\n",parameter,value)
@@ -66,11 +67,14 @@ int cpufreq_set_str(char *parameter,char *value,int checkdelay)
DEBUG1_CPUFREQ("Failed to set %s\n",d->d_name);
return -1;
}
+ done++;
}
bpos+=d->d_reclen;
}
}
close(fd);
+
+ return done;
}
int cpufreq_set_long_int(char *parameter,long int value,int checkdelay)
@@ -78,5 +82,5 @@ int cpufreq_set_long_int(char *parameter,long int value,int checkdelay)
char buf[255];
snprintf(buf,255,"%ld",value);
- cpufreq_set_str(parameter,buf,checkdelay);
+ return cpufreq_set_str(parameter,buf,checkdelay);
}
diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c
index 6ea71bf..00d8f4f 100644
--- a/cputemp2maxfreq.c
+++ b/cputemp2maxfreq.c
@@ -57,7 +57,7 @@ int main()
}
// Set the governor
- if (cpufreq_set_str("scaling_governor",config.governor,0)!=0)
+ if (cpufreq_set_str("scaling_governor",config.governor,0)<0)
{
printf("Failed to set governor, error %d (%s).\n",errno,strerror(errno));
@@ -84,24 +84,48 @@ int main()
DEBUG1_MAIN("Data: %ld %ld %ld %ld %ld\n",cpudata.cur_temp,config.max_temp,cpudata.max_freq,cpudata.scale_max,cpudata.cur_freq);
+// Check if we should increase
if ((cpudata.cur_temp<config.max_temp) && (cpudata.scale_max<cpudata.max_freq))
{
diff=(config.max_temp-cpudata.cur_temp)/1000;
newfreq=cpudata.scale_max+(config.freq_step*diff);
if (newfreq>cpudata.max_freq) newfreq=cpudata.max_freq;
DEBUG1_MAIN("Increase to %ld\n",newfreq);
- cpufreq_set_long_int("scaling_max_freq",newfreq,100);
+
+// Set new value and validate
+ if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
+ {
+ printf("Failed to set scaling_max_freq, exiting.\n");
+ failsafe(1);
+ }
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
+ if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
+ {
+ printf("Invalid scale_max_freq reported, exiting,\n");
+ failsafe(1);
+ }
}
+// Check if we should decrease
if ((cpudata.cur_temp>config.max_temp) && (cpudata.scale_max>cpudata.min_freq))
{
diff=(cpudata.cur_temp-config.max_temp)/1000;
newfreq=cpudata.scale_max-(config.freq_step*diff);
if (newfreq<cpudata.min_freq) newfreq=cpudata.min_freq;
DEBUG1_MAIN("Decrease to %ld\n",newfreq);
- cpufreq_set_long_int("scaling_max_freq",newfreq,100);
+
+// Set new value and validate
+ if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
+ {
+ printf("Failed to set scaling_max_freq, exiting.\n");
+ failsafe(1);
+ }
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
+ if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
+ {
+ printf("Invalid scale_max_freq reported, exiting,\n");
+ failsafe(1);
+ }
}
sleep(config.interval);
diff --git a/failsafe.c b/failsafe.c
index 3b9fe48..fec00e8 100644
--- a/failsafe.c
+++ b/failsafe.c
@@ -10,24 +10,23 @@ extern struct s_config config;
void failsafe(int code)
{
- printf("Failed to set governor, error %d (%s).\n",errno,strerror(errno));
-
-// We failed to set the governor, try to fail safe. First try the CPU minimum
-// frequency, if that doesn't work out try the fallback frequency
- if (cpufreq_set_long_int("scaling_max_freq",cpudata.min_freq,100)==0)
+// First try to set the CPU to it's minimum frequency
+ if (cpufreq_set_long_int("scaling_max_freq",cpudata.min_freq,100)>0)
{
- printf("Set scaling frequency to CPU's minimum frequency.");
+ printf("Set scaling frequency to CPU's minimum frequency.\n");
exit(code);
}
printf("Failed to set scaling frequency to CPU's minimum frequency, error: %d (%s).\n",errno,strerror(errno));
- if (cpufreq_set_long_int("scaling_max_freq",config.fallback_freq,100)==0)
+// If that failed, try the fallback frequency
+ if (cpufreq_set_long_int("scaling_max_freq",config.fallback_freq,100)>0)
{
- printf("Set scaling frequency to fallback frequency.");
+ printf("Set scaling frequency to fallback frequency.\n");
exit(code);
}
printf("Failed to set scaling frequency to fallback frequency, error: %d (%s).\n",errno,strerror(errno));
+// Everything failed, issue a warning
printf("All safety measures failed, watch out not to fry your hardware.\n");
exit(code);
}