1148 Commits

Author SHA1 Message Date
wangjianyu3
0f5a786347 nshlib: Fix stack-buffer-overflow of nsh_redirect()
Board/Config/Command
  sim:nsh
  cat < /etc/init.d/rc.sysinit

Problem
  The NSH hangs after exec the test command.
  `nsh_redirect()` needs to save three fd but array length is only two, stack buffer overflowed!

Related: https://github.com/apache/nuttx-apps/pull/2469

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-10-13 15:48:57 +08:00
Bowen Wang
6f85062019 nshlib/nsh_parse: support use \" and \' to pass " and ' to arguments
Now we can pass symbol " and ' to the nsh command arguments

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-11 19:22:57 +08:00
Bowen Wang
0b8a52379c nsh_syscmds/rpmsg: include <nuttx/rpmsg/rpmsg.h> to fix compile error
nsh_syscmds.c: In function ‘cmd_rpmsg_once’:
nsh_syscmds.c:567:13: error: ‘RPMSGIOC_PANIC’ undeclared (first use in this function)
  567 |       cmd = RPMSGIOC_PANIC;
      |             ^~~~~~~~~~~~~~
nsh_syscmds.c:567:13: note: each undeclared identifier is reported only once for each function it appears in
nsh_syscmds.c:571:13: error: ‘RPMSGIOC_DUMP’ undeclared (first use in this function); did you mean ‘FIOC_DUMP’?
  571 |       cmd = RPMSGIOC_DUMP;

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-11 14:59:22 +08:00
Yongrong Wang
568310858b nsh_syscmds.c: upadate rpmsg ping help
1. struct rpmsg_ping_s ack change to cmd;
2. Update the help descrption based on the new cmd
   meaning;

Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-11 14:59:22 +08:00
wangmingrong
c2597504e0 nsh: Fix PS printing misalignment
When the stack is allocated in megabytes, printing seven bits of ps will not align

Signed-off-by: wangmingrong <wangmingrong@xiaomi.com>
2024-10-08 07:58:51 +02:00
wanggang26
d407670115 resetcause:restore to factory, factory to factory_inquiry
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2024-10-07 05:10:19 +08:00
anjiahao
c916f163f2 nsh:support wait command to wait task exit.
usage like:
  nsh> sleep 10 &
  [5:100]
  nsh>set pid1=$!
  nsh>sleep 15 &
  [6:100]
  nsh>set pid2=$!
  nsh>wait $pid1 $pid2

'wait' command will block nsh from running until pid1 and pid2 finish.
This is useful for parallel requirements to perform certain tasks

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-02 21:13:54 +08:00
dulibo1
b20f023ab7 pmconfig:fix coverity scan issue which is unsigned_compare
Signed-off-by: dulibo1 <dulibo1@xiaomi.com>
2024-09-30 18:31:03 +08:00
dongjiuzhu1
97a33e7491 nshlib: support dd with conv=notrunc,nocreat
refs to: https://man7.org/linux/man-pages/man1/dd.1.html

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-09-30 14:16:40 +08:00
anjiahao
5dcfe93a95 nsh:support $! get last pid use nsh run
test:
    nsh> hello &
    hello [4:100]
    nsh> Hello, World!!

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

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-09-28 11:28:15 +08:00
yanghuatao
909e479d7f toolchain/ghs: Fix green hills toolchain build Vela warnings
Signed-off-by: yanghuatao <yanghuatao@xiaomi.com>
2024-09-28 10:47:32 +08:00
Huang Qi
dca6c0cd91 nshlib: Add missing newline in cmd_pidof output
Add newline at the end of cmd_pidof to show nsh
prompt correctly.

Before this patch:
```
nsh> pidof wifi
3 nsh>
```

After:
```
nsh> pidof wifi
3
nsh>
```
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2024-09-27 23:24:22 +08:00
Michal Lenc
f81a094283 nshlib: add support for pkill command
This command looks through the currently running processes and kills
the those that match the selection criteria. This way system can send
signal to processes by name and without knowing the IDs.

Example (kill application hello):
	pkill -15 hello

The command can be turned off by NSH_DISABLE_PKILL option and depends
on FS_PROCFS.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2024-09-23 08:40:54 +08:00
wanggang26
bae15dfd58 nshlib: enable O_CLOEXEC explicit to avoid potential fd leak
leaking here means fork/vfork will duplicate fd without O_CLOEXEC flag
to the child process.

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2024-09-23 08:34:16 +08:00
wanggang26
864371cc10 nshlib:enable O_CLOEXEC expliciti to avoid potential fd leak
leaking here means fork/vfork will duplicate fd without O_CLOEXEC flag
to the child process.

Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2024-09-22 01:44:06 +08:00
wangjianyu3
aaf4e9d480 nshlib: Support "-f" option for command "rm"
In below two cases "rm" command with "-f" option will return 0:
  a. Bad arguments
  b. File not exists

