1148 Commits

Author SHA1 Message Date
wangjianyu3
c3e628c45a nshlib/md5: Support reading from standard input
Sometimes users may want to calculate the MD5 of part of a file(e.g. check
partition contents for debug after flashing, size of which may be greater
than image). This can be done with the "dd" command and pipes, the "md5"
command just needs to support reading from standard input. For example:
`dd if=/dev/virtblk0 bs=512 count=1 | md5`.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-07-02 10:59:40 -03:00
dongjiuzhu1
fb6b3ddfd9 nsh/script: remove double close for output fd
the output fd had been closed in nsh_closeifnotclosed, so
remove double close.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-05-14 15:03:35 +08:00
dongjiuzhu1
5585c9d347 nshlib/dd: remove nsh dd cmd
using system/dd to instead nsh dd cmd
remove related to config, file.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-04-20 00:41:18 +08:00
Michal Lenc
0640d3f550 nshlib/nsh_fsutils.c: fix getpid_callback function read size
Commit 5330966 replaced local static buffer with malloc allocated
lib_get_pathbuffer. This however prevents the usage of sizeof(buffer)
in the follow up read function, therefore the read size has to be
specified as PATH_MAX - 1.

This fixes the functionality of pidof function call (and subsequently
pkill call that uses pidof).

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-03-27 09:25:21 +08:00
Roy Feng
cc3392452a nshlib: typo fixed in Kconfig
`NSH_DISABLE_NFSMOUNT` should be `NSH_DISABLE_NFSMOUNT`

Signed-off-by: Roy Feng <roy.feng@sony.com>
2025-03-24 20:33:26 +08:00
chao an
7f424c3e8d nshlib/builtin: check background task before restore the signal
fix crash if:
CONFIG_SCHED_WAITPID=n
CONFIG_SCHED_CHILD_STATUS=y

The old signal will be restored only when sigaction is saved to avoid invaild access.

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-02-12 10:30:05 -03:00
Xiang Xiao
5330966762 nshlib: Replace the big temp buffer with lib_get_[path|temp]buffer
to save the stack consumption

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-01-20 22:39:35 +08:00
Xiang Xiao
131d50ae9d nshlib: Remove the unnecessary temp path buffer
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-01-20 22:39:35 +08:00
wangjianyu3
f6462111b7 apps/nshlib: Reuse local variable for cmd_memdump
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-17 23:27:43 +08:00
zhangshoukui
339eeaa087 Use lib_get_tempbuffer Saving stack
Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
2025-01-16 22:49:19 +08:00
wangjianyu3
3108c27877 apps/nshlib: Save result and return ERROR if lib_get_tempbuffer() fails
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-16 22:48:34 +08:00
wangjianyu3
410ca7d88e apps/nshlib: Remove the deprecated config NSH_LINELEN
NSH_LINELEN is replaced by POSIX standard LINE_MAX.

https://github.com/apache/nuttx/pull/15541
https://github.com/apache/nuttx-apps/pull/2943

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-16 20:54:50 +08:00
wangjianyu3
a3049aae56 apps/nshlib: Using lib_get_tempbuffer() to save stack space
Comparison

  Config: "esp32s3-devkit:adb" with `CONFIG_LINE_MAX=512`

  Test CMD: `ls | cat | cat | cat`

  Without this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0002624/0008096 | 0002624/0008096 |
    |       sh.STACK | 0003360/0004008 | 0003360/0004008 |
    |     Free/Total |   355288/403116 |   355288/403116 |

  With this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0001616/0008096 | 0001616/0008096 |
    |       sh.STACK | 0002352/0004008 | 0002352/0004008 |
    |     Free/Total |   355288/403116 |   354760/403116 |

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-14 19:31:54 +08:00
wangjianyu3
26173597a9 apps/nshlib: replace CONFIG_NSH_LINELEN to LINE_MAX
LINE_MAX: https://github.com/apache/nuttx/pull/15344

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-14 19:31:54 +08:00
chao an
66231194be apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX
Applications should not depend on any properties of nshlib

