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

git pushed a commit to branch master
in repository efl.

View the commit online.

commit d8c628a59b0c17d3001fbd3d1043a696e3415ce1
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 15 12:26:48 2026 -0600

    ecore: Efl.Exe builds its child environment right and before the fork
    
    The environment handed to a child was built out of values without their
    names. An entry of environ is NAME=VALUE, and this stored the value on
    its own, then left the array unterminated. A child given an
    Efl.Core.Env saw a handful of nameless strings and none of the
    variables it was supposed to have.
    
    It was also built between fork and exec. The function already collects
    its iterators before the fork, with a comment explaining that heap
    allocation there deadlocks, and then walked a hash and took stringshare
    locks after it anyway. In a program with other threads the child quietly
    ran with the parent's environment. The array is now built in the parent
    and the child only installs it.
    
    Replacing the environment of an Efl.Exe leaked the previous one, the
    test before the unref was inverted.
    
    Efl.Exe implements Efl.Io.Closer but answered for neither
    close_on_exec nor close_on_invalidate. It has three descriptors rather
    than one, so it cannot take them from Efl.Io.Closer_Fd; it answers for
    them itself. Its pipes belong to the child and stay close-on-exec, and
    the setter says so rather than pretending it complied.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/ecore/efl_exe.c            | 132 ++++++++++++++++++++------
 src/lib/ecore/efl_exe.eo           |   2 +
 src/tests/ecore/efl_app_suite.c    |   1 +
 src/tests/ecore/efl_app_suite.h    |   1 +
 src/tests/ecore/efl_app_test_exe.c | 184 +++++++++++++++++++++++++++++++++++++
 src/tests/ecore/meson.build        |   1 +
 6 files changed, 293 insertions(+), 28 deletions(-)

diff --git a/src/lib/ecore/efl_exe.c b/src/lib/ecore/efl_exe.c
index 2210f6d14e..b4059331e6 100644
--- a/src/lib/ecore/efl_exe.c
+++ b/src/lib/ecore/efl_exe.c
@@ -71,6 +71,8 @@ struct _Efl_Exe_Data
    Eina_Bool exit_called : 1;
    Eina_Bool exit_signalled : 1;
    Eina_Bool run : 1;
+   Eina_Bool close_on_exec : 1;
+   Eina_Bool close_on_invalidate : 1;
 };
 
 //////////////////////////////////////////////////////////////////////////
