raster pushed a commit to branch master.

http://git.enlightenment.org/core/enlightenment.git/commit/?id=7aaf8f4feba554197d7904194cd9b810c0d49a1e

commit 7aaf8f4feba554197d7904194cd9b810c0d49a1e
Author: Carsten Haitzler (Rasterman) <ras...@rasterman.com>
Date:   Tue Jan 14 15:31:00 2020 +0000

    cpufreq - move to use the new e_system cpufreq support from own suid exe
---
 src/modules/cpufreq/e_mod_main.c | 158 +++++++++++++-----------------
 src/modules/cpufreq/e_mod_main.h |   1 -
 src/modules/cpufreq/freqset.c    | 207 ---------------------------------------
 src/modules/cpufreq/meson.build  |  12 ---
 4 files changed, 66 insertions(+), 312 deletions(-)

diff --git a/src/modules/cpufreq/e_mod_main.c b/src/modules/cpufreq/e_mod_main.c
index b66b365d8..f594ba8f3 100644
--- a/src/modules/cpufreq/e_mod_main.c
+++ b/src/modules/cpufreq/e_mod_main.c
@@ -486,41 +486,74 @@ _menu_cb_post(void *data, E_Menu *m EINA_UNUSED)
    cpufreq_config->menu_powersave = NULL;
 }
 
-void
-_cpufreq_set_governor(const char *governor)
+static Eina_Bool _response = EINA_FALSE;
+
+static void
+_cpufreq_error(const char *params)
 {
-   char buf[4096];
-   int ret;
-   struct stat st;
+   E_Dialog *dia;
+
+   if (!((params) && (!strcmp(params, "err")))) return;
+
+   if (!(dia = e_dialog_new(NULL, "E", "_e_mod_cpufreq_error_setfreq")))
+     return;
+   e_dialog_title_set(dia, "Enlightenment Cpufreq Module");
+   e_dialog_icon_set(dia, "enlightenment", 64);
+   e_dialog_text_set(dia, _("There was an error trying to modify CPU<br>"
+                            "frequency control parameters."));
+   e_dialog_button_add(dia, _("OK"), NULL, NULL, NULL);
+   elm_win_center(dia->win, 1, 1);
+   e_dialog_show(dia);
+}
 
-   if (stat(cpufreq_config->set_exe_path, &st) < 0) return;
+static void
+_cb_cpufreq_governor(void *data EINA_UNUSED, const char *params)
+{
+   _cpufreq_error(params);
+}
 
-   snprintf(buf, sizeof(buf),
-            "%s %s %s", cpufreq_config->set_exe_path, "governor", governor);
-   ret = system(buf);
-   if (ret != 0)
-     {
-        E_Dialog *dia;
+static void
+_cb_cpufreq_freq(void *data EINA_UNUSED, const char *params)
+{
+   _cpufreq_error(params);
+}
 
-        if (!(dia = e_dialog_new(NULL, "E", "_e_mod_cpufreq_error_setfreq")))
-          return;
-        e_dialog_title_set(dia, "Enlightenment Cpufreq Module");
-        e_dialog_icon_set(dia, "enlightenment", 64);
-        e_dialog_text_set(dia, _("There was an error trying to set the<ps/>"
-                                 "cpu frequency governor via the module's<ps/>"
-                                 "setfreq utility."));
-        e_dialog_button_add(dia, _("OK"), NULL, NULL, NULL);
-        elm_win_center(dia->win, 1, 1);
-        e_dialog_show(dia);
-     }
+static void
+_cb_cpufreq_pstate(void *data EINA_UNUSED, const char *params)
+{
+   _cpufreq_error(params);
 }
 
 static void
-_cpufreq_set_frequency(int frequency)
+_cpufreq_response_init(void)
 {
-   char buf[4096];
-   int ret;
+   if (_response) return;
+   _response = EINA_TRUE;
+   e_system_handler_add("cpufreq-governor", _cb_cpufreq_governor, NULL);
+   e_system_handler_add("cpufreq-freq", _cb_cpufreq_freq, NULL);
+   e_system_handler_add("pstate", _cb_cpufreq_pstate, NULL);
+}
+
+static void
+_cpufreq_response_shutdown(void)
+{
+   if (!_response) return;
+   _response = EINA_FALSE;
+   e_system_handler_add("cpufreq-governor", _cb_cpufreq_governor, NULL);
+   e_system_handler_add("cpufreq-freq", _cb_cpufreq_freq, NULL);
+   e_system_handler_add("pstate", _cb_cpufreq_pstate, NULL);
+}
+
+void
+_cpufreq_set_governor(const char *governor)
+{
+   _cpufreq_response_init();
+   e_system_send("cpufreq-governor", "%s", governor);
+}
 
