diff options
author | PA4WDH | 2023-05-21 11:01:50 +0200 |
---|---|---|
committer | PA4WDH | 2023-05-21 11:01:50 +0200 |
commit | da1b103a50732804bec48429523011322eb1acf5 (patch) | |
tree | 1f1c73a56393a2b151f7751fc603ab017a05de71 /cputemp2maxfreq.c | |
parent | Add input validation (diff) | |
download | cputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.tar.gz cputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.tar.bz2 cputemp2maxfreq-da1b103a50732804bec48429523011322eb1acf5.zip |
Improve error handling
Diffstat (limited to 'cputemp2maxfreq.c')
-rw-r--r-- | cputemp2maxfreq.c | 30 |
1 files changed, 27 insertions, 3 deletions
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); |