aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPA4WDH2024-07-28 15:13:52 +0200
committerPA4WDH2024-07-28 15:13:52 +0200
commitab2d1b31b715f817ea1d16c67743b54fb5f79107 (patch)
treef1c80e89e3dbe4ffa7b3f6c5df136e47c21e9340
parentAdd autodetection for temperature input (diff)
downloadcputemp2maxfreq-ab2d1b31b715f817ea1d16c67743b54fb5f79107.tar.gz
cputemp2maxfreq-ab2d1b31b715f817ea1d16c67743b54fb5f79107.tar.bz2
cputemp2maxfreq-ab2d1b31b715f817ea1d16c67743b54fb5f79107.zip
Rewrite cpufreq_set_str to use opendir/readdir/closedir
-rw-r--r--cpufreq.c69
1 files changed, 21 insertions, 48 deletions
diff --git a/cpufreq.c b/cpufreq.c
index df21b36..c683d6c 100644
--- a/cpufreq.c
+++ b/cpufreq.c
@@ -3,6 +3,8 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
+#include <dirent.h>
+#include <sys/types.h>
#include "debug.h"
#include "sysfs.h"
@@ -24,70 +26,41 @@ int cpufreq_is_cpu(char *name)
// Set a cpufreq parameter to a value
int cpufreq_set_str(char *parameter,char *value,long int checkdelay)
{
- int fd;
+ DIR *cpudir;
+ struct dirent *cpu_dirent;
char sysfs_file[128];
int done=0;
DEBUG1_CPUFREQ("Set %s to %s\n",parameter,value)
- char buf[DIR_BUF_SIZE];
- long nread;
-
- struct linux_dirent {
- unsigned long d_ino;
- off_t d_off;
- unsigned short d_reclen;
- char d_name[];
- };
-
- struct linux_dirent *d;
- size_t bpos;
-
// Open the CPU directory in sysfs
- fd=open("/sys/devices/system/cpu",O_RDONLY|O_DIRECTORY);
- if (fd==-1)
+ cpudir=opendir("/sys/devices/system/cpu");
+ if (cpudir==NULL)
{
DEBUG1_CPUFREQ("Unable to find CPU's\n");
return -1;
}
- while(1)
+ cpu_dirent=readdir(cpudir);
+ while(cpu_dirent!=NULL)
{
-// Iterate over SYS_getdents calls
- nread=syscall(SYS_getdents,fd,buf,DIR_BUF_SIZE);
-
-// If we can't read return an error
- if (nread==-1)
- {
- DEBUG1_CPUFREQ("Unable to find CPU's\n");
- close(fd);
- return -1;
- }
-
-// If there are no entries left we're done
- if (nread==0) break;
-
-// Iterate over returned file info
- for(bpos=0;bpos<nread;)
- {
- d=(struct linux_dirent *)(buf+bpos);
-
// If it's a CPU, set our parameter
- if (cpufreq_is_cpu(d->d_name))
+ if (cpufreq_is_cpu(cpu_dirent->d_name))
+ {
+ DEBUG2_CPUFREQ("Found CPU %s\n",cpu_dirent->d_name);
+ snprintf(sysfs_file,128,"/sys/devices/system/cpu/%s/cpufreq/%s",cpu_dirent->d_name,parameter);
+ if (sysfs_write_str(sysfs_file,value,checkdelay)!=0)
{
- DEBUG2_CPUFREQ("Found CPU %s\n",d->d_name);
- snprintf(sysfs_file,128,"/sys/devices/system/cpu/%s/cpufreq/%s",d->d_name,parameter);
- if (sysfs_write_str(sysfs_file,value,checkdelay)!=0)
- {
- DEBUG1_CPUFREQ("Failed to set %s\n",d->d_name);
- return -1;
- }
- done++;
+ DEBUG1_CPUFREQ("Failed to set %s\n",cpu_dirent->d_name);
+ closedir(cpudir);
+ return -1;
}
- bpos+=d->d_reclen;
- }
+ done++;
+ }
+
+ cpu_dirent=readdir(cpudir);
}
- close(fd);
+ closedir(cpudir);
return done;
}