+static void
+_cpufreq_set_frequency(int frequency)
+{
 #if defined(__FreeBSD__) || defined(__DragonFly__)
    frequency /= 1000;
 #endif
@@ -546,25 +579,8 @@ _cpufreq_set_frequency(int frequency)
    /* OpenBSD doesn't have governors */
    _cpufreq_set_governor("userspace");
 #endif
-
-   snprintf(buf, sizeof(buf),
-            "%s %s %i", cpufreq_config->set_exe_path, "frequency", frequency);
-   ret = system(buf);
-   if (ret != 0)
-     {
-        E_Dialog *dia;
-
-        if (!(dia = e_dialog_new(NULL, "E", "_e_mod_cpufreq_error_setfreq")))
-          return;
-        e_dialog_title_set(dia, "Enlightenment Cpufreq Module");
-        e_dialog_icon_set(dia, "enlightenment", 64);
-        e_dialog_text_set(dia, _("There was an error trying to set the<ps/>"
-                                 "cpu frequency setting via the module's<ps/>"
-                                 "setfreq utility."));
-        e_dialog_button_add(dia, _("OK"), NULL, NULL, NULL);
-        elm_win_center(dia->win, 1, 1);
-        e_dialog_show(dia);
-     }
+   _cpufreq_response_init();
+   e_system_send("cpufreq-freq", "%i", frequency);
 }
 
 void
@@ -574,25 +590,8 @@ _cpufreq_set_pstate(int min, int max)
    (void) min;
    (void) max;
 #else
-   char buf[4096];
-   snprintf(buf, sizeof(buf),
-            "%s %s %i %i %i", cpufreq_config->set_exe_path, "pstate", min, 
max, cpufreq_config->status->pstate_turbo);
-   int ret = system(buf);
-   if (ret != 0)
-     {
-        E_Dialog *dia;
-
-        if (!(dia = e_dialog_new(NULL, "E", "_e_mod_cpufreq_error_setfreq")))
-          return;
-        e_dialog_title_set(dia, "Enlightenment Cpufreq Module");
-        e_dialog_icon_set(dia, "enlightenment", 64);
-        e_dialog_text_set(dia, _("There was an error trying to set the<ps/>"
-                                 "cpu power state setting via the 
module's<ps/>"
-                                 "setfreq utility."));
-        e_dialog_button_add(dia, _("OK"), NULL, NULL, NULL);
-        elm_win_center(dia->win, 1, 1);
-        e_dialog_show(dia);
-     }
+   _cpufreq_response_init();
+   e_system_send("cpufreq-pstate", "%i %i %i", min, max, 
cpufreq_config->status->pstate_turbo);
 #endif
 }
 
@@ -1410,7 +1409,6 @@ E_API E_Module_Api e_modapi =
 E_API void *
 e_modapi_init(E_Module *m)
 {
-   struct stat st;
    char buf[PATH_MAX];
    Eina_List *l;
 
@@ -1452,30 +1450,6 @@ e_modapi_init(E_Module *m)
      }
    E_CONFIG_LIMIT(cpufreq_config->poll_interval, 1, 1024);
 
-   snprintf(buf, sizeof(buf), "%s/%s/freqset",
-            e_module_dir_get(m), MODULE_ARCH);
-   cpufreq_config->set_exe_path = strdup(buf);
-   
-   if (stat(buf, &st) < 0)
-     {
-        e_util_dialog_show(_("Cpufreq Error"),
-                           _("The freqset binary in the cpufreq module<ps/>"
-                             "directory cannot be found (stat failed)"));
-     }
-   else if ((st.st_uid != 0) ||
-            ((st.st_mode & (S_ISUID)) != (S_ISUID)) ||
-            ((st.st_mode & (S_IXOTH)) != (S_IXOTH)))
-     {
-        e_util_dialog_show(_("Cpufreq Permissions Error"),
-                           _("The freqset binary in the cpufreq module<ps/>"
-                             "is not owned by root or does not have the<ps/>"
-                             "setuid bit set. Please ensure this is the<ps/>"
-                             "case. For example:<ps/>"
-                             "<ps/>"
-                             "sudo chown root %s<ps/>"
-                             "sudo chmod u+s,a+x %s<ps/>"),
-                           buf, buf);
-     }
    cpufreq_config->status = _cpufreq_status_new();
 
    _cpufreq_status_check_available(cpufreq_config->status);
