This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1e1e6473477eedc53b74dfc1519fb6197c159c89
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 2 12:18:26 2026 +0100

    libs/libc/unistd: Run a file that is not a program with the shell in 
execvp().
    
    POSIX specifies that execlp(), execvp() and execvpe() run a file that
    execve() refuses with ENOEXEC (a shell script without a "#!" line, an
    empty file) with the shell, as if the shell was started with the file
    and the arguments after argv[0].  NuttX returned the error.  For
    example, Perl's open2() starts an empty file through execvp() and got
    "Exec format error", where other systems run the file as an empty
    script.
    
    execvpe() now calls execve() again with the shell when the first
    execve() fails with ENOEXEC: "sh" from PATH with CONFIG_LIBC_ENVPATH
    (as popen() finds it), else _PATH_BSHELL.  A bare file name is given
    to the shell as the path that PATH gives.  execvp() calls execvpe(),
    and execlp() calls execvp().  execl(), execv() and execve() do not
    change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/unistd.h              |   2 +
 libs/libc/unistd/lib_execl.c  |  18 +++---
 libs/libc/unistd/lib_execvp.c | 131 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 140 insertions(+), 11 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 25a40c36435..5eb84aaf459 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -551,6 +551,8 @@ int     fchownat(int dirfd, FAR const char *path, uid_t 
owner,
 /* NOTE: NuttX provides only one behavior:  If CONFIG_LIBC_ENVPATH is
  * defined, then execv(), execl() and execve() also search the PATH as
  * execvp(), execlp() and execvpe() do; otherwise none of them searches it.
+ * Only execvp(), execlp() and execvpe() run a file that is not a program
+ * (ENOEXEC, for example a script without "#!") with the shell (POSIX).
  */
 
 #ifdef CONFIG_LIBC_EXECFUNCS
diff --git a/libs/libc/unistd/lib_execl.c b/libs/libc/unistd/lib_execl.c
index e39f5207f1d..fb4484e3356 100644
--- a/libs/libc/unistd/lib_execl.c
+++ b/libs/libc/unistd/lib_execl.c
@@ -57,7 +57,9 @@
  * Private Functions
  ****************************************************************************/
 
-static int lib_execl(FAR const char *path, FAR const char *arg0,
+static int lib_execl(CODE int (*execfn)(FAR const char *,
+                                        FAR char * const []),
+                     FAR const char *path, FAR const char *arg0,
                      va_list ap)
 {
   FAR char **argv = NULL;
@@ -110,9 +112,9 @@ static int lib_execl(FAR const char *path, FAR const char 
*arg0,
       argv[argc] = va_arg(ap, FAR char *);
     }
 
-  /* Then let execv() do the real work */
+  /* Then let execv() or execvp() do the real work */
 
-  ret = execv(path, argv);
+  ret = execfn(path, argv);
 
   /* Free the allocated argv[] list */
 
@@ -188,7 +190,7 @@ int execl(FAR const char *path, FAR const char *arg0, ...)
   int ret;
 
   va_start(ap, arg0);
-  ret = lib_execl(path, arg0, ap);
+  ret = lib_execl(execv, path, arg0, ap);
   va_end(ap);
   return ret;
 }
@@ -197,8 +199,10 @@ int execl(FAR const char *path, FAR const char *arg0, ...)
  * Name: execlp
  *
  * Description:
- *   The same as execl().  NuttX has only one behavior: with
- *   CONFIG_LIBC_ENVPATH, execl() also searches the PATH for file.
+ *   The same as execl(), except that a file that execl() refuses with
+ *   ENOEXEC is run with the shell (see execvp()).  NuttX has only one PATH
+ *   behavior: with CONFIG_LIBC_ENVPATH, execl() also searches the PATH for
+ *   file.
  *
  ****************************************************************************/
 
@@ -208,7 +212,7 @@ int execlp(FAR const char *file, FAR const char *arg0, ...)
   int ret;
 
   va_start(ap, arg0);
-  ret = lib_execl(file, arg0, ap);
+  ret = lib_execl(execvp, file, arg0, ap);
   va_end(ap);
   return ret;
 }
diff --git a/libs/libc/unistd/lib_execvp.c b/libs/libc/unistd/lib_execvp.c
index 1d18a19cbca..14fee3293cf 100644
--- a/libs/libc/unistd/lib_execvp.c
+++ b/libs/libc/unistd/lib_execvp.c
@@ -24,8 +24,121 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <paths.h>
+#include <string.h>
 #include <unistd.h>
 
+#include <nuttx/envpath.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The shell that runs a file that is not a program.  With
+ * CONFIG_LIBC_ENVPATH, PATH gives the location of "sh" (as for popen()).
+ */
+
+#ifdef CONFIG_LIBC_ENVPATH
+#  define EXECVP_SHELL "sh"
+#else
+#  define EXECVP_SHELL _PATH_BSHELL
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: execvp_shell
+ *
+ * Description:
+ *   Run file with the shell after execve() refused it with ENOEXEC (for
+ *   example a script without "#!"), as POSIX specifies for execvp().
+ *
+ * Returned Value:
+ *   This function does not return on success.  On failure, it returns -1
+ *   (ERROR) and sets errno.
+ *
+ ****************************************************************************/
+
+static int execvp_shell(FAR const char *file, FAR char * const argv[],
+                        FAR char * const envp[])
+{
+  FAR char *fullpath = NULL;
+  FAR const char **shargv;
+  int argc;
+  int ret;
+  int i;
+
+#ifdef CONFIG_LIBC_ENVPATH
+  /* execve() found a bare file name in PATH: give the shell that path */
+
+  if (strchr(file, '/') == NULL)
+    {
+      ENVPATH_HANDLE handle = envpath_init("PATH");
+
+      if (handle != NULL)
+        {
+          fullpath = envpath_next(handle, file);
+          envpath_release(handle);
+        }
+
+      if (fullpath == NULL)
+        {
+          set_errno(ENOEXEC);
+          return ERROR;
+        }
+
+      file = fullpath;
+    }
+#endif
+
+  /* The arguments of the shell: its name, the file, then argv[1] and the
+   * arguments after it.
+   */
+
+  for (argc = 0; argv[argc] != NULL; argc++)
+    {
+    }
+
+  if (argc == 0)
+    {
+      argc = 1;
+    }
+
+  shargv = lib_malloc((argc + 2) * sizeof(FAR const char *));
+  if (shargv == NULL)
+    {
+      lib_free(fullpath);
+      set_errno(ENOMEM);
+      return ERROR;
+    }
+
+  shargv[0] = EXECVP_SHELL;
+  shargv[1] = file;
+  for (i = 1; i < argc; i++)
+    {
+      shargv[i + 1] = argv[i];
+    }
+
+  shargv[argc + 1] = NULL;
+
+  /* execve() takes the strings as char *, but does not change them */
+
+  ret = execve(EXECVP_SHELL, (FAR char * const *)shargv, envp);
+
+  /* The shell did not start: free the memory, keep the errno of execve() */
+
+  lib_free(shargv);
+  lib_free(fullpath);
+  return ret;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -34,21 +147,23 @@
  * Name: execvp
  *
  * Description:
- *   The same as execv().  NuttX has only one behavior: with
+ *   The same as execv(), except that a file that execv() refuses with
+ *   ENOEXEC is run with the shell.  NuttX has only one PATH behavior: with
  *   CONFIG_LIBC_ENVPATH, execv() also searches the PATH for file.
  *
  ****************************************************************************/
 
 int execvp(FAR const char *file, FAR char * const argv[])
 {
-  return execv(file, argv);
+  return execvpe(file, argv, NULL);
 }
 
 /****************************************************************************
  * Name: execvpe
  *
  * Description:
- *   The same as execve().  NuttX has only one behavior: with
+ *   The same as execve(), except that a file that execve() refuses with
+ *   ENOEXEC is run with the shell.  NuttX has only one PATH behavior: with
  *   CONFIG_LIBC_ENVPATH, execve() also searches the PATH for file.
  *
  ****************************************************************************/
@@ -56,5 +171,13 @@ int execvp(FAR const char *file, FAR char * const argv[])
 int execvpe(FAR const char *file, FAR char * const argv[],
             FAR char * const envp[])
 {
-  return execve(file, argv, envp);
+  int ret;
+
+  ret = execve(file, argv, envp);
+  if (ret < 0 && get_errno() == ENOEXEC)
+    {
+      ret = execvp_shell(file, argv, envp);
+    }
+
+  return ret;
 }

Reply via email to