Skip to content

Commit c57f5d6

Browse files
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> in seconds defaults to 5, and a timeout only warns and continues. Assisted-by: OpenCode:mimo-v2.6-pro Co-authored-by: multica-agent <github@multica.ai> Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
1 parent 9cd18a2 commit c57f5d6

4 files changed

Lines changed: 194 additions & 0 deletions

File tree

‎system/nxinit/builtin.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <errno.h>
2828
#include <fcntl.h>
2929
#include <inttypes.h>
30+
#include <limits.h>
3031
#include <stdlib.h>
3132
#include <string.h>
3233
#include <spawn.h>
@@ -35,7 +36,9 @@
3536
#include <sys/ioctl.h>
3637
#include <sys/mount.h>
3738
#include <sys/param.h>
39+
#include <sys/stat.h>
3840
#include <sys/wait.h>
41+
#include <time.h>
3942

4043
#include <netutils/netinit.h>
4144

@@ -125,6 +128,8 @@ static int cmd_rptun(FAR struct action_manager_s *am,
125128
#endif
126129
static int cmd_unlink(FAR struct action_manager_s *am,
127130
int argc, FAR char **argv);
131+
static int cmd_wait(FAR struct action_manager_s *am,
132+
int argc, FAR char **argv);
128133
#ifndef CONFIG_DISABLE_MOUNTPOINT
129134
static int cmd_mount(FAR struct action_manager_s *am,
130135
int argc, FAR char **argv);
@@ -194,6 +199,7 @@ static const struct cmd_map_s g_builtin[] =
194199
{"umount", 2, 2, cmd_umount},
195200
#endif
196201
{"unlink", 2, 2, cmd_unlink},
202+
{"wait", 2, 3, cmd_wait},
197203
};
198204

