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

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


The following commit(s) were added to refs/heads/master by this push:
     new 53f5906de system/nxinit: add the wait command
53f5906de is described below

commit 53f5906de5936b2d4ca9c138d8a33f5177b5760d
Author: wangjianyu3 <[email protected]>
AuthorDate: Wed Nov 25 13:48:29 2026 +0800

    system/nxinit: add the wait command
    
    An init.rc line could only wait for a file with a fixed sleep: an NSH
    script can loop "while [ ! -e <path> ]; do usleep ...; done", but
    NXInit's parser has no loops and an unknown line falls back to
    "sh -c".  Check for <path> with stat() every 10 ms; <timeout_ms> is
    an integer number of milliseconds (default 5000) and a timeout only
    warns.
    
    Assisted-by: OpenCode:mimo-v2.6-pro
    Co-authored-by: multica-agent <[email protected]>
    Signed-off-by: wangjianyu3 <[email protected]>
---
 system/nxinit/builtin.c                 |  67 +++++++++++++
 system/nxinit/test/test_nxinit.c        |   1 +
 system/nxinit/test/test_nxinit.h        |   1 +
 system/nxinit/test/test_nxinit_action.c | 167 ++++++++++++++++++++++++++++++++
 4 files changed, 236 insertions(+)

diff --git a/system/nxinit/builtin.c b/system/nxinit/builtin.c
index 70fcccb5e..8b76d7955 100644
--- a/system/nxinit/builtin.c
+++ b/system/nxinit/builtin.c
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 #include <spawn.h>
@@ -35,7 +36,11 @@
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <sys/param.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
+#include <time.h>
+
+#include <nuttx/clock.h>
 
 #include <netutils/netinit.h>
 