Signed-off-by: chao an <anchao@lixiang.com>
2025-01-14 19:31:54 +08:00
Alin Jerpelea
27d8ae36ba nshlib: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-12-30 18:02:50 +08:00
wangjianyu3
39bd365787 nshlib/nsh_parse: Removing unnecessary value assigning about redirection
Coverity Log

  CID 1612757: (#1 of 1): UNUSED_VALUE
  assigned_value: The value -1 is assigned to param.fd_in here, but the stored value is overwritten before it can be used.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-12-19 14:11:55 +08:00
wangjianyu3
f1212a6065 nshlib/nsh_parse: Closing fds opened for redirection if necessary
Coverity Log

  CID 1612743: (#1 of 1): Resource leak (RESOURCE_LEAK)
  12. leaked_handle: The handle variable fd_out goes out of scope and leaks the handle.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-12-19 14:11:55 +08:00
chao an
9c8a96aa51 nshlib: add support for disable sigmask from ps command
before:

nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
    0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069616 Idle_Task
    1     0 224 FIFO     Kthread   - Waiting  Signal    0000000000000000 0067536 loop_task
    2     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067504 hpwork 0x400fcfe0 0x400fd028
    3     3 100 FIFO     Task      - Running            0000000000000000 0067536 nsh_main

after:

nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT       STACK COMMAND
    0     0   0 FIFO     Kthread   - Ready              0069616 Idle_Task
    1     0 224 FIFO     Kthread   - Waiting  Signal    0067536 loop_task
    2     0 224 FIFO     Kthread   - Waiting  Semaphore 0067504 hpwork 0x400fcfe0 0x400fd028
    3     3 100 FIFO     Task      - Running            0067536 nsh_main

Signed-off-by: chao an <anchao@lixiang.com>
2024-12-18 11:48:16 +08:00
chao an
e861ea8b53 nshlib/[cd|ls|pwd]: add support for local CWD(Current working directory)
This PR will still allow basic shell operations such as cd/ls/pwd to be used even when the environment is disabled.

Signed-off-by: chao an <anchao@lixiang.com>
2024-12-12 02:12:55 +08:00
wangjianyu3
687c1cacd9 nshlib/cmd_wait: Wait failed if "PROCFS/PROCES" not enabled
Env

  sim:nsh
  - CONFIG_FS_PROCFS_EXCLUDE_PROCES=y

Error

  nsh> sleep 5 &
  sh [4:100]
  nsh> wait 4
  nsh: wait: wait failed: 2

cmd_wait():

  snprintf(path, sizeof(path), "/proc/%d/status", tid);

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-14 23:20:39 +08:00
wangjianyu3
3c4ddd2802 nshlib/pipeline: Concat variable arguments failed
1. Without this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [5:100]

  nsh>

2. With this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [4:100]
  NuttX
  nsh>

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-14 15:51:40 +08:00
buxiasen
3a6ecb82b5 nshlib/nsh_parse: Fix variable arguments concat error of nsh_execute()
Without this patch

  nsh> set time 5
  nsh> echo $time
  5
  nsh> sleep $time &
  sh [5:100]
  nsh> nsh: sleep: missing required argument(s)

With this patch

  nsh> set time 5
  nsh> echo $time
  5
  nsh> sleep $time &
  sh [4:100]
  nsh> ps
    PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
      0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069616 Idle_Task
      1     0 224 FIFO     Kthread   - Waiting  Signal    0000000000000000 0067536 loop_task
      2     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067504 hpwork 0x501760e0 0x50176128
      3     3 100 FIFO     Task      - Running            0000000000000000 0067536 nsh_main
      4     4 100 FIFO     Task      - Waiting  Signal    0000000000000000 0067520 sh -c sleep

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-14 15:49:14 +08:00
anjiahao
dfbf408567 stdio.h:Remove unnecessary header file, app header change
stdio.h remove <nuttx/xxxx.h>  Some .c previously depended on
<nuttx/xxx.h> and needed to be added to .c instead of stdio.h

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-11-12 19:37:10 +08:00
wangjianyu3
b3e1077f87 nsh: Using sizeof() to get string length of char array
Changed from calling `strlen()` at runtime to using `sizeof()` at compile time.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
wangjianyu3
c052bd8377 nsh: Add pipeline support for nsh commandline
And nested pipeline supported.

Test
  1. Redirect in
    cat < /etc/init.d/rc.sysinit

  2. Simple pipeline
    ls | cat

  3. Nested pipeline
    ls | dd | cat | dd | cat

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
wangjianyu3
3da204c23e Pack parameters of nsh_execute() as struct nsh_exec_param_s
1. Input redirect flags currently is hardcode, passing by arguments maybe better.
2. Only support redirect to path_name, redirect to fd is needed for pipeline.

Test
  1. Redirect in
    cat < /etc/init.d/rc.sysinit

  2. Redirect with FIFO
    mkfifo /dev/testfifo
    cat /dev/testfifo &
    ls > /dev/testfifo

  3. NSH Params
    set name `uname`
    echo $name

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
wangjianyu3
27b5021e0d nshlib/cmd_dd: Retry if read() was interrupted
Without this patch

  nsh> ls /etc/group | dd | dd
  sh [13:100]
  sh [14:100]
  nsh: dd: read failed: 4
  nsh>

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:23 +08:00
wangjianyu3
ebc19a60ff nshlib/cmd_cat: Retry if nsh_read was interrupted by a signal
When read from stdio of child process through pipe, SIGCHLD received if child exits.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-10 22:03:07 +08:00
Ville Juven
f97304fa3f nsh_vars: Remove rogue sched_unlock()
There is no corresponding sched_lock() for this and therefore this causes
random crashes.

Found when running a long init script which utilizes shell variables in
SMP mode.
2024-11-06 01:35:42 +08:00
ligd
d9e178aad0 nshlib/console: fix compile error when open CONFIG_NSH_ALTCONDEV
CC:  mm_heap/mm_memalign.c misc/rwbuffer.c: In function 'rwb_readbytes':
misc/rwbuffer.c:1172:2: warning: #warning Not Implemented [-Wcpp]
 1172 | #warning Not Implemented
      |  ^~~~~~~
CC:  binfmt_unloadmodule.c In file included from nsh_console.c:40:
nsh_console.c: In function 'nsh_consoleredirect':
nsh_console.h:85:30: error: 'struct serialsave_s' has no member named 'cn_confd'; did you mean 'cn_infd'?
   85 | #  define INFD(p)      ((p)->cn_confd)
      |                              ^~~~~~~~
nsh_console.c:344:7: note: in expansion of macro 'INFD'
  344 |       INFD(ssave) = INFD(pstate);
      |       ^~~~
nsh_console.c: In function 'nsh_consoleundirect':
nsh_console.h:85:30: error: 'struct serialsave_s' has no member named 'cn_confd'; did you mean 'cn_infd'?
   85 | #  define INFD(p)      ((p)->cn_confd)
      |                              ^~~~~~~~
nsh_console.c:370:18: note: in expansion of macro 'INFD'
  370 |   INFD(pstate) = INFD(ssave);
      |                  ^~~~

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-10-29 23:43:04 +08:00
zhanghongyu
4cec3dad0d nsh_netcmds.c: handle the ENETUNREACH error number
before:
server> arp -a 10.0.1.4
nsh: arp: ioctl failed: 101

after:
server> arp -a 10.0.1.4
nsh: arp: no such ARP entry: 10.0.1.4

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2024-10-29 12:27:16 +08:00
buxiasen
790be6d639 nshlib/nsh_parse: use sh -c replace pthread detach when nsh background.
pthread & detach will still quit when parent task exit,
cause nsh_parse clone args leak. nsh should use task instead of thread

this case can reproduce the memory leak.
int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");
  system("sleep 1 &");
  return 0;
}

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-10-22 00:04:09 +08:00
wangjianyu3
e5a1bb1796 nshlib/dd: Add support for reading from/writing to standard input/output
Test
  1. Input from stdin and output to stdout
       Keyboard input: 12345AAAAABBBBB
    nsh> dd bs=5
    1234512345AAAAAAAAAABBBBBBBBBB

  2. Input from file and output to stdout
    nsh> dd if=/etc/init.d/rc.sysinit
    mkrd -m 2 -s 512 1024
    mkfatfs /dev/ram2
    mount -t vfat /dev/ram2 "/tmp"

  3. Input from stdin and output to file
       Keyboard input: QWERT
    dd of=/data/dd_stdout bs=5

    Then, cat the output file in host (based on HostFS):
    $ cat ./dd_stdout
    QWERT

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-10-19 14:07:32 +08:00
zhengyu9
697e3bed0a apps/nshlib/cmd_cd: Modify the logic of cd
Modify the implementation logic of changing directory.
There's an error handling the path incluing "../" or "./"

bug examples:

nsh> cd proc/self/group
nsh> cd ../..
nsh: cd: chdir failed: 2

nsh> pwd
/proc/self
nsh> ls
/proc/self:
 status
 cmdline
 stack
 group/
nsh> cd ./group
nsh: cd: chdir failed: 2

Signed-off-by: zhengyu9 <zhengyu9@xiaomi.com>
2024-10-17 18:06:48 +08:00
yinshengkai
59c21c7aee nsh: support watch command
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-17 18:06:32 +08:00
wangjianyu3
c5c18a2997 nshlib/cmd_cat: Avoid casting -1 to size_t as count of nsh_write()
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-10-17 08:55:51 +08:00
yinshengkai
15b081f02d nshlib: fix top command sorting result error
qsort expects the return value to be -1,0,1

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-17 00:00:51 +08:00
buxiasen
4285c4df73 nsh/ps: fix -heap cut more than 10MB
Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-10-16 18:23:54 +08:00
anjiahao
6979fca4dc nshlib: Support top command
show thread sort by cpuloading:
use `top [-n <num> ][ -d <delay>] [ -p <pidlist>] [ -h ]`

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-16 18:23:54 +08:00
anjiahao
6951b64038 nshlib: Reconstruct ps command for top command
divide data collection and data output into two parts

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-16 18:23:54 +08:00
yinshengkai
b90899b1d5 nshlib: ps supports noheap parameter
Obtaining the heap usage size requires traversing all memory.
When the number of threads is large, it will become very slow.

ps -heap
PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK              HEAP  STACK   USED  FILLED COMMAND
  0     0   0 FIFO     Kthread   - Ready              0000000000000000  4338432 2162672 0005196   0.2%  Idle_Task

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-16 18:23:54 +08:00
zhushiqshi
a655e067ef apps: added pid list for ps command.
Signed-off-by: zhushiqshi <zhushiqshi@xiaomi.com>
2024-10-16 18:23:54 +08:00
chao an
43d634ce00 nsh/fsutils: fix build break if CONFIG_NSH_DISABLE_PKILL not disabled
arm-none-eabi/bin/ld: apps/libapps.a(nsh_proccmds.c.obj): in function `cmd_pkill':
apps/nshlib/nsh_proccmds.c:861:(.text.cmd_pkill+0x38): undefined reference to `nsh_getpid'

Signed-off-by: chao an <anchao@lixiang.com>
2024-10-16 14:32:13 +08:00
yangguangcai
49941f1d57 cmd_date:Set optind to zero in the error case.
Signed-off-by: yangguangcai <yangguangcai@xiaomi.com>
2024-10-16 12:40:04 +08:00
Bowen Wang
4af9653b1e nshlib/nsh_parse: support use \\ to pass '\' to argument
Now the nsh can use \\ to pass '\' to the command

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-16 10:21:30 +08:00
ouyangxiangzhen
44d2250b6e apps: Fix compilation error after nuttx/arch.h excluded.
This patch fix compilation error after excluding nuttx/arch.h.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-10-16 07:12:40 +08:00
anjiahao
345e303133 nshlib:Add macro restrictions to code that uses floating point numbers
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-16 07:11:02 +08:00
yinshengkai
2ea062642f nshlib: Add startup process buried points
Used for system startup time profiling

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-15 12:34:09 +08:00
wangjianyu3
2bfe2c2ed0 nshlib: Fix a typo error of redirect
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-10-13 15:48:57 +08:00