@@ -247,7 +249,7 @@ _efl_exe_env_set(Eo *obj EINA_UNUSED, Efl_Exe_Data *pd, Efl_Core_Env *env)
 {
    if (pd->env == env) return;
 
-   if (!pd->env)
+   if (pd->env)
      efl_unref(pd->env);
    pd->env = env;
    if (pd->env)
@@ -358,9 +360,9 @@ _efl_exe_efl_task_run(Eo *obj, Efl_Exe_Data *pd)
 #else
    Eo *loop;
    Efl_Task_Data *tdl = NULL, *td = efl_data_scope_get(obj, EFL_TASK_CLASS);
-   Eina_Iterator *itr = NULL, *itr2 = NULL;
+   Eina_Iterator *itr = NULL;
    const char *cmd;
-   char **newenv, **env = NULL, **e;
+   char **newenv = NULL, **env = NULL, **e;
    int devnull;
    int pipe_stdin[2];
    int pipe_stdout[2];
@@ -433,8 +435,51 @@ _efl_exe_efl_task_run(Eo *obj, Efl_Exe_Data *pd)
    // get these before the fork to avoid heap malloc deadlocks
    loop = efl_provider_find(obj, EFL_LOOP_CLASS);
    if (loop) tdl = efl_data_scope_get(loop, EFL_TASK_CLASS);
-   if (pd->env) itr = efl_core_env_content_get(pd->env);
-   if (pd->env) itr2 = efl_core_env_content_get(pd->env);
+   /* The child's environment is built here, as an array of NAME=VALUE: no heap
+    * work or lock is safe between fork and exec in a multithreaded process.
+    */
+   if (pd->env)
+     {
+        const char *key;
+        int count = 0, i = 0;
+
+        itr = efl_core_env_content_get(pd->env);
+        EINA_ITERATOR_FOREACH(itr, key)
+          {
+             count++;
+          }
+        eina_iterator_free(itr);
+        itr = NULL;
+
+        newenv = malloc(sizeof(char *) * (count + 1));
+        if (!newenv)
+          {
+             _ecore_signal_pid_unlock();
+             return EINA_FALSE;
+          }
+
+        itr = efl_core_env_content_get(pd->env);
+        EINA_ITERATOR_FOREACH(itr, key)
+          {
+             const char *value = efl_core_env_get(pd->env, key);
+             size_t klen = strlen(key);
+             size_t vlen = value ? strlen(value) : 0;
+             char *entry;
+
+             if (i >= count) break;
+             entry = malloc(klen + vlen + 2);
+             if (!entry) break;
+             memcpy(entry, key, klen);
+             entry[klen] = '=';
+             if (vlen) memcpy(entry + klen + 1, value, vlen);
+             entry[klen + 1 + vlen] = '\0';
+             newenv[i] = entry;
+             i++;
+          }
+        eina_iterator_free(itr);
+        itr = NULL;
+        newenv[i] = NULL;
+     }
    sigset_t newset, oldset;
 
    sigemptyset(&newset);
@@ -480,7 +525,18 @@ _efl_exe_efl_task_run(Eo *obj, Efl_Exe_Data *pd)
      {
         pthread_sigmask(SIG_SETMASK, &oldset, NULL);
         if (itr) eina_iterator_free(itr);
-        if (itr2) eina_iterator_free(itr2);
+        // the child has its own copy of these now
+        if (newenv)
+          {
+             char **e2;
+
+             for (e2 = newenv; *e2; e2++)
+               {
+                  free(*e2);
+               }
+             free(newenv);
+             newenv = NULL;
+          }
         // parent process is here inside this if block
         if (td->flags & EFL_TASK_FLAGS_USE_STDIN)  close(pipe_stdin[0]);
         if (td->flags & EFL_TASK_FLAGS_USE_STDOUT) close(pipe_stdout[1]);
@@ -598,28 +654,9 @@ _efl_exe_efl_task_run(Eo *obj, Efl_Exe_Data *pd)
           }
      }
 
-   // actually setenv the env object (clear what was there before so it is
-   // the only env there)
-   if (pd->env)
+   // the environment built before the fork replaces the one inherited
+   if (newenv)
      {
-        const char *key;
-        int count = 0, i = 0;
-
-        // use 2nd throw-away itr to count
-        EINA_ITERATOR_FOREACH(itr2, key)
-          {
-             count++;
-          }
-        // object which we don't free (sitting in hash table in env obj)
-        newenv = alloca(sizeof(char *) * (count + 1));
-        // use 2st iter to walk and fill new env
-        EINA_ITERATOR_FOREACH(itr, key)
-          {
-             newenv[i] = (char *)efl_core_env_get(pd->env, key);
-             i++;
-          }
-        // yes - we dont free itr or itr2 - we're going to exec below or exit
-        // also put newenv array on stack pointign to the strings in the env
 # if defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
         if (_dl_environ) *_dl_environ = newenv;
         else ERR("Can't find envrion symbol");
@@ -697,6 +734,9 @@ _efl_exe_efl_object_constructor(Eo *obj, Efl_Exe_Data *pd)
    pd->fd.can_write = EINA_TRUE;
    pd->flags = 0;
    pd->exit_signal = -1;
+   /* both default to true, as Efl.Io.Closer documents */
+   pd->close_on_exec = EINA_TRUE;
+   pd->close_on_invalidate = EINA_TRUE;
    return obj;
 }
 
@@ -721,7 +761,7 @@ _efl_exe_efl_object_destructor(Eo *obj, Efl_Exe_Data *pd)
    if (pd->fd.out_handler) efl_del(pd->fd.out_handler);
    pd->fd.in_handler = NULL;
    pd->fd.out_handler = NULL;
-   _close_fds(pd);
+   if (pd->close_on_invalidate) _close_fds(pd);
 #endif
    efl_destructor(efl_super(obj, MY_CLASS));
 }