Following "rm" of GNU coreutils 8.32

GNU coreutils
  $ rm --version
  rm (GNU coreutils) 8.32
  Copyright (C) 2020 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.

  Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
  and Jim Meyering.

  $ rm /FILE_NOT_EXISTS
  rm: cannot remove '/FILE_NOT_EXISTS': No such file or directory
  $ echo $?
  1

  $ rm -f
  $ echo $?
  0

  $ rm -f /FILE_NOT_EXISTS
  $ echo $?
  0

Without this patch
  nsh> rm
  nsh: rm: missing required argument(s)

  nsh> rm /FILE_NOT_EXISTS
  nsh: rm: unlink failed: 2
  nsh> echo $?
  1

With this patch
  nsh> rm -f
  nsh> echo $?
  0

  nsh> rm -f /FILE_NOT_EXISTS
  nsh> echo $?
  0

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-09-19 03:45:37 +08:00
chao an
8ad39537dd nshlib: move g_builtin_prompt from data to rodata
1. move g_builtin_prompt from data to rodata
2. add g_ prefix to some global variables

Signed-off-by: chao an <anchao@lixiang.com>
2024-09-05 09:49:44 +08:00
meijian
c91a1817aa netlib/setroute: fix dhcpd stack-overflow when calls netlib_set_dripv4addr with debug info-level
Signed-off-by: meijian <meijian@xiaomi.com>
2024-08-26 01:54:46 +08:00
zhangshuai39
6d52a0fbc1 netutils/netlib: Encapsulate the dhcp configuration network code and modify at all calling locations
The DHCP configuration network code is called multiple times, so it is encapsulated

Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
2024-08-23 22:10:04 +08:00
guoshichao
573008244c greenhills: fix the variable unused warning
"nsh_parse.c", line 1565: warning #550-D: variable "quoted" was set but never
          used
    bool quoted;
         ^

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-08-20 02:50:13 +08:00
Shoukui Zhang
7df20ea1bb nshlib: Optimize the size of the recursive stack
Signed-off-by: Shoukui Zhang <zhangshoukui@xiaomi.com>
2024-08-16 19:48:18 +08:00
Marco Casaroli
0797ee2312 fix(nsh_parse): handle env variables correctly
The PR #2469 broke handling of environment variables because an
error in the if/else if control flow.

This fixes it.
2024-08-10 11:08:18 -03:00
Marco Casaroli
e46347ec1b nsh_fileapp: handle input redirection 2024-08-08 19:19:18 -03:00
Marco Casaroli
8fba726a7d feat(nsh_cat): allow cat to read from stdin
Now, if we run cat without arguments, it will just read from stdin.

It can be used with redirect like `cat < infile > outfile`.
2024-08-08 19:19:18 -03:00
Marco Casaroli
4104019e1c feat(nsh): add console read
This allows programs such as `cat` to read from the console (that could be redirected).
2024-08-08 19:19:18 -03:00
Marco Casaroli
96100b30f2 feat(nsh): input (stdin) redirection
This adds support for `<` to redirect input on nsh commands.
2024-08-08 19:19:18 -03:00
chenrun1
ffcee721ce nshlib/ddcmd:Fixed NSH_CMDOPT_DD_STATS output format warning in 64-bit environment
In file included from nsh_ddcmd.c:44:
nsh_ddcmd.c: In function 'cmd_dd':
nsh_ddcmd.c:456:20: error: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'uint64_t' {aka 'long unsigned int'} [-Werror=format=]
  456 |   nsh_output(vtbl, "%llu bytes copied, %u usec, ",
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  457 |              total, (unsigned int)elapsed);
      |              ~~~~~
      |              |
      |              uint64_t {aka long unsigned int}
nsh_console.h:55:49: note: in definition of macro 'nsh_output'
   55 | #  define nsh_output(v, ...)   (v)->output(v, ##__VA_ARGS__)
      |                                                 ^~~~~~~~~~~
