Skip to content

system/nxinit: support compound command and resetcause-based triggers - #3751

Open
JianyuWang0623 wants to merge 5 commits into
apache:masterfrom
JianyuWang0623:nxinit-action-trigger-enhancements
Open

system/nxinit: support compound command and resetcause-based triggers#3751
JianyuWang0623 wants to merge 5 commits into
apache:masterfrom
JianyuWang0623:nxinit-action-trigger-enhancements

Conversation

@JianyuWang0623

Copy link
Copy Markdown
Contributor

Summary

  • Add support for compound command execution in init.rc action bodies (&& / || short-circuit semantics), e.g.:
    echo "start" && hello && echo "done"
    ls /missing || echo "not found"
    
  • Add built-in property sys.boot.reason so action triggers can fire based on the board's reset cause, e.g.:
    on property:sys.boot.reason=cpu_soft_reset(bootloader)
        echo "bootloader mode ..."
        start fastboot
    
  • Add subreason support for sys.boot.reason to allow finer-grained matching, e.g.:
    on init && property:sys.boot.reason=watchdog,4
    on init && property:sys.boot.reason=bootloader|recovery|thermal
    
  • Fix a missing "assert" entry in the resetflag[] designated-initializer array that could lead to a NULL pointer dereference when accessing reset.flag.

Impact

  • No impact on existing behavior when these features are unused.
  • action.c/action.h: init_action_reap_command() signature gained a second parameter to propagate the command's return value for &&/|| short-circuiting.

Testing

Build Host: Linux x86_64 (Ubuntu, kernel 6.8.0), gcc (Ubuntu 13.4.0-6ubuntu122ppa2) 13.4.0
Target: sim:nsh (apps/system/nxinit is architecture-independent; verified via the host simulator since no esp32p4 riscv32 cross toolchain is available on this machine)

Build (excerpt, CONFIG_SYSTEM_NXINIT=y):

Register: init
CC:  action.c
CC:  init.c
CC:  parser.c
CC:  builtin.c
CC:  import.c
CC:  service.c
CC:  property_simple.c
IN: /.../apps/libapps.a -> staging/libapps.a
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz

Runtime (./nuttx, actual console output):

NuttShell (NSH) NuttX-13.0.1-RC0
nsh> ps
  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
    0     0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069584 Idle_Task
    1     0     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067448 sim_loop_wq 0x72f7d6c003f0 0x72f7d6c00470
    2     0     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067472 hpwork 0x40167dc0 0x40167e40
    4     4     0 100 FIFO     Task      - Running            0000000000000000 0067504 nsh_main
nsh> exit

Style check (tools/checkpatch.sh -g apache/master..nxinit-action-trigger-enhancements, actual output):

/tmp/.../apps/system/nxinit/action.c:117:4: error: Bad left brace alignment
Some checks failed. For contributing guidelines, see:
  https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md

The single reported issue (action.c:117) is pre-existing on apache/master — reproduced by running nxstyle directly against the unmodified apache/master copy of action.c, which reports the identical error at the same line. It is not introduced by this change.

Not verified: esp32p4/esp32s3 board-level flashing and serial output — this change is architecture-independent apps-layer code, and this machine has no esp32p4 riscv32 cross toolchain installed.

fangpeina and others added 4 commits August 24, 2026 20:28
Example in init.rc:
  echo "start" && hello && echo "done"
  ls /missing || echo "not found"
  echo "A" && echo "B" || echo "fallback"

Signed-off-by: fangpeina <fangpeina@xiaomi.com>
Add built-in property sys.boot.reason, which allows action triggers to be
executed on specific reset cause.

For example:
  ```
  on property:sys.boot.reason=cpu_soft_reset(bootloader)
      echo "bootloader mode ..."
      start fastboot
  ```

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
init.rc

  on init && property:sys.boot.reason=watchdog,4
     ...

  on init && property:sys.boot.reason=bootloader|recovery|thermal
     ...

Test

  cause->cause = BOARDIOC_RESETCAUSE_CPU_RWDT;
  cause->flag  = 4
  init_main: setprop key:sys.boot.reason value:watchdog,4

  cause->cause = BOARDIOC_RESETCAUSE_CPU_SOFT;
  cause->flag  = BOARDIOC_SOFTRESETCAUSE_ENTER_BOOTLOADER;
  init_main: setprop key:sys.boot.reason value:bootloader

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
Add the missing mapping entries in the resetflag[] array to prevent
potential NULL pointer dereference when accessing reset.flag.

The resetflag array uses designated initializers and must have entries
for all BOARDIOC_SOFTRESETCAUSE_* values to avoid array holes.

Reported by: xuchuntian@xiaomi.com
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
@JianyuWang0623

Copy link
Copy Markdown
Contributor Author

CI failure investigation

The check job failure (action.c:117:4: error: Bad left brace alignment) is not caused by this PR's commits. It is a false positive from a regression in apache/nuttx's tools/nxstyle (commit bdeb262b8d3, "tools/nxstyle: indent code against its enclosing brace, not modulo 4", merged 2026-07-27).

Evidence:

  1. The flagged line (system/nxinit/action.c:117) is outside the diff of all 4 commits in this PR — the hunks in this PR only touch lines 223+, 315+, 326+, 361+, 377+, 435+.
  2. Rebuilding nxstyle from current apache/master and running it against the pre-PR baseline (commit 0923948d80, before any commit in this PR) reproduces the identical error on the identical line — the code this PR is based on already fails under the new tool.
  3. The new rule's exception for iterator/critical-section macros (prevlastcode == ')' && indent == prevcodeindent + 2) only correctly handles single-line macro invocations. For multi-line list_for_every_entry(...) calls where the closing ) is on a continuation line, it computes the wrong expected indent and flags a false "Bad left brace alignment". This pattern is used throughout the codebase, e.g. apache/nuttx's own drivers/vhost/vhost.c (lines 138-140, 237-239, 278-280, 346-348, 388-390, 408-410) hits the same false positive with the same nxstyle build.

Since tools/checkpatch.sh -g (as invoked by CI, without -r) lints the whole file rather than just the diff range, this pre-existing false positive surfaces on any PR touching action.c, unrelated to what the PR actually changes.

No code change is needed in this PR. Marking ready for review; CI should pass once the upstream nxstyle regression is fixed (or if maintainers choose to bypass/merge despite the known tool issue).

@JianyuWang0623
JianyuWang0623 marked this pull request as ready for review August 24, 2026 13:38
@fdcavalcanti

Copy link
Copy Markdown
Contributor

Very nice! Please fix CI error

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool thank you @JianyuWang0623 the change is good, but also please fix caught syntax issues as we are here and we have them fixed for future :-)

@JianyuWang0623

Copy link
Copy Markdown
Contributor Author

@cederom @fdcavalcanti
A fix is already available: apache/nuttx#19555 (comment)

cederom
cederom previously approved these changes Aug 24, 2026

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @JianyuWang0623 :-)

We may want to wait for fixes from apache/nuttx#19555 or merge if other CI checks pass when in hurry in that case :-)

@JianyuWang0623

Copy link
Copy Markdown
Contributor Author

Thank you @JianyuWang0623 :-)

We may want to wait for fixes from apache/nuttx#19555 or merge if other CI checks pass when in hurry in that case :-)

@cederom Would be nice if we could ignore this false‑positive. Some in‑flight PRs need this PR(#3751) merged first.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@JianyuWang0623 fix typo error:

/home/runner/work/nuttx-apps/nuttx-apps/apps/system/nxinit/init.c:119: unkown ==> unknown
/home/runner/work/nuttx-apps/nuttx-apps/apps/system/nxinit/init.c:130: unkown ==> unknown

codespell flagged this in PR apache#3751 CI:
  system/nxinit/init.c:119: unkown ==> unknown
  system/nxinit/init.c:130: unkown ==> unknown

Both entries were introduced by the resetcause-for-triggers commit and
are unrelated to the earlier nxstyle regression already discussed on
the PR.

Assisted-by: GitHubCopilot:claude-sonnet-5
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
@JianyuWang0623

Copy link
Copy Markdown
Contributor Author

Root cause of the risc-v-03 CI failure identified and fixed upstream, unrelated to this PR's changes.

What happened: nuttx#19952 (boards: update LVGLTERM requirements) and nuttx-apps#3742 (examples/lvglterm: replace PIPES with PSEUDOTERM) were merged 4 seconds apart:

  • #19952 explicitly added CONFIG_PSEUDOTERM=y to three board defconfigs (esp32p4-tab5/lvgl_term, esp32s3-m5-cardputer/lvglterm, linum-stm32h753bi/lvglterm{,_kbda}) so lvglterm would build, since at that point the app still relied on plain pipes.
  • #3742 (merged seconds later) switched lvglterm to run the shell on a pseudo-terminal instead of pipes, and made EXAMPLES_LVGLTERM do select PSEUDOTERM, making the explicit line redundant. It also left CONFIG_PIPES=y as dead weight in those same defconfigs from the old pipe-based implementation.

Neither PR's author was aware of the other at merge time, so the redundant lines were never cleaned up. testbuild.sh runs refresh.sh (savedefconfig normalization) and diffs it against the committed defconfig — since both lines are now implied rather than needed, the diff comes back non-empty and the tree-cleanliness check fails. This PR's CI run happened to land on a nuttx SHA right after #19952 merged, so it caught the window.

Fix: opened apache/nuttx#19963 to drop the two now-redundant options from all four affected defconfigs. Verified refresh.sh --silent now reports a clean diff and all four configs (esp32p4-tab5, esp32s3-m5-cardputer, linum-stm32h753bi x2) still build successfully across their respective toolchains, plus checkpatch.sh passes on all four files.

This is unrelated to system/nxinit and doesn't block this PR's merge.

@linguini1

Copy link
Copy Markdown
Contributor

So do applications need to adapt and use the BOARDIOC command to set reset cause for the reason field to work?

What happens if no reset cause is given, is the default behaviour to not trigger?

This new functionality should be documented, please!

@linguini1 linguini1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide documentation about the new features.

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.

7 participants