sysinfo - cpuclock - migrate from custom suid to e_system like cpufreq

This commit is contained in:
Carsten Haitzler 2020-01-14 16:01:43 +00:00
parent 7aaf8f4feb
commit be8792a2ee
4 changed files with 4 additions and 269 deletions

View File

@ -74,66 +74,22 @@ _cpuclock_cb_sort(const void *item1, const void *item2)
void
_cpuclock_set_governor(const char *governor)
{
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
return;
#endif
char buf[4096 + 100], exe[4096];
struct stat st;
snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
if (stat(exe, &st) < 0) return;
snprintf(buf, sizeof(buf),
"%s %s %s", exe, "governor", governor);
if (system(buf) != 0)
ERR("Error code from trying to run \"%s\"", buf);
e_system_send("cpufreq-governor", "%s", governor);
}
void
_cpuclock_set_frequency(int frequency)
{
char buf[4096 + 100], exe[4096];
struct stat st;
#if defined(__FreeBSD__) || defined(__DragonFly__)
frequency /= 1000;
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
snprintf(exe, sizeof(exe), "%s/%s/cpuclock_sysctl",
e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
if (stat(exe, &st) < 0) return;
snprintf(buf, sizeof(buf), "%s %d", exe, frequency);
if (system(buf) != 0)
ERR("Error code from trying to run \"%s\"", buf);
#else
snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
if (stat(exe, &st) < 0) return;
snprintf(buf, sizeof(buf),
"%s %s %i", exe, "frequency", frequency);
if (system(buf) != 0)
ERR("Error code from trying to run \"%s\"", buf);
#endif
e_system_send("cpufreq-freq", "%i", frequency);
}
void
_cpuclock_set_pstate(int min, int max, int turbo)
{
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
return;
#endif
char buf[4096 + 100], exe[4096];
struct stat st;
snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
if (stat(exe, &st) < 0) return;
snprintf(buf, sizeof(buf),
"%s %s %i %i %i", exe, "pstate", min, max, turbo);
if (system(buf) != 0)
ERR("Error code from trying to run \"%s\"", buf);
e_system_send("cpufreq-pstate", "%i %i %i", min, max, turbo);
}
static void

View File

@ -1,46 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#if defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/sysctl.h>
#endif
#if defined(__OpenBSD__)
# include <sys/param.h>
# include <sys/resource.h>
# include <sys/sysctl.h>
#endif
#if defined(__OpenBSD__)
int
_cpuclock_sysctl_frequency(int new_perf)
{
int mib[] = {CTL_HW, HW_SETPERF};
size_t len = sizeof(new_perf);
if (sysctl(mib, 2, NULL, 0, &new_perf, len) == -1)
return 1;
return 0;
}
#elif defined(__FreeBSD__) || defined(__DragonFly__)
int
_cpuclock_sysctl_frequency(int new_perf)
{
if (sysctlbyname("dev.cpu.0.freq", NULL, NULL, &new_perf, sizeof(new_perf)) == -1)
return 1;
return 0;
}
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
int main(int argc, char **argv)
{
if (argc != 2)
exit(1 << 0);
return _cpuclock_sysctl_frequency(atoi(argv[1]));
}
#endif

View File

@ -1,150 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
static int sys_cpu_setall(const char *control, const char *value);
static int sys_cpufreq_set(const char *control, const char *value);
static int sys_cpu_pstate(int min, int max, int turbo);
int
main(int argc, char *argv[])
{
if (argc < 3)
{
fprintf(stderr, "Invalid command. Syntax:\n");
fprintf(stderr, "\tcpuclock_sysfs <frequency|governor> <freq-level|governor-name>\n");
fprintf(stderr, "\tcpuclock_sysfs <pstate> <min> <max> <turbo>\n");
return 1;
}
if (seteuid(0))
{
fprintf(stderr, "%s %s\n", argv[0], argv[1]);
fprintf(stderr, "Unable to assume root privileges\n");
return 1;
}
if (!strcmp(argv[1], "frequency"))
{
if (sys_cpu_setall("scaling_setspeed", argv[2]) == 0)
{
fprintf(stderr, "Unable to open frequency interface for writing.\n");
return 1;
}
return 0;
}
else if (!strcmp(argv[1], "governor"))
{
if (sys_cpu_setall("scaling_governor", argv[2]) == 0)
{
fprintf(stderr, "Unable to open governor interface for writing.\n");
return 1;
}
if (!strcmp(argv[2], "ondemand"))
sys_cpufreq_set("ondemand/ignore_nice_load", "0");
else if (!strcmp(argv[2], "conservative"))
sys_cpufreq_set("conservative/ignore_nice_load", "0");
return 0;
}
else if (!strcmp(argv[1], "pstate"))
{
int min, max, turbo;
if (argc < 5)
{
fprintf(stderr, "Invalid number of arguments.\n");
return 1;
}
min = atoi(argv[2]);
max = atoi(argv[3]);
turbo = atoi(argv[4]);
if ((min < 0) || (min > 100) ||
(max < 0) || (max > 100) ||
(turbo < 0) || (turbo > 1))
{
fprintf(stderr, "Invalid pstate values.\n");
return 1;
}
sys_cpu_pstate(min, max, turbo);
return 0;
}
else
{
fprintf(stderr, "Unknown command.\n");
return 1;
}
return -1;
}
static int
sys_cpu_setall(const char *control, const char *value)
{
int num = 0;
char filename[4096];
FILE *f;
while (1)
{
snprintf(filename, sizeof(filename), "/sys/devices/system/cpu/cpu%i/cpufreq/%s", num, control);
f = fopen(filename, "w");
if (!f)
{
return num;
}
fprintf(f, "%s\n", value);
fclose(f);
num++;
}
return -1;
}
static int
sys_cpufreq_set(const char *control, const char *value)
{
char filename[4096];
FILE *f;
snprintf(filename, sizeof(filename), "/sys/devices/system/cpu/cpufreq/%s", control);
f = fopen(filename, "w");
if (!f)
{
if (sys_cpu_setall(control, value) > 0)
return 1;
else
return -1;
}
fprintf(f, "%s\n", value);
fclose(f);
return 1;
}
static int
sys_cpu_pstate(int min, int max, int turbo)
{
FILE *f;
f = fopen("/sys/devices/system/cpu/intel_pstate/min_perf_pct", "w");
if (!f) return 0;
fprintf(f, "%i\n", min);
fclose(f);
f = fopen("/sys/devices/system/cpu/intel_pstate/max_perf_pct", "w");
if (!f) return 0;
fprintf(f, "%i\n", max);
fclose(f);
f = fopen("/sys/devices/system/cpu/intel_pstate/no_turbo", "w");
if (!f) return 0;
fprintf(f, "%i\n", turbo ? 0 : 1);
fclose(f);
return 1;
}

View File

@ -34,36 +34,11 @@ elif host_machine.system().contains('bsd') == true
src += files(
'batman/batman_sysctl.c',
'thermal/thermal_sysctl.c',
'cpuclock/cpuclock_sysctl.c',
'netstatus/netstatus_sysctl.c',
'cpumonitor/cpumonitor_sysctl.c',
'memusage/memusage_sysctl.c')
else
src += files(
'batman/batman_upower.c',
'thermal/thermal_sysctl.c',
'cpuclock/cpuclock_sysctl.c')
endif
if get_option(m) == true
executable('cpuclock_sysfs',
'cpuclock/cpuclock_sysfs.c',
dependencies: dep_crypt,
c_args : suid_cflags,
link_args : suid_ldflags,
install_dir : _dir_bin,
install : true
)
suid_exes += join_paths(_dir_bin, 'cpuclock_sysfs')
endif
if get_option(m) == true and host_os.startswith('freebsd') or host_os.startswith('dragonfly')
executable('cpuclock_sysctl',
'cpuclock/cpuclock_sysctl.c',
c_args : suid_cflags,
link_args : suid_ldflags,
install_dir : _dir_bin,
install : true
)
suid_exes += join_paths(_dir_bin, 'cpuclock_sysctl')
'thermal/thermal_sysctl.c')
endif