JianyuWang0623 opened a new pull request, #4135:
URL: https://github.com/apache/nuttx-apps/pull/4135

   ## 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 https://github.com/apache/nuttx/pull/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
   ```
   


-- 
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