Skip to content

system/nxinit: add the wait command - #4135

Draft
JianyuWang0623 wants to merge 1 commit into
apache:masterfrom
JianyuWang0623:jy847-nxinit-wait-only
Draft

JianyuWang0623 wants to merge 1 commit into
apache:masterfrom
JianyuWang0623:jy847-nxinit-wait-only

Conversation

@JianyuWang0623

Copy link
Copy Markdown
Contributor

Summary

Add a wait <path> [<timeout>] command to NXInit, so an init.rc line can wait for a file instead of sleeping for a fixed time.

Without the command, an NSH script waits for a file with a shell loop:

while [ ! -e /dev/xxx ]; do
  usleep 100000
done

This needs the NSH scripting commands while, test and usleep; NSH has no single command that waits for a file (its wait takes wait <pid>... and waits for tasks to exit). NXInit cannot express this wait at all: its parser is line-based with no loops or conditions, and an unknown line falls back to sh -c. So without NSH an rc file can only sleep for a fixed time, which either gives up too early or keeps waiting long after the file is there.

For example, a service that registers a device node and a service that needs it:

start foo_driver
wait /dev/xxx 2
start bar

The documentation change is in apache/nuttx#22301.

Impact

wait checks stat() for <path> every 10 ms and returns when the file exists; <timeout> is in seconds, may be fractional and defaults to 5, and reaching it only warns and continues. While it waits, the NXInit command loop is blocked. Existing init.rc files and builds are unchanged.

Testing

Build Host: Linux x86_64, host gcc (CROSSDEV empty). Target: sim:nsh with CONFIG_SYSTEM_NXINIT=y, CONFIG_SYSTEM_NXINIT_TEST=y, CONFIG_TESTING_CMOCKA=y, CONFIG_BOARDCTL=y, CONFIG_PM=y, and # CONFIG_SYSTEM_NSH is not set, on current master 9cd18a2d6. Run: cd nuttx && timeout 30 ./nuttx < /dev/null.

The new cmocka case in the run output:

[ RUN      ] test_nxinit_action_builtin_wait
Executing command 'mount'
Executing command 'wait'
wait for '/nxinit_test_mnt/no_such_file' timed out
Executing command 'wait'
wait for '/nxinit_test_mnt/wait_late' took 219 ms
Executing command 'wait'
Error wait: bad timeout 'bogus'
Executing command 'wait'
Error wait: bad timeout '-1'
Executing command 'wait'
wait for '/nxinit_test_mnt/wait_file' took 0 ms
Executing command 'umount'
[       OK ] test_nxinit_action_builtin_wait

Suite summary of the same run: [==========] nxinit_tests: 24 test(s) run. and [ PASSED ] 24 test(s).

Waiting for a /dev node registered late by a service: a oneshot test service sleeps 300 ms and then registers /dev/wait_node with register_driver(), and init.rc runs start mknode, wait /dev/wait_node 5, exec -- hello (NSH disabled, so every line runs as an NXInit builtin or a spawned Builtin App):

Started service 'mknode' pid 7
Executing command 'wait'
waitscaf: /dev/wait_node registered
wait for '/dev/wait_node' took 319 ms
Command 'wait' took 319 ms
Service 'mknode' flag 0x6 add 0x4
  -flag 'running'
Service 'mknode' flag 0x2 add 0x80000001
  +flag 'disabled'
  +flag 'remove'
Service 'mknode' pid 7 exited status 0
Removing service 'mknode' ...
Executing command 'exec'
executed command 'hello' pid 8
Waiting 'exec' pid 8
Hello, World!!
Command 'exec' pid 8 exited status 0

Without the patch the same init.rc sends the wait line to sh -c, which fails without NSH, and hello runs before the node exists:

command 'wait': 2
Error executing command 'wait /dev/wait_node 5': 2
  argv[0] 'sh'
  argv[1] '-c'
  argv[2] 'wait /dev/wait_node 5'
Executing command 'exec'
executed command 'hello' pid 7
Waiting 'exec' pid 7
Hello, World!!
Command 'exec' pid 7 exited status 0
waitscaf: /dev/wait_node registered

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>
@JianyuWang0623
JianyuWang0623 force-pushed the jy847-nxinit-wait-only branch 2 times, most recently from fde74ae to c57f5d6 Compare November 25, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant