xiaoxiang781216 commented on code in PR #20599:
URL: https://github.com/apache/nuttx/pull/20599#discussion_r4244167199


##########
binfmt/binfmt_exec.c:
##########
@@ -92,6 +97,42 @@ static int exec_internal(FAR const char *filename,
       return -EPERM;
     }
 
+#ifdef CONFIG_BINFMT_SCRIPT
+  /* A script that starts with "#!" runs its interpreter.  The interpreter
+   * can be a script too, up to four levels deep, as in Linux.
+   */
+
+  for (depth = 0; ; depth++)

Review Comment:
   why not implement script load as binfmt_s ?



##########
binfmt/binfmt_script.c:
##########
@@ -0,0 +1,281 @@
+/****************************************************************************
+ * binfmt/binfmt_script.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include <nuttx/envpath.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/lib/lib.h>
+
+#include "binfmt.h"
+
+#ifdef CONFIG_BINFMT_SCRIPT
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The longest "#!" line that is read, as in Linux */
+
+#define SCRIPT_LINE_MAX 256
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: script_readline
+ *
+ * Description:
+ *   Read the first line of a file into buf, without the newline.
+ *
+ * Returned Value:
+ *   The length of the line, zero if the file cannot be read, or -ENOEXEC
+ *   if the file starts with "#!" and the line does not fit in buf.
+ *
+ ****************************************************************************/
+
+static ssize_t script_readline(FAR const char *path, FAR char *buf,
+                               size_t size)
+{
+  struct file filep;
+  FAR char *nl;
+  ssize_t nread;
+
+  if (file_open(&filep, path, O_RDONLY | O_CLOEXEC) < 0)
+    {
+      return 0;
+    }
+
+  nread = file_read(&filep, buf, size - 1);
+  file_close(&filep);
+  if (nread <= 0)
+    {
+      return 0;
+    }
+
+  buf[nread] = '\0';
+  nl = memchr(buf, '\n', nread);
+  if (nl == NULL)
+    {
+      /* A "#!" line that does not fit cannot be run.  Any other file is
+       * not a script, whatever its first bytes are.
+       */
+
+      if (nread == size - 1 && buf[0] == '#' && buf[1] == '!')
+        {
+          return -ENOEXEC;
+        }
+
+      return nread;
+    }
+
+  *nl = '\0';
+  return nl - buf;
+}
+
+/****************************************************************************
+ * Name: script_isspace
+ ****************************************************************************/
+
+static bool script_isspace(char ch)
+{
+  return ch == ' ' || ch == '\t' || ch == '\r';
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: binfmt_script
+ *
+ * Description:
+ *   If filename starts with "#!", build the argument list of its
+ *   interpreter: interpreter [argument] filename argv[1] ... argv[n].
+ *
+ * Input Parameters:
+ *   filename - The program to be executed
+ *   argv     - Its argument list, argv[0] first, or NULL
+ *   newargv  - The new argument list, one allocation freed with kmm_free()
+ *
+ * Returned Value:
+ *   OK with *newargv set if filename is a script; OK with *newargv NULL if
+ *   it is not (or cannot be read: load_module() then reports the error);
+ *   a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int binfmt_script(FAR const char *filename, FAR char * const *argv,

Review Comment:
   could you cherry-pick 
https://github.com/xiaoxiang781216/nuttx/commit/f6f79205e06858a2b43934540a74ca87353cebfa#diff-0d5df6a5d7311db8deb98b01df5c08a8a1f35ad37a12890a3b9052d581767068
 which implement the similar function and pass the test on several product?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to