This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository enlightenment.

View the commit online.

commit ff3bf99f8909dc2973ad48733d04d2c3ec30822d
Author: Carsten Haitzler <[email protected]>
AuthorDate: Tue Feb 10 19:44:12 2026 +0000

    pid nice/pri - make this go up and down without cap_sys_nice
    
    focused app should be nice 2, other apps and their children, nice 3
    (so lower priority) assuming the default config for application
    priority of 3 - focused app and its children always 1 higher).
---
 src/bin/e_utils.c              | 149 ++---------------------------------------
 src/bin/system/e_system.h      |   3 +
 src/bin/system/e_system_main.c |   2 +
 src/bin/system/e_system_nice.c |  85 +++++++++++++++++++++++
 src/bin/system/meson.build     |   1 +
 5 files changed, 95 insertions(+), 145 deletions(-)

diff --git a/src/bin/e_utils.c b/src/bin/e_utils.c
index ce565b6f1..4b16fbb7e 100644
--- a/src/bin/e_utils.c
+++ b/src/bin/e_utils.c
@@ -1589,159 +1589,18 @@ e_username_get(void)
    return "";
 }
 
-typedef struct
-{
-   int        pid;
-   int        pri_set, pri_adj;
-   int        pri_only_filter;
-   Eina_Bool  do_adj : 1;
-   Eina_Bool  do_adj_children : 1;
-   Eina_Bool  do_children : 1;
-} E_Pri_Set_Adj_Data;
-
-static void
-_pri_adj(int pid, int pri_set, int pri_adj, int pri_only_filter,
-         Eina_Bool do_adj, Eina_Bool do_adj_children, Eina_Bool do_children)
-{
-   Eina_List *files;
-   char *file, buf[PATH_MAX];
-   int pid2, ppid, newpri, ret;
-   FILE *f;
-
-   newpri = getpriority(PRIO_PROCESS, pid) + pri_adj;
-   if ((pri_only_filter == -99) || (pri_only_filter != newpri))
-     {
-        if (do_adj) newpri += pri_adj;
-        else        newpri = pri_set;
-        if (newpri > 19) newpri = 19;
-//        printf("PRI: %i -> %i (adj=%i, adj_ch=%i ch=%i)\n", pid, newpri, do_adj, do_adj_children, do_children);
-        ret = setpriority(PRIO_PROCESS, pid, newpri);
-//        if (ret < 0) printf("PRI: ret = %i | %s\n", ret, strerror(errno));
-     }
-// shouldn't need to do this as default ionice class is "none" (0), and
-// this inherits io priority FROM nice level
-//        ioprio_set(IOPRIO_WHO_PROCESS, pid,
-//                   IOPRIO_PRIO_VALUE(2, 5));
-   if (do_children)
-     {
-// yes - this is /proc specific... so this may not work on some
-// os's - works on linux. too bad for others.
-        files = ecore_file_ls("/proc");
-        EINA_LIST_FREE(files, file)
-          {
-             if (isdigit(file[0]))
-               {
-                  snprintf(buf, sizeof(buf), "/proc/%s/stat", file);
-                  f = fopen(buf, "r");
-                  if (f)
-                    {
-                       pid2 = ppid = -1;
-                       ret = fscanf(f, "%i %*s %*s %i %*s", &pid2, &ppid);
-                       fclose(f);
-                       if (ret == 2)
-                         {
-                            if (ppid == pid)
-                              {
-                                 if (do_adj_children)
-                                   _pri_adj(pid2, pri_set, pri_adj,
-                                            pri_only_filter,
-                                            EINA_TRUE,
-                                            do_adj_children,
-                                            do_children);
-                                 else
-                                   _pri_adj(pid2, pri_set, pri_adj,
-                                            pri_only_filter,
-                                            do_adj,
-                                            do_adj_children,
-                                            do_children);
-                              }
-                         }
-                    }
-               }
-             free(file);
-          }
-     }
-}
-
-static void
-_e_pid_nice_priority_set_adjust_thread(void *data,
-                                       Ecore_Thread *eth EINA_UNUSED)
-{
-   E_Pri_Set_Adj_Data *dat = data;
-
-//   printf("PRI: --------\n");
-   _pri_adj(dat->pid, dat->pri_set, dat->pri_adj, dat->pri_only_filter,
-            dat->do_adj, dat->do_adj_children, dat->do_children);
-   free(dat);
-}
-
-static void
-_e_pid_nice_priority_set_adjust(int pid, int pri_set, int pri_adj,
-                                int pri_only_filter,
-                                Eina_Bool do_adj, Eina_Bool do_adj_children,
-                                Eina_Bool do_children)
-{
-   E_Pri_Set_Adj_Data *dat = calloc(1, sizeof(E_Pri_Set_Adj_Data));
-
-   if (!dat) return;
-   dat->pid             = pid;
-   dat->pri_set         = pri_set;
-   dat->pri_adj         = pri_adj;
-   dat->pri_only_filter = pri_only_filter;
-   dat->do_adj          = do_adj;
-   dat->do_adj_children = do_adj_children;
-   dat->do_children     = do_children;
-   ecore_thread_run(_e_pid_nice_priority_set_adjust_thread, NULL, NULL, dat);
-}
-
-static Eina_Bool
-_e_pid_nice_priority_lower_can(void)
-{
-#ifdef RLIMIT_NICE
-   static int checked = -1;
-   struct rlimit rlim;
-
-again:
-   if      (checked == 0) return EINA_TRUE;
-   else if (checked == 1) return EINA_TRUE;
-
-   checked = 1;
-   if (getrlimit(RLIMIT_NICE, &rlim) == 0)
-     {
-       if (rlim.rlim_max >= 20)
-         {
-            rlim.rlim_cur = 20;
-            setrlimit(RLIMIT_NICE, &rlim);
-         }
-     }
-   if (getrlimit(RLIMIT_NICE, &rlim) == 0)
-     {
-        // if we can't lower pri to at least 20 (nice 0 ...) then assume
-        // we can only raise nice level never lower it
-//        printf("PRI: getrlimit(RLIMIT_NUICE) === cur=%i max=%i\n", (int)rlim.rlim_cur, (int)rlim.rlim_max);
-        if (rlim.rlim_cur < 20) checked = 0;
-     }
-   goto again; // set checked now - try again
-#endif
-   return EINA_TRUE;
-}
-
 E_API void
 e_pid_nice_priority_fg(int pid)
 {
-   int pri = e_config->priority - 1;
+  int pri = e_config->priority - 1;
 
-   if (!_e_pid_nice_priority_lower_can()) pri = e_config->priority;
-   _e_pid_nice_priority_set_adjust(pid, pri, -1, -99, // only these procs
-                                   EINA_FALSE, EINA_FALSE, EINA_FALSE);
+  e_system_send("nice-set",  "%i set %i", pid, pri);
 }
 
 E_API void
 e_pid_nice_priority_bg(int pid)
 {
-   int pri = e_config->priority;
+  int pri = e_config->priority;
 
-   if (!_e_pid_nice_priority_lower_can()) pri = e_config->priority;
-   _e_pid_nice_priority_set_adjust(pid, pri, 1, -99, // only these procs
-                                   EINA_FALSE, EINA_FALSE, EINA_FALSE);
+  e_system_send("nice-set",  "%i set %i", pid, pri);
 }