@@ -125,6 +130,8 @@ static int cmd_rptun(FAR struct action_manager_s *am,
 #endif
 static int cmd_unlink(FAR struct action_manager_s *am,
                       int argc, FAR char **argv);
+static int cmd_wait(FAR struct action_manager_s *am,
+                    int argc, FAR char **argv);
 #ifndef CONFIG_DISABLE_MOUNTPOINT
 static int cmd_mount(FAR struct action_manager_s *am,
                      int argc, FAR char **argv);
@@ -194,6 +201,7 @@ static const struct cmd_map_s g_builtin[] =
   {"umount", 2, 2, cmd_umount},
 #endif
   {"unlink", 2, 2, cmd_unlink},
+  {"wait", 2, 3, cmd_wait},
 };
 
 /****************************************************************************
@@ -485,6 +493,65 @@ static int cmd_unlink(FAR struct action_manager_s *am,
   return ret;
 }
 
+/* wait <path> [<timeout_ms>]
+ *
+ * Check for the existence of <path> with stat() every 10 ms and
+ * return when it is found.  <timeout_ms> is an integer number of
+ * milliseconds; it defaults to 5000.  When the timeout is reached,
+ * only warn and continue so a later command still runs.
+ */
+
+static int cmd_wait(FAR struct action_manager_s *am,
+                    int argc, FAR char **argv)
+{
+  int timeout_ms = 5000;
+  struct timespec start;
+  struct timespec diff;
+  struct timespec now;
+  struct stat st;
+  int waited;
+
+  UNUSED(am);
+
+  if (argc > 2)
+    {
+      FAR char *end;
+      long val;
+
+      val = strtol(argv[2], &end, 10);
+      if (end == argv[2] || *end != '\0' || val < 0 ||
+          val > INT_MAX)
+        {
+          init_err("wait: bad timeout '%s'", argv[2]);
+          return -EINVAL;
+        }
+
+      timeout_ms = val;
+    }
+
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  for (; ; )
+    {
+      clock_gettime(CLOCK_MONOTONIC, &now);
+      clock_timespec_subtract(&now, &start, &diff);
+      waited = TIMESPEC2MS(diff);
+
+      if (stat(argv[1], &st) == 0)
+        {
+          init_info("wait for '%s' took %d ms", argv[1], waited);
+          return 0;
+        }
+
+      if (waited >= timeout_ms)
+        {
+          init_warn("wait for '%s' timed out", argv[1]);
+          return 0;
+        }
+
+      usleep(10 * 1000);
+    }
+}
+
 #ifndef CONFIG_DISABLE_MOUNTPOINT
 /* Copy the comma-separated list in options to data without the flags
  * (g_mount_flags), and return the flags.  options does not change, as the
diff --git a/system/nxinit/test/test_nxinit.c b/system/nxinit/test/test_nxinit.c
index 8b383787e..ff152075c 100644
--- a/system/nxinit/test/test_nxinit.c
+++ b/system/nxinit/test/test_nxinit.c
@@ -59,6 +59,7 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test(test_nxinit_action_empty_skipped),
       cmocka_unit_test(test_nxinit_action_builtin_mount),
       cmocka_unit_test(test_nxinit_action_builtin_pmconfig),
+      cmocka_unit_test(test_nxinit_action_builtin_wait),
       cmocka_unit_test(test_nxinit_service_duplicate_conflict),
       cmocka_unit_test(test_nxinit_service_override_replaces_duplicate),
       cmocka_unit_test(test_nxinit_service_args_max_boundary),
diff --git a/system/nxinit/test/test_nxinit.h b/system/nxinit/test/test_nxinit.h
index 479aeaba3..7e5f77d16 100644
--- a/system/nxinit/test/test_nxinit.h
+++ b/system/nxinit/test/test_nxinit.h
@@ -73,6 +73,7 @@ void test_nxinit_action_event_and_semantics(FAR void **state);
 void test_nxinit_action_empty_skipped(FAR void **state);
 void test_nxinit_action_builtin_mount(FAR void **state);
 void test_nxinit_action_builtin_pmconfig(FAR void **state);
+void test_nxinit_action_builtin_wait(FAR void **state);
 
 /****************************************************************************
  * Name: test_nxinit_service_*
diff --git a/system/nxinit/test/test_nxinit_action.c 
b/system/nxinit/test/test_nxinit_action.c
index b032f2c27..9c05ef3b0 100644
--- a/system/nxinit/test/test_nxinit_action.c
+++ b/system/nxinit/test/test_nxinit_action.c
@@ -37,7 +37,9 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <pthread.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/boardctl.h>
 #include <sys/param.h>
@@ -532,3 +534,168 @@ void test_nxinit_action_builtin_pmconfig(FAR void **state)
   skip();
 #endif
 }
+
+#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)
+
+/* Create the file of the "late" case of test_nxinit_action_builtin_wait
+ * while the test is blocked in "wait".
+ */
+
+static void *wait_create_late_file(FAR void *arg)
+{
+  int fd;
+
+  UNUSED(arg);
+  usleep(200 * 1000);
+  fd = open(TEST_MNT "/wait_late", O_CREAT | O_WRONLY, 0644);
+  if (fd >= 0)
+    {
+      close(fd);
+    }
+
+  return NULL;
+}
+
+#endif
+
+/****************************************************************************
+ * Name: test_nxinit_action_builtin_wait
+ *
+ * Description:
+ *   "wait" polls for the file and returns when it exists.  A missing
+ *   file only warns and returns 0 once the optional timeout (seconds,
+ *   default 5) is reached, and a bad timeout fails with -EINVAL.
+ ****************************************************************************/
+
+void test_nxinit_action_builtin_wait(FAR void **state)
+{
+#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)
+  FAR char *mnt[] =
+    {
+      "mount", "tmpfs", "none", TEST_MNT, NULL
+    };
+
+  FAR char *umnt[] =
+    {
+      "umount", TEST_MNT, NULL
+    };
+
+  FAR char *found[] =
+    {
+      "wait", TEST_MNT "/wait_file", NULL
+    };
+
+  FAR char *late[] =
+    {
+      "wait", TEST_MNT "/wait_late", NULL
+    };
+
+  FAR char *badnum[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "bogus", NULL
+    };
+
+  FAR char *negative[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "-1", NULL
+    };
+
+  FAR char *frac[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "0.5", NULL
+    };
+
+  FAR char *exponent[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "1e3", NULL
+    };
+
+  FAR char *empty[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "", NULL
+    };
+
+  FAR char *overflow[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "99999999999999999999", NULL
+    };
+
+  FAR char *twohundred[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "200", NULL
+    };
+
+  FAR char *twothousand[] =
+    {
+      "wait", TEST_MNT "/no_such_file", "2000", NULL
+    };
+
+  struct action_manager_s am;
+  struct timespec start;
+  struct timespec end;
+  pthread_t tid;
+  long waited;
+  int fd;
+
+  action_manager_init(&am);
+  assert_int_equal(init_builtin_run(&am, 4, mnt), 0);
+
+  /* The timeout is an integer number of milliseconds; anything else
+   * fails right away.
+   */
+
+  assert_int_equal(init_builtin_run(&am, 3, badnum), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, negative), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, frac), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, exponent), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, empty), -EINVAL);
+  assert_int_equal(init_builtin_run(&am, 3, overflow), -EINVAL);
+
+  /* A missing file waits for the timeout, then warns and returns 0 */
+
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  assert_int_equal(init_builtin_run(&am, 3, twohundred), 0);
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  waited = (end.tv_sec - start.tv_sec) * 1000 +
+           (end.tv_nsec - start.tv_nsec) / 1000000;
+  assert_true(waited >= 150 && waited < 1200);
+
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  assert_int_equal(init_builtin_run(&am, 3, twothousand), 0);
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  waited = (end.tv_sec - start.tv_sec) * 1000 +
+           (end.tv_nsec - start.tv_nsec) / 1000000;
+  assert_true(waited >= 1900 && waited < 3200);
+
+  /* A file that shows up while waiting ends the wait */
+
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  assert_int_equal(pthread_create(&tid, NULL, wait_create_late_file,
+                                  NULL), 0);
+  assert_int_equal(init_builtin_run(&am, 2, late), 0);
+  assert_int_equal(pthread_join(tid, NULL), 0);
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  waited = (end.tv_sec - start.tv_sec) * 1000 +
+           (end.tv_nsec - start.tv_nsec) / 1000000;
+  assert_true(waited >= 150 && waited < 3000);
+  assert_int_equal(unlink(TEST_MNT "/wait_late"), 0);
+
+  /* An existing file returns 0 right away */
+
+  fd = open(TEST_MNT "/wait_file", O_CREAT | O_WRONLY, 0644);
+  assert_true(fd >= 0);
+  close(fd);
+  clock_gettime(CLOCK_MONOTONIC, &start);
+  assert_int_equal(init_builtin_run(&am, 2, found), 0);
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  waited = (end.tv_sec - start.tv_sec) * 1000 +
+           (end.tv_nsec - start.tv_nsec) / 1000000;
+  assert_true(waited < 1000);
+  assert_int_equal(unlink(TEST_MNT "/wait_file"), 0);
+
+  assert_int_equal(init_builtin_run(&am, 2, umnt), 0);
+  action_manager_free_all(&am);
+#else
+  skip();
+#endif
+}

Reply via email to