@@ -749,6 +789,42 @@ _efl_exe_efl_io_closer_close(Eo *obj, Efl_Exe_Data *pd)
    return 0;
 }
 
+/* Efl.Exe has three descriptors, so it cannot take close_on_exec and
+ * close_on_invalidate from Efl.Io.Closer_Fd. Its pipes belong to the child and
+ * stay close-on-exec: the setter refuses to turn that off.
+ */
+EOLIAN static Eina_Bool
+_efl_exe_efl_io_closer_close_on_exec_set(Eo *obj EINA_UNUSED, Efl_Exe_Data *pd,
+                                         Eina_Bool close_on_exec)
+{
+   if (close_on_exec) return EINA_TRUE;
+
+   ERR("Efl.Exe keeps its pipes close-on-exec; they belong to the child.");
+   pd->close_on_exec = EINA_TRUE;
+   return EINA_FALSE;
+}
+
+EOLIAN static Eina_Bool
+_efl_exe_efl_io_closer_close_on_exec_get(const Eo *obj EINA_UNUSED,
+                                         Efl_Exe_Data *pd)
+{
+   return pd->close_on_exec;
+}
+
+EOLIAN static void
+_efl_exe_efl_io_closer_close_on_invalidate_set(Eo *obj EINA_UNUSED, Efl_Exe_Data *pd,
+                                               Eina_Bool close_on_invalidate)
+{
+   pd->close_on_invalidate = close_on_invalidate;
+}
+
+EOLIAN static Eina_Bool
+_efl_exe_efl_io_closer_close_on_invalidate_get(const Eo *obj EINA_UNUSED,
+                                               Efl_Exe_Data *pd)
+{
+   return pd->close_on_invalidate;
+}
+
 EOLIAN static Eina_Bool
 _efl_exe_efl_io_closer_closed_get(const Eo *obj EINA_UNUSED, Efl_Exe_Data *pd)
 {
diff --git a/src/lib/ecore/efl_exe.eo b/src/lib/ecore/efl_exe.eo
index c4cc2d7ebf..7c45198669 100644
--- a/src/lib/ecore/efl_exe.eo
+++ b/src/lib/ecore/efl_exe.eo
@@ -85,6 +85,8 @@ class @beta Efl.Exe extends Efl.Task implements Efl.Io.Reader, Efl.Io.Writer, Ef
       Efl.Task.end;
       Efl.Io.Closer.close;
       Efl.Io.Closer.closed { get; }
+      Efl.Io.Closer.close_on_exec { get; set; }
+      Efl.Io.Closer.close_on_invalidate { get; set; }
       Efl.Io.Reader.read;
       Efl.Io.Reader.can_read { get; set; }
       Efl.Io.Reader.eos { get; set; }
diff --git a/src/tests/ecore/efl_app_suite.c b/src/tests/ecore/efl_app_suite.c
index c5dee5df00..f4863ed2b7 100644
--- a/src/tests/ecore/efl_app_suite.c
+++ b/src/tests/ecore/efl_app_suite.c
@@ -55,6 +55,7 @@ static const Efl_Test_Case etc[] = {
   { "Env", efl_test_efl_env },
   { "CML", efl_test_efl_cml },
   { "Io", efl_app_test_efl_io },
+  { "Exe", efl_app_test_efl_exe },
   { NULL, NULL }
 };
 
diff --git a/src/tests/ecore/efl_app_suite.h b/src/tests/ecore/efl_app_suite.h
index 22c9c1d1fe..a9718c88ea 100644
--- a/src/tests/ecore/efl_app_suite.h
+++ b/src/tests/ecore/efl_app_suite.h
@@ -8,6 +8,7 @@ void efl_app_test_efl_loop(TCase *tc);
 void efl_app_test_efl_loop_fd(TCase *tc);
 void efl_app_test_efl_loop_timer(TCase *tc);
 void efl_app_test_efl_io(TCase *tc);
+void efl_app_test_efl_exe(TCase *tc);
 void efl_app_test_promise(TCase *tc);
 void efl_app_test_promise_2(TCase *tc);
 void efl_app_test_promise_3(TCase *tc);
diff --git a/src/tests/ecore/efl_app_test_exe.c b/src/tests/ecore/efl_app_test_exe.c
new file mode 100644
index 0000000000..ece0ba8786
--- /dev/null
+++ b/src/tests/ecore/efl_app_test_exe.c
@@ -0,0 +1,184 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#define EFL_BETA_API_SUPPORT
+#define EFL_NOLEGACY_API_SUPPORT
+#include <Efl_Core.h>
+#include "efl_app_suite.h"
+#include "../efl_check.h"
+
+typedef struct
+{
+   Eina_Strbuf *collected;
+   int exit_code;
+   Eina_Bool exited;
+} Exe_Run;
+
+/* Drain, rather than read once. The exit event does not arrive until the
+ * child's stdout has been read to its end -- that is how Efl.Exe knows there
+ * is nothing left to hand over -- so a handler that takes one chunk per
+ * notification and stops leaves the task looking as though it were still
+ * running.
+ */
+static void
+_exe_data(void *data, const Efl_Event *ev)
+{
+   Exe_Run *run = data;
+
+   while (efl_io_reader_can_read_get(ev->object))
+     {
+        char buf[256];
+        Eina_Rw_Slice slice = { sizeof(buf), { buf } };
+
+        if (efl_io_reader_read(ev->object, &slice) != 0) return;
+        eina_strbuf_append_length(run->collected, buf, slice.len);
+     }
+}
+
+/* The exit code is read here rather than after the loop: an Efl.Exe deletes
+ * itself once it has exited, the way ecore_exe always did, so this event is
+ * the last moment the object exists.
+ */
+static void
+_exe_exited(void *data, const Efl_Event *ev)
+{
+   Exe_Run *run = data;
+   run->exit_code = efl_task_exit_code_get(ev->object);
+   run->exited = EINA_TRUE;
+   efl_loop_quit(efl_main_loop_get(), eina_value_int_init(0));
+}
+
+/* Run something, read what it wrote. The command line comes from
+ * Efl.Core.Command_Line, which Efl.Exe implements.
+ */
+EFL_START_TEST(efl_exe_test_reads_child_output)
+{
+   Eo *exe;
+   Exe_Run run = { eina_strbuf_new(), -1, EINA_FALSE };
+
+   exe = efl_add(EFL_EXE_CLASS, efl_main_loop_get(),
+                 efl_core_command_line_command_string_set(efl_added,
+                    "/bin/sh -c 'printf hello'"),
+                 /* without this the child inherits our stdout and there is
+                  * nothing to read */
+                 efl_task_flags_set(efl_added, EFL_TASK_FLAGS_USE_STDOUT),
+                 efl_event_callback_add(efl_added, EFL_IO_READER_EVENT_CAN_READ_CHANGED,
+                                        _exe_data, &run),
+                 efl_event_callback_add(efl_added, EFL_TASK_EVENT_EXIT,
+                                        _exe_exited, &run));
+   fail_if(!exe);
+   efl_task_run(exe);
+
+   efl_loop_begin(efl_main_loop_get());
+
+   ck_assert(run.exited);
+   ck_assert_str_eq(eina_strbuf_string_get(run.collected), "hello");
+   ck_assert_int_eq(run.exit_code, 0);
+
+   /* no efl_del here: the class deletes itself once it has exited */
+   eina_strbuf_free(run.collected);
+}
+EFL_END_TEST
+
+/* A child sees the Efl.Core.Env it was given, and nothing of the parent's. */
+EFL_START_TEST(efl_exe_test_env_reaches_the_child)
+{
+   Eo *exe, *env;
+   Exe_Run run = { eina_strbuf_new(), -1, EINA_FALSE };
+
+   /* start from this process's environment, so the child still has a PATH,
+      and add one variable to it */
+   env = efl_duplicate(efl_env_self());
+   efl_core_env_set(env, "EFL_EXE_TEST_VAR", "visible");
+
+   exe = efl_add(EFL_EXE_CLASS, efl_main_loop_get(),
+                 efl_core_command_line_command_string_set(efl_added,
+                    "/bin/sh -c 'printf %s $EFL_EXE_TEST_VAR'"),
+                 efl_task_flags_set(efl_added, EFL_TASK_FLAGS_USE_STDOUT),
+                 efl_exe_env_set(efl_added, env),
+                 efl_event_callback_add(efl_added, EFL_IO_READER_EVENT_CAN_READ_CHANGED,
+                                        _exe_data, &run),
+                 efl_event_callback_add(efl_added, EFL_TASK_EVENT_EXIT,
+                                        _exe_exited, &run));
+   fail_if(!exe);
+   efl_task_run(exe);
+
+   efl_loop_begin(efl_main_loop_get());
+
+   ck_assert(run.exited);
+   ck_assert_str_eq(eina_strbuf_string_get(run.collected), "visible");
+
+   eina_strbuf_free(run.collected);
+   efl_unref(env);
+}
+EFL_END_TEST
+
+/* Replacing the environment of an Efl.Exe lets go of the previous one. */
+EFL_START_TEST(efl_exe_test_env_set_releases_previous)
+{
+   Eo *exe, *first, *second;
+   Eo *first_wref = NULL;
+
+   exe = efl_add(EFL_EXE_CLASS, efl_main_loop_get(),
+                 efl_core_command_line_command_string_set(efl_added, "/bin/true"));
+   fail_if(!exe);
+
+   first = efl_duplicate(efl_env_self());
+   efl_wref_add(first, &first_wref);
+   efl_exe_env_set(exe, first);
+   efl_unref(first);
+   ck_assert_ptr_nonnull(first_wref);
+
+   second = efl_duplicate(efl_env_self());
+   efl_exe_env_set(exe, second);
+   efl_unref(second);
+   ck_assert_ptr_null(first_wref);
+
+   efl_del(exe);
+}
+EFL_END_TEST
+
+/* The two Efl.Io.Closer properties the class had never answered for. */
+EFL_START_TEST(efl_exe_test_closer_properties)
+{
+   Eo *exe;
+   Exe_Run run = { NULL, -1, EINA_FALSE };
+
+   exe = efl_add(EFL_EXE_CLASS, efl_main_loop_get(),
+                 efl_core_command_line_command_string_set(efl_added, "/bin/true"),
+                 efl_event_callback_add(efl_added, EFL_TASK_EVENT_EXIT,
+                                        _exe_exited, &run));
+   fail_if(!exe);
+
+   /* both default to true */
+   ck_assert(efl_io_closer_close_on_exec_get(exe));
+   ck_assert(efl_io_closer_close_on_invalidate_get(exe));
+
+   /* the pipes to the child are not for anything this process execs, so the
+    * setter refuses rather than pretending; the safety log is the test working
+    */
+   ck_assert(!efl_io_closer_close_on_exec_set(exe, EINA_FALSE));
+   ck_assert(efl_io_closer_close_on_exec_get(exe));
+
+   efl_io_closer_close_on_invalidate_set(exe, EINA_FALSE);
+   ck_assert(!efl_io_closer_close_on_invalidate_get(exe));
+   efl_io_closer_close_on_invalidate_set(exe, EINA_TRUE);
+
+   efl_task_run(exe);
+   efl_loop_begin(efl_main_loop_get());
+   ck_assert(run.exited);
+   ck_assert_int_eq(run.exit_code, 0);
+}
+EFL_END_TEST
+
+void
+efl_app_test_efl_exe(TCase *tc)
+{
+   tcase_add_test(tc, efl_exe_test_reads_child_output);
+   tcase_add_test(tc, efl_exe_test_env_reaches_the_child);
+   tcase_add_test(tc, efl_exe_test_env_set_releases_previous);
+   tcase_add_test(tc, efl_exe_test_closer_properties);
+}
diff --git a/src/tests/ecore/meson.build b/src/tests/ecore/meson.build
index 8dcf72f6f3..9f107c9842 100644
--- a/src/tests/ecore/meson.build
+++ b/src/tests/ecore/meson.build
@@ -86,6 +86,7 @@ efl_app_suite_src = [
   'efl_app_test_env.c',
   'efl_app_test_cml.c',
   'efl_app_test_io.c',
+  'efl_app_test_exe.c',
 ]
 
 priv_eo_files = [

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

Reply via email to