@@ -1508,6 +1482,7 @@ e_modapi_init(E_Module *m)
 E_API int
 e_modapi_shutdown(E_Module *m EINA_UNUSED)
 {
+   _cpufreq_response_shutdown();
    e_configure_registry_item_del("advanced/cpufreq");
    e_configure_registry_category_del("advanced");
    
@@ -1551,11 +1526,10 @@ e_modapi_shutdown(E_Module *m EINA_UNUSED)
    if (cpufreq_config->governor)
      eina_stringshare_del(cpufreq_config->governor);
    if (cpufreq_config->status) _cpufreq_status_free(cpufreq_config->status);
-   E_FREE(cpufreq_config->set_exe_path);
-   
+
    if (cpufreq_config->config_dialog)
      e_object_del(E_OBJECT(cpufreq_config->config_dialog));
-   
+
    free(cpufreq_config);
    cpufreq_config = NULL;
    E_CONFIG_DD_FREE(conf_edd);
diff --git a/src/modules/cpufreq/e_mod_main.h b/src/modules/cpufreq/e_mod_main.h
index 453cf5e0f..209819445 100644
--- a/src/modules/cpufreq/e_mod_main.h
+++ b/src/modules/cpufreq/e_mod_main.h
@@ -48,7 +48,6 @@ struct _Config
    E_Menu       *menu_pstate1;
    E_Menu       *menu_pstate2;
    Cpu_Status   *status;
-   char         *set_exe_path;
    Ecore_Thread *frequency_check_thread;
    Ecore_Event_Handler *handler;
    E_Config_Dialog *config_dialog;
diff --git a/src/modules/cpufreq/freqset.c b/src/modules/cpufreq/freqset.c
deleted file mode 100644
index 5c848be51..000000000
--- a/src/modules/cpufreq/freqset.c
+++ /dev/null
@@ -1,207 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <string.h>
-
-#ifdef __FreeBSD__
-# include <sys/sysctl.h>
-#endif
-
-#ifdef __OpenBSD__
-# include <sys/param.h>
-# include <sys/resource.h>
-# include <sys/sysctl.h>
-#endif
-
-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, "\tfreqset <frequency|governor> 
<freq-level|governor-name>\n");
-        fprintf(stderr, "\tfreqset <pstate> <min> <max> <turbo>\n");
-        return 1;
-     }
-
-   if (seteuid(0))
-     {
-        fprintf(stderr, "Unable to assume root privileges\n");
-        return 1;
-     }
-
-#if defined __OpenBSD__
-   if (!strcmp(argv[1], "frequency"))
-     {
-        int mib[] = {CTL_HW, HW_SETPERF};
-        int new_perf = atoi(argv[2]);
-        size_t len = sizeof(new_perf);
-
-        if (sysctl(mib, 2, NULL, 0, &new_perf, len) == -1)
-          {
-             return 1;
-          }
-
-        return 0;
-     }
-   else
-     {
-        fprintf(stderr, "Unknown command (%s %s).\n", argv[1], argv[2]);
-        return 1;
-     }
-#elif defined __FreeBSD__
-   if (!strcmp(argv[1], "frequency"))
-     {
-        int newfreq = atoi(argv[2]);
-        if (sysctlbyname("dev.cpu.0.freq", NULL, NULL, &newfreq, 
sizeof(newfreq)) == -1)
-          {
-             fprintf(stderr, "Unable to open frequency interface for 
writing.\n");
-             return 1;
-          }
-
-        return 0;
-     }
-   else if (!strcmp(argv[1], "governor"))
-     {
-        fprintf(stderr, "Governors not (yet) implemented on FreeBSD.\n");
-        return 0;
-     }
-   else if (!strcmp(argv[1], "pstate"))
-     {
-        fprintf(stderr, "Pstates not (yet) implemented on FreeBSD.\n");
-        return 0;
-     }
-   else
-     {
-        fprintf(stderr, "Unknown command.\n");
-        return 1;
-     }
-#else
-   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;
-     }
-#endif
-
-   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;
-}
diff --git a/src/modules/cpufreq/meson.build b/src/modules/cpufreq/meson.build
index adf5bae23..b42668d41 100644
--- a/src/modules/cpufreq/meson.build
+++ b/src/modules/cpufreq/meson.build
@@ -3,15 +3,3 @@ src = files(
   'e_mod_config.c',
   'e_mod_main.h'
 )
-
-if get_option(m) == true
-  executable('freqset',
-             'freqset.c',
-             c_args     : suid_cflags,
-             link_args  : suid_ldflags,
-             install_dir: _dir_bin,
-             install    : true
-            )
-  suid_exes += join_paths(_dir_bin, 'freqset')
-endif
-

-- 


Reply via email to