199205
/****************************************************************************
@@ -485,6 +491,63 @@ static int cmd_unlink(FAR struct action_manager_s *am,
485491
return ret;
486492
}
487493

494+
/* wait <path> [<timeout>]
495+
*
496+
* Check for the existence of <path> with stat() every 10 ms and
497+
* return when it is found. <timeout> is in seconds and may be
498+
* fractional; it defaults to 5. When the timeout is reached, only
499+
* warn and continue so a later command still runs.
500+
*/
501+
502+
static int cmd_wait(FAR struct action_manager_s *am,
503+
int argc, FAR char **argv)
504+
{
505+
long timeout_ms = 5000;
506+
struct timespec start;
507+
struct timespec now;
508+
struct stat st;
509+
FAR char *end;
510+
double timeout;
511+
long waited;
512+
513+
UNUSED(am);
514+
515+
if (argc > 2)
516+
{
517+
timeout = strtod(argv[2], &end);
518+
if (end == argv[2] || *end != '\0' || !(timeout >= 0) ||
519+
timeout > (double)LONG_MAX / 1000)
520+
{
521+
init_err("wait: bad timeout '%s'", argv[2]);
522+
return -EINVAL;
523+
}
524+
525+
timeout_ms = (long)(timeout * 1000);
526+
}
527+
528+
clock_gettime(CLOCK_MONOTONIC, &start);
529+
for (; ; )
530+
{
531+
clock_gettime(CLOCK_MONOTONIC, &now);
532+
waited = (now.tv_sec - start.tv_sec) * 1000 +
533+
(now.tv_nsec - start.tv_nsec) / 1000000;
534+
535+
if (stat(argv[1], &st) == 0)
536+
{
537+
init_info("wait for '%s' took %ld ms", argv[1], waited);
538+
return 0;
539+
}
540+
541+
if (waited >= timeout_ms)
542+
{
543+
init_warn("wait for '%s' timed out", argv[1]);
544+
return 0;
545+
}
546+
547+
usleep(10 * 1000);
548+
}
549+
}
550+
488551
#ifndef CONFIG_DISABLE_MOUNTPOINT
489552
/* Copy the comma-separated list in options to data without the flags
490553
* (g_mount_flags), and return the flags. options does not change, as the

‎system/nxinit/test/test_nxinit.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int main(int argc, FAR char *argv[])
5959
cmocka_unit_test(test_nxinit_action_empty_skipped),
6060
cmocka_unit_test(test_nxinit_action_builtin_mount),
6161
cmocka_unit_test(test_nxinit_action_builtin_pmconfig),
62+
cmocka_unit_test(test_nxinit_action_builtin_wait),
6263
cmocka_unit_test(test_nxinit_service_duplicate_conflict),
6364
cmocka_unit_test(test_nxinit_service_override_replaces_duplicate),
6465
cmocka_unit_test(test_nxinit_service_args_max_boundary),

‎system/nxinit/test/test_nxinit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void test_nxinit_action_event_and_semantics(FAR void **state);
7373
void test_nxinit_action_empty_skipped(FAR void **state);
7474
void test_nxinit_action_builtin_mount(FAR void **state);
7575
void test_nxinit_action_builtin_pmconfig(FAR void **state);
76+
void test_nxinit_action_builtin_wait(FAR void **state);
7677

7778
/****************************************************************************
7879
* Name: test_nxinit_service_*

‎system/nxinit/test/test_nxinit_action.c‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
#include <stdint.h>
3838
#include <stdio.h>
3939
#include <stdlib.h>
40+
#include <pthread.h>
4041
#include <string.h>
42+
#include <time.h>
4143
#include <unistd.h>
4244
#include <sys/boardctl.h>
4345
#include <sys/param.h>
@@ -532,3 +534,130 @@ void test_nxinit_action_builtin_pmconfig(FAR void **state)
532534
skip();
533535
#endif
534536
}
537+
538+
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)
539+
540+
/* Create the file of the "late" case of test_nxinit_action_builtin_wait
541+
* while the test is blocked in "wait".
542+
*/
543+
544+
static void *wait_create_late_file(FAR void *arg)
545+
{
546+
int fd;
547+
548+
UNUSED(arg);
549+
usleep(200 * 1000);
550+
fd = open(TEST_MNT "/wait_late", O_CREAT | O_WRONLY, 0644);
551+
if (fd >= 0)
552+
{
553+
close(fd);
554+
}
555+
556+
return NULL;
557+
}
558+
559+
#endif
560+
561+
/****************************************************************************
562+
* Name: test_nxinit_action_builtin_wait
563+
*
564+
* Description:
565+
* "wait" polls for the file and returns when it exists. A missing
566+
* file only warns and returns 0 once the optional timeout (seconds,
567+
* default 5) is reached, and a bad timeout fails with -EINVAL.
568+
****************************************************************************/
569+
570+
void test_nxinit_action_builtin_wait(FAR void **state)
571+
{
572+
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_TMPFS)
573+
FAR char *mnt[] =
574+
{
575+
"mount", "tmpfs", "none", TEST_MNT, NULL
576+
};
577+
578+
FAR char *umnt[] =
579+
{
580+
"umount", TEST_MNT, NULL
581+
};
582+
583+
FAR char *found[] =
584+
{
585+
"wait", TEST_MNT "/wait_file", NULL
586+
};
587+
588+
FAR char *missing[] =
589+
{
590+
"wait", TEST_MNT "/no_such_file", "0.1", NULL
591+
};
592+
593+
FAR char *late[] =
594+
{
595+
"wait", TEST_MNT "/wait_late", NULL
596+
};
597+
598+
FAR char *badnum[] =
599+
{
600+
"wait", TEST_MNT "/no_such_file", "bogus", NULL
601+
};
602+
603+
FAR char *negative[] =
604+
{
605+
"wait", TEST_MNT "/no_such_file", "-1", NULL
606+
};
607+
608+
struct action_manager_s am;
609+
struct timespec start;
610+
struct timespec end;
611+
pthread_t tid;
612+
long waited;
613+
int fd;
614+
615+
action_manager_init(&am);
616+
assert_int_equal(init_builtin_run(&am, 4, mnt), 0);
617+
618+
/* A missing file waits for the timeout, then warns and returns 0 */
619+
620+
clock_gettime(CLOCK_MONOTONIC, &start);
621+
assert_int_equal(init_builtin_run(&am, 3, missing), 0);
622+
clock_gettime(CLOCK_MONOTONIC, &end);
623+
waited = (end.tv_sec - start.tv_sec) * 1000 +
624+
(end.tv_nsec - start.tv_nsec) / 1000000;
625+
assert_true(waited >= 90);
626+
627+
/* A file that shows up while waiting ends the wait */
628+
629+
clock_gettime(CLOCK_MONOTONIC, &start);
630+
assert_int_equal(pthread_create(&tid, NULL, wait_create_late_file,
631+
NULL), 0);
632+
assert_int_equal(init_builtin_run(&am, 2, late), 0);
633+
assert_int_equal(pthread_join(tid, NULL), 0);
634+
clock_gettime(CLOCK_MONOTONIC, &end);
635+
waited = (end.tv_sec - start.tv_sec) * 1000 +
636+
(end.tv_nsec - start.tv_nsec) / 1000000;
637+
assert_true(waited >= 150 && waited < 3000);
638+
assert_int_equal(unlink(TEST_MNT "/wait_late"), 0);
639+
640+
/* A bad or negative timeout fails right away */
641+
642+
assert_int_equal(init_builtin_run(&am, 3, badnum), -EINVAL);
643+
assert_int_equal(init_builtin_run(&am, 3, negative), -EINVAL);
644+
645+
/* An existing file returns 0 right away */
646+
647+
fd = open(TEST_MNT "/wait_file", O_CREAT | O_WRONLY, 0644);
648+
assert_true(fd >= 0);
649+
close(fd);
650+
clock_gettime(CLOCK_MONOTONIC, &start);
651+
assert_int_equal(init_builtin_run(&am, 2, found), 0);
652+
clock_gettime(CLOCK_MONOTONIC, &end);
653+
waited = (end.tv_sec - start.tv_sec) * 1000 +
654+
(end.tv_nsec - start.tv_nsec) / 1000000;
655+
assert_true(waited < 1000);
656+
assert_int_equal(unlink(TEST_MNT "/wait_file"), 0);
657+
658+
assert_int_equal(init_builtin_run(&am, 2, umnt), 0);
659+
action_manager_free_all(&am);
660+
#else
661+
skip();
662+
#endif
663+
}

0 commit comments

Comments
 (0)