nsh_ddcmd.c:456:24: note: format string is defined here
  456 |   nsh_output(vtbl, "%llu bytes copied, %u usec, ",
      |                     ~~~^
      |                        |
      |                        long long unsigned int
      |                     %lu
cc1: all warnings being treated as errors

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-08-07 15:04:39 +08:00
chenrun1
f126adb5fc nshlib/fscmds:Fix write overflow during cp -r process
Summary:
      do
        {
          nbyteswritten = write(wrfd, iobuffer, nbytesread);
          if (nbyteswritten >= 0)

The write return type is ssize_t, which is different in size from the originally declared type, so the unified type is ssize_t

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-08-06 19:30:12 -03:00
Denis Ryndine
a15403958d nsh_dd: fix lseek return check.
- As was merged in commit 1852731df87e5895e299b74aabdb1984b7e3f795 on
   dd_main.c: lseek returns -1 on error.

   Should be consistent in nsh_ddcmd.c and nsh_main.c.
2024-08-01 09:52:03 +08:00
buxiasen
b9a7353a43 add include to handle when PM_NDOMAINS from pm.h
Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-05-18 22:08:14 +08:00
Alan Carvalho de Assis
b4ec000dec apps/nsh_commands: Use quit to poweroff
Currently new users when try to run NuttX using the SIM
get stuck into simulator, because they have no idea that
poweroff command is used to leave it. Let use KISS approach
and use quit as an alias to poweroff command.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
2024-04-13 15:17:50 +08:00
Alan Carvalho de Assis
8d14f4eaac apps/nshlib: Never disable HELP and ? 2024-04-06 13:34:56 +08:00
chao an
20c90d5b43 nshlib/irqaff: add irq affinity command
add support for set an IRQ affinity to CPUs by software

qemu-armv7a/bmp: switch uart irq to different CPUs

nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK           STACK   USED  FILLED COMMAND
    0     0   0 FIFO     Kthread   - Ready              0000000000000000 004080 000536  13.1%  CPU0 IDLE
    1     1 192 RR       Kthread   - Waiting  Semaphore 0000000000000000 004032 000296   7.3%  hpwork 0x4013f51c 0x4013f530
    2     2 100 RR       Task      - Running            0000000000000000 004056 001168  28.7%  nsh_main
nsh> irqaff 33 0x2
nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK           STACK   USED  FILLED COMMAND
    0     0   0 FIFO     Kthread   - Ready              0000000000000000 004080 000736  18.0%  CPU1 IDLE
    1     1 192 RR       Kthread   - Waiting  Semaphore 0000000000000000 004032 000296   7.3%  hpwork 0x4013f544 0x4013f558
    2     2 100 RR       Task      - Running            0000000000000000 004056 001288  31.7%  nsh_main
nsh> irqaff 33 0x4
nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK           STACK   USED  FILLED COMMAND
    0     0   0 FIFO     Kthread   - Ready              0000000000000000 004080 000736  18.0%  CPU2 IDLE
    1     1 192 RR       Kthread   - Waiting  Semaphore 0000000000000000 004032 000296   7.3%  hpwork 0x4013f56c 0x4013f580
    2     2 100 RR       Task      - Running            0000000000000000 004056 001168  28.7%  nsh_main
nsh> irqaff 33 0x8
nsh> ps
  PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK           STACK   USED  FILLED COMMAND
    0     0   0 FIFO     Kthread   - Ready              0000000000000000 004080 000736  18.0%  CPU3 IDLE
    1     1 192 RR       Kthread   - Waiting  Semaphore 0000000000000000 004032 000296   7.3%  hpwork 0x4013f594 0x4013f5a8
    2     2 100 RR       Task      - Running            0000000000000000 004056 001168  28.7%  nsh_main

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-29 16:16:07 +08:00
chenrun1
bac8aab243 cmd_cp:Skip mkdir when mkdir is not available
The behavior of mkdir is used during the cp -r process, and the mkdir behavior is skipped when mkdir is unavailable (this may cause unpredictable behavior of cp -r)

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-02-25 21:59:57 -08:00
chenrun1
e8470f7d0e cp:support -r recursive copies
The cp command supports recursive copying. Use "cp -r" to copy a folder.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-02-25 21:59:57 -08:00
chenrun1
bd1cc85ad4 dd:support "seek"
Now the dd command can use the "seek" parameter to adjust how many bytes need to be skipped before being written to the target.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2024-02-25 06:36:43 -08:00
Yanfeng Liu
31908b3128 nshlib/prompt: extend NSH prompt string management
Currently NSH prompt is defined at build time, thus improper for AMP cases
where the same NSH binary is used on different nodes as the same NSH prompt
shows on all nodes.

This patch attempts to support runtime prompt string population from ordered
sources:
  - the environment variable defined by NSH_PROMPT_ENV plus suffix
  - the NSH_PROMPT_STRING
  - the HOSTNAME plus suffix
The suffix is defined by NSH_PROMPT_SUFFIX so that to clearly separate the
command inputs.

Changes in `nshlib/`

- Kconfig:       add configs NSH_PROMPT_MAX/ENV/SUFFIX etc
- nsh.h:         adjust g_nshprompt defs, add nsh_update_prompt
- nsh_parse.c    relocate g_nshpromt to nsh_prompt.c
- nsh_init.c     revise to use nsh_update_prompt once
- nsh_session.c  revise to use methods in nsh_prompt.c
- Makefile       add nsh_prompt.c
- CMakeLists.txt add nsh_prompt.c

New additions in `nshlib/`

- nsh_prompt.c   prompt related data structures and methods.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-02-25 00:55:47 -08:00
Yanfeng Liu
3dd35cb549 nshlib/syscmds: guard help mesg for rpmsg ping
This patch guards help message for ping sub-command so that it only
shows when the sub-command is enabled.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-02-21 05:31:45 -08:00
wangyongrong
25e8c1c9c4 cmd rptun: Strip rpmsg ioctl and rptun ioctl.
cmd_rptun handles RPTUNIOC_START, RPTUNIOC_STOP, RPTUNIOC_RESET,
cmd_rpmsg handles the public ioctl commands part.

Signed-off-by: wangyongrong <wangyongrong@xiaomi.com>
2024-02-19 05:59:34 -08:00
chao an
05ffbabca7 nsh/uname: improve compatibility for non-GNU compilers
The alloca() function is machine- and compiler-dependent.  For certain
applications, its use can improve efficiency compared to the use of
malloc(3) plus free(3).  In certain cases, it can also simplify memory
deallocation in applications that use longjmp(3) or siglongjmp(3).
Otherwise, its use is discouraged.

Signed-off-by: chao an <anchao@lixiang.com>
2024-02-19 03:49:22 -08:00
Ville Juven
0f31a8d98e nsh_fscmds.c: Fix CONFIG_FS_SHM -> CONFIG_FS_SHMFS
The define macro was wrong
2024-01-31 08:52:05 -08:00
chao an
0968264ac0 nshlib: fix build break if CONFIG_CPP_HAVE_VARARGS if undefined
ctc E272: ["nsh_ddcmd.c" 197/7] undeclared identifier "vtbl"

Signed-off-by: chao an <anchao@lixiang.com>
2024-01-30 20:44:05 -08:00
wangyongrong
32f269a079 nsh_syscmds: update rptun_ping to rpmsg_ping, add cmd_rpmsg.
To support rpmsg ioctl, add cmd_rpmsg function, and update rptun ping to rpmsg ping.
depends on apache/nuttx#11618

Signed-off-by: wangyongrong <wangyongrong@xiaomi.com>
2024-01-30 20:42:49 -08:00
yangjian11
09122debe9 nsh_timcmds: display date using set format
Reference: https://www.geeksforgeeks.org/date-command-linux-examples/?ref=gcse#9-list-of-format-specifiers-used-with-date-command

Signed-off-by: yangjian11 <yangjian11@xiaomi.com>
2024-01-26 19:13:25 -08:00
Michal Lenc
61d1f37f5f nsh_timcmds: show RTC time in timedatectl command only if CONFIG_RTC_DRIVER is set
NuttX build fails otherwise as required structures are not included.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2024-01-26 09:45:03 -08:00
Junbo Zheng
c699b05f74 nshlib: add expr command support
It is a mini version for the `expr` command, which implements the features of addition, subtraction, multiplication, division and mod.

Reference: https://www.geeksforgeeks.org/expr-command-in-linux-with-examples/

bl2>
bl2> expr 1 + 2
3
bl2> expr
Usage: expr <operand1> <operator> <operand2>
bl2> expr 5 - 2
3
bl2> set hello 10
bl2> expr $hello - 2
8
bl2> expr 8 a 9
Unknown operator
bl2> expr 20 / 5
4
bl2> expr 10 % 4
2
bl2> expr 10 / 0
operand2 invalid
bl2>
bl2> expr mi + 100
invalid parameter
bl2> expr 100 + mi
invalid parameter
bl2> expr 100 + 0
100

Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2024-01-25 18:12:28 -08:00
Yanfeng Liu
596328ccca nshcmd/rptun: align help mesg with impl
the ping subcommand is guarded with RPTUN_PING in its handler.
its help mesg should have same guard to avoid confusing user.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-01-25 18:11:39 -08:00
wangyongrong
7047fa5bde nsh syscmds: add CONFIG_RPTUN_PING when use rptun_ping
Signed-off-by: wangyongrong <wangyongrong@xiaomi.com>
2024-01-13 02:15:55 -08:00
fangxinyong
385f707382 nshlib: move etc romfs mount from nsh to nuttx
This commit corresponds to apache/nuttx#11498 in nuttx.

Move etc romfs mount to sched/init for compatibility with kernel/protected mode.

changes:
- move etc romfs mount from nsh to Nuttx, but keep the script to parse and execute.
- move and rename the related CONFIG, move customized nsh_romfsimg.h to etc_romfs.c
  in boards, and no need declaration for romfs_img/romfs_img_len.

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2024-01-09 21:25:19 -08:00
Bowen Wang
2a08d17ed6 nsh_mmcmds: support "memdump -h/help" to show usage and sequence number
Compare with "cat /proc/memdump", use "memdump -h/help" is more
convenient.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-01-06 04:34:27 -08:00