diff --git a/src/bin/system/e_system.h b/src/bin/system/e_system.h
index b78cdb06d..b994c0984 100644
--- a/src/bin/system/e_system.h
+++ b/src/bin/system/e_system.h
@@ -132,6 +132,9 @@ void e_system_acpi_shutdown(void);
 void e_system_battery_init(void);
 void e_system_battery_shutdown(void);
 
+void e_system_nice_init(void);
+void e_system_nice_shutdown(void);
+
 extern Ecore_Exe *e_system_run(const char *exe);
 
 #endif
diff --git a/src/bin/system/e_system_main.c b/src/bin/system/e_system_main.c
index f2ac0abe4..073f4edda 100644
--- a/src/bin/system/e_system_main.c
+++ b/src/bin/system/e_system_main.c
@@ -175,6 +175,7 @@ main(int argc EINA_UNUSED, const char **argv EINA_UNUSED)
    CONF_INIT_CHECK("cpufreq",   e_system_cpufreq_init,   init_cpufreq);
    CONF_INIT_CHECK("acpi",      e_system_acpi_init,      init_acpi);
    CONF_INIT_CHECK("battery",   e_system_battery_init,   init_battery);
+   CONF_INIT_CHECK("nice",      e_system_nice_init,      init_nice);
 
    if (systems == 0)
      {
@@ -186,6 +187,7 @@ main(int argc EINA_UNUSED, const char **argv EINA_UNUSED)
 
    ecore_main_loop_begin();
 
+   CONF_SHUTDOWN(e_system_nice_shutdown,      init_nice);
    CONF_SHUTDOWN(e_system_battery_shutdown,   init_battery);
    CONF_SHUTDOWN(e_system_acpi_shutdown,      init_acpi);
    CONF_SHUTDOWN(e_system_cpufreq_shutdown,   init_cpufreq);
diff --git a/src/bin/system/e_system_nice.c b/src/bin/system/e_system_nice.c
new file mode 100644
index 000000000..bf9302300
--- /dev/null
+++ b/src/bin/system/e_system_nice.c
@@ -0,0 +1,85 @@
+#include "e_system.h"
+
+#include <linux/ioprio.h>
+
+typedef enum
+{
+  SET,
+  ADJ
+} Mode;
+
+static void
+_pri_adj(int pid, int pri, Mode mode)
+{
+  Eina_List *files;
+  char *file, buf[PATH_MAX];
+  int pid2, ppid, oldpri, newpri = 0, ret;
+  FILE *f;
+  int fd;
+  struct stat st;
+
+  oldpri = getpriority(PRIO_PROCESS, pid);
+  if      (mode == SET)  newpri = pri;
+  else if (mode == ADJ)  newpri = oldpri + pri;
+  if      (newpri >  19) newpri =  19; // lowest pri
+  else if (newpri < -20) newpri = -20; // highest pri
+  ret = setpriority(PRIO_PROCESS, pid, newpri);
+// shouldn't need to do this as default ionice class is "none" (0), and
+// this inherits io priority FROM nice level
+//  ioprio_set(IOPRIO_WHO_PROCESS, pid,
+//             IOPRIO_PRIO_VALUE(2, 5));
+  files = ecore_file_ls("/proc");
+  EINA_LIST_FREE(files, file)
+    {
+      if (isdigit(file[0]))
+        {
+          snprintf(buf, sizeof(buf), "/proc/%s/stat", file);
+          fd = open(buf, O_RDONLY);
+          if (fd >= 0)
+            {
+              if (fstat(fd, &st) == 0)
+                {
+                  if (st.st_uid == uid)
+                    {
+                      f = fdopen(fd, "r");
+                      if (f)
+                        {
+                          pid2 = ppid = -1;
+                          ret = fscanf(f, "%i %*s %*s %i %*s", &pid2, &ppid);
+                          fclose(f);
+                          close(fd);
+                          fd = -1;
+                          if ((ret == 2) && (ppid == pid))
+                             _pri_adj(pid2, pri, mode);
+                        }
+                    }
+                }
+            }
+          if (fd >= 0) close(fd);
+          free(file);
+        }
+    }
+}
+static void
+_cb_nice_set(void *data EINA_UNUSED, const char *params)
+{
+  // PID set|adj NICE_VAL_OR_ADJUST
+  char buf[64] = "";
+  int pid = -1, adj = 0;
+
+  if (sscanf(params, "%i %63s %i", &pid, buf, &adj) != 3) return;
+  if      (!strcmp(buf, "set")) _pri_adj(pid, adj, SET);
+  else if (!strcmp(buf, "adj")) _pri_adj(pid, adj, ADJ);
+}
+
+void
+e_system_nice_init(void)
+{
+  e_system_inout_command_register("nice-set", _cb_nice_set, NULL);
+}
+
+void
+e_system_nice_shutdown(void)
+{
+   // only shutdown things we really have to - no need to free mem etc.
+}
diff --git a/src/bin/system/meson.build b/src/bin/system/meson.build
index d3008e659..34293d22d 100644
--- a/src/bin/system/meson.build
+++ b/src/bin/system/meson.build
@@ -10,6 +10,7 @@ src = [
   'e_system_cpufreq.c',
   'e_system_acpi.c',
   'e_system_battery.c',
+  'e_system_nice.c',
   'e_system.h',
 ]
 executable('enlightenment_system', src,

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to