2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_proccmds.c
|
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2015-11-28 22:03:21 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2014-02-27 18:11:24 +01:00
|
|
|
#include <fcntl.h>
|
2015-11-28 22:03:21 +01:00
|
|
|
#include <dirent.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <errno.h>
|
2021-06-01 05:12:41 +02:00
|
|
|
#include <signal.h>
|
apps/nshlib: add uptime command support
run uptime command on sim:
nsh>
nsh>
nsh> uptime
19:35:01 up 1:40, load average: 0.00, 0.00, 0.00
nsh>
nsh>
nsh> uptime -s
2022-09-16 17:54:26
nsh>
nsh>
nsh> uptime -p
up 1 hour, 40 minutes
nsh>
nsh>
nsh> uptime -h
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> uptime -abc
uptime: invalid option -- -abc
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> date
Fri, Sep 16 19:35:18 2022
nsh>
nsh>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-12 09:43:36 +02:00
|
|
|
#include <sys/sysinfo.h>
|
2023-08-11 13:09:16 +02:00
|
|
|
#include <sys/param.h>
|
apps/nshlib: add uptime command support
run uptime command on sim:
nsh>
nsh>
nsh> uptime
19:35:01 up 1:40, load average: 0.00, 0.00, 0.00
nsh>
nsh>
nsh> uptime -s
2022-09-16 17:54:26
nsh>
nsh>
nsh> uptime -p
up 1 hour, 40 minutes
nsh>
nsh>
nsh> uptime -h
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> uptime -abc
uptime: invalid option -- -abc
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> date
Fri, Sep 16 19:35:18 2022
nsh>
nsh>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-12 09:43:36 +02:00
|
|
|
#include <time.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-02-27 18:11:24 +01:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-12-01 13:41:30 +01:00
|
|
|
#ifndef CONFIG_NSH_PROC_MOUNTPOINT
|
|
|
|
# define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
|
2014-02-27 18:11:24 +01:00
|
|
|
#endif
|
|
|
|
|
apps/nshlib: add uptime command support
run uptime command on sim:
nsh>
nsh>
nsh> uptime
19:35:01 up 1:40, load average: 0.00, 0.00, 0.00
nsh>
nsh>
nsh> uptime -s
2022-09-16 17:54:26
nsh>
nsh>
nsh> uptime -p
up 1 hour, 40 minutes
nsh>
nsh>
nsh> uptime -h
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> uptime -abc
uptime: invalid option -- -abc
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> date
Fri, Sep 16 19:35:18 2022
nsh>
nsh>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-12 09:43:36 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
2023-06-10 18:44:11 +02:00
|
|
|
# ifndef FSHIFT
|
|
|
|
# define FSHIFT SI_LOAD_SHIFT
|
|
|
|
# endif
|
apps/nshlib: add uptime command support
run uptime command on sim:
nsh>
nsh>
nsh> uptime
19:35:01 up 1:40, load average: 0.00, 0.00, 0.00
nsh>
nsh>
nsh> uptime -s
2022-09-16 17:54:26
nsh>
nsh>
nsh> uptime -p
up 1 hour, 40 minutes
nsh>
nsh>
nsh> uptime -h
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> uptime -abc
uptime: invalid option -- -abc
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> date
Fri, Sep 16 19:35:18 2022
nsh>
nsh>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-12 09:43:36 +02:00
|
|
|
# define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
|
|
|
|
# define LOAD_INT(x) ((x) >> FSHIFT)
|
|
|
|
# define LOAD_FRAC(x) (LOAD_INT(((x) & (FIXED_1 - 1)) * 100))
|
|
|
|
#endif
|
|
|
|
|
2023-03-02 15:35:00 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
|
|
|
# define PS_SHOW_HEAPSIZE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_PSSTACKUSAGE
|
|
|
|
# define PS_SHOW_STACKSIZE
|
|
|
|
# ifdef CONFIG_STACK_COLORATION
|
|
|
|
# define PS_SHOW_STACKUSAGE
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-27 18:11:24 +01:00
|
|
|
/* The returned value should be zero for success or TRUE or non zero for
|
2011-03-18 20:46:25 +01:00
|
|
|
* failure or FALSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef int (*exec_t)(void);
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* This structure represents the parsed task characteristics */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
struct nsh_taskstatus_s
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
FAR const char *td_type; /* Thread type */
|
2015-11-30 13:58:56 +01:00
|
|
|
FAR const char *td_groupid; /* Group ID */
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
FAR const char *td_cpu; /* CPU */
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
2015-11-28 22:03:21 +01:00
|
|
|
FAR const char *td_state; /* Thread state */
|
|
|
|
FAR const char *td_event; /* Thread wait event */
|
|
|
|
FAR const char *td_flags; /* Thread flags */
|
|
|
|
FAR const char *td_priority; /* Thread priority */
|
|
|
|
FAR const char *td_policy; /* Thread scheduler */
|
|
|
|
FAR const char *td_sigmask; /* Signal mask */
|
2011-03-18 20:46:25 +01:00
|
|
|
};
|
2011-04-07 03:54:59 +02:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Status strings */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2017-03-07 22:10:17 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2015-11-28 22:03:21 +01:00
|
|
|
#if 0 /* Not used */
|
|
|
|
static const char g_name[] = "Name:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
static const char g_type[] = "Type:";
|
2015-11-30 13:58:56 +01:00
|
|
|
static const char g_groupid[] = "Group:";
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
static const char g_cpu[] = "CPU:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
static const char g_state[] = "State:";
|
|
|
|
static const char g_flags[] = "Flags:";
|
|
|
|
static const char g_priority[] = "Priority:";
|
|
|
|
static const char g_scheduler[] = "Scheduler:";
|
|
|
|
static const char g_sigmask[] = "SigMask:";
|
2015-07-23 18:15:57 +02:00
|
|
|
|
2022-07-04 10:31:39 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
2022-01-22 13:24:32 +01:00
|
|
|
static const char g_heapsize[] = "AllocSize:";
|
|
|
|
#endif /* CONFIG_DEBUG _MM && !CONFIG_NSH_DISABLE_PSHEAPUSAGE */
|
|
|
|
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-06-30 15:28:42 +02:00
|
|
|
static const char g_stacksize[] = "StackSize:";
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
static const char g_stackused[] = "StackUsed:";
|
|
|
|
#endif
|
2017-03-07 22:10:17 +01:00
|
|
|
#endif /* !CONFIG_NSH_DISABLE_PSSTACKUSAGE */
|
|
|
|
#endif /* !CONFIG_NSH_DISABLE_PS */
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-27 18:11:24 +01:00
|
|
|
/****************************************************************************
|
2016-06-30 15:28:42 +02:00
|
|
|
* Name: nsh_parse_statusline
|
2014-02-27 18:11:24 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
|
|
|
static void nsh_parse_statusline(FAR char *line,
|
|
|
|
FAR struct nsh_taskstatus_s *status)
|
2014-02-27 18:11:24 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Parse the task status.
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* 111111111122222222223
|
|
|
|
* 123456789012345678901234567890
|
2019-11-08 14:52:22 +01:00
|
|
|
* Name: xxxx... Task/thread name
|
|
|
|
* (See CONFIG_TASK_NAME_SIZE)
|
2015-11-28 22:03:21 +01:00
|
|
|
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
|
2015-11-30 13:58:56 +01:00
|
|
|
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
|
|
|
|
* PPID: xxxxx Parent thread ID
|
2019-11-08 14:52:22 +01:00
|
|
|
* State: xxxxxxxx,xxxxxxxxx {Invalid, Waiting, Ready, Running,
|
|
|
|
* Inactive}, {Unlock, Semaphore, Signal,
|
|
|
|
* MQ empty, MQ full}
|
2015-11-28 22:03:21 +01:00
|
|
|
* Flags: xxx N,P,X
|
|
|
|
* Priority: nnn Decimal, 0-255
|
2019-11-08 14:52:22 +01:00
|
|
|
* Scheduler: xxxxxxxxxxxxxx {SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,
|
|
|
|
* SCHED_OTHER}
|
2015-11-28 22:03:21 +01:00
|
|
|
* Sigmask: nnnnnnnn Hexadecimal, 32-bit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0 /* Not used */
|
2019-11-08 14:52:22 +01:00
|
|
|
/* Task name */
|
2015-11-28 22:03:21 +01:00
|
|
|
|
2019-11-08 14:52:22 +01:00
|
|
|
if (strncmp(line, g_name, strlen(g_name)) == 0)
|
|
|
|
{
|
|
|
|
/* Not used */
|
|
|
|
}
|
|
|
|
else
|
2015-11-28 22:03:21 +01:00
|
|
|
#endif
|
|
|
|
/* Task/thread type */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
if (strncmp(line, g_type, strlen(g_type)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the thread type */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status->td_type = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
2015-11-30 13:58:56 +01:00
|
|
|
else if (strncmp(line, g_groupid, strlen(g_groupid)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the Group ID */
|
|
|
|
|
|
|
|
status->td_groupid = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
else if (strncmp(line, g_cpu, strlen(g_cpu)) == 0)
|
|
|
|
{
|
|
|
|
/* Save the current CPU */
|
|
|
|
|
|
|
|
status->td_cpu = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
else if (strncmp(line, g_state, strlen(g_state)) == 0)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Save the thread state */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status->td_state = nsh_trimspaces(&line[12]);
|
|
|
|
|
|
|
|
/* Check if an event follows the state */
|
|
|
|
|
|
|
|
ptr = strchr(status->td_state, ',');
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
*ptr++ = '\0';
|
|
|
|
status->td_event = nsh_trimspaces(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strncmp(line, g_flags, strlen(g_flags)) == 0)
|
|
|
|
{
|
|
|
|
status->td_flags = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
|
|
|
else if (strncmp(line, g_priority, strlen(g_priority)) == 0)
|
|
|
|
{
|
2016-07-15 16:31:47 +02:00
|
|
|
FAR char *ptr = nsh_trimspaces(&line[12]);
|
|
|
|
status->td_priority = ptr;
|
2016-07-15 15:01:02 +02:00
|
|
|
|
|
|
|
/* If priority inheritance is enabled, use current pri, ignore base */
|
|
|
|
|
|
|
|
while (isdigit(*ptr))
|
|
|
|
{
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = '\0';
|
2015-11-28 22:03:21 +01:00
|
|
|
}
|
|
|
|
else if (strncmp(line, g_scheduler, strlen(g_scheduler)) == 0)
|
|
|
|
{
|
2019-11-08 14:52:22 +01:00
|
|
|
/* Skip over the SCHED_ part of the policy. Result is max 8 bytes. */
|
2015-11-28 22:03:21 +01:00
|
|
|
|
2019-11-08 14:52:22 +01:00
|
|
|
status->td_policy = nsh_trimspaces(&line[12 + 6]);
|
2015-11-28 22:03:21 +01:00
|
|
|
}
|
|
|
|
else if (strncmp(line, g_sigmask, strlen(g_sigmask)) == 0)
|
|
|
|
{
|
|
|
|
status->td_sigmask = nsh_trimspaces(&line[12]);
|
|
|
|
}
|
2014-02-27 18:11:24 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
2015-11-28 15:29:27 +01:00
|
|
|
* Name: ps_callback
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2015-11-28 22:03:21 +01:00
|
|
|
static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
|
|
|
|
FAR struct dirent *entryp, FAR void *pvarg)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(pvarg);
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
struct nsh_taskstatus_s status;
|
|
|
|
FAR char *filepath;
|
|
|
|
FAR char *line;
|
|
|
|
FAR char *nextline;
|
2014-02-27 18:11:24 +01:00
|
|
|
int ret;
|
2011-03-18 20:46:25 +01:00
|
|
|
int i;
|
2022-07-04 10:31:39 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
2022-07-17 08:05:23 +02:00
|
|
|
unsigned long heap_size = 0;
|
2022-01-22 13:24:32 +01:00
|
|
|
#endif
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2022-07-17 08:05:23 +02:00
|
|
|
unsigned long stack_size = 0;
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2022-07-17 08:05:23 +02:00
|
|
|
unsigned long stack_used = 0;
|
|
|
|
unsigned long stack_filled = 0;
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-06-30 15:28:42 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2016-06-30 15:28:42 +02:00
|
|
|
/* Task/thread entries in the /proc directory will all be (1) directories
|
|
|
|
* with (2) all numeric names.
|
2015-11-28 22:03:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!DIRENT_ISDIRECTORY(entryp->d_type))
|
|
|
|
{
|
|
|
|
/* Not a directory... skip this entry */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Check each character in the name */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
for (i = 0; i < NAME_MAX && entryp->d_name[i] != '\0'; i++)
|
2014-02-27 18:11:24 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
if (!isdigit(entryp->d_name[i]))
|
|
|
|
{
|
|
|
|
/* Name contains something other than a numeric character */
|
2014-02-27 18:11:24 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-27 18:11:24 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Set all pointers to the empty string. */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
status.td_type = "";
|
2015-11-30 13:58:56 +01:00
|
|
|
status.td_groupid = "";
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
status.td_cpu = "";
|
2015-11-30 13:58:56 +01:00
|
|
|
#endif
|
2015-11-28 22:03:21 +01:00
|
|
|
status.td_state = "";
|
|
|
|
status.td_event = "";
|
|
|
|
status.td_flags = "";
|
|
|
|
status.td_priority = "";
|
|
|
|
status.td_policy = "";
|
|
|
|
status.td_sigmask = "";
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Read the task status */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/status", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-02-08 01:17:54 +01:00
|
|
|
else
|
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
2013-02-08 01:17:54 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
if (ret >= 0)
|
2013-02-08 01:17:54 +01:00
|
|
|
{
|
2015-11-28 22:03:21 +01:00
|
|
|
/* Parse the task status. */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
nextline = vtbl->iobuffer;
|
|
|
|
do
|
2014-09-02 00:47:20 +02:00
|
|
|
{
|
2019-11-08 14:52:22 +01:00
|
|
|
/* Find the beginning of the next line and NUL-terminate the
|
2015-11-28 22:03:21 +01:00
|
|
|
* current line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line = nextline;
|
|
|
|
for (nextline++;
|
|
|
|
*nextline != '\n' && *nextline != '\0';
|
|
|
|
nextline++);
|
|
|
|
|
|
|
|
if (*nextline == '\n')
|
|
|
|
{
|
|
|
|
*nextline++ = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the current line */
|
|
|
|
|
|
|
|
nsh_parse_statusline(line, &status);
|
2014-09-02 00:47:20 +02:00
|
|
|
}
|
2015-11-28 22:03:21 +01:00
|
|
|
while (nextline != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, print the status information */
|
|
|
|
|
2023-03-02 15:35:00 +01:00
|
|
|
nsh_output(vtbl,
|
|
|
|
"%5s %5s "
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2023-03-02 15:35:00 +01:00
|
|
|
"%3s "
|
2016-02-19 22:33:32 +01:00
|
|
|
#endif
|
|
|
|
|
2023-03-02 15:35:00 +01:00
|
|
|
"%3s %-8s %-7s %3s %-8s %-9s %-8s ",
|
|
|
|
entryp->d_name, status.td_groupid,
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
status.td_cpu,
|
|
|
|
#endif
|
2015-11-30 13:58:56 +01:00
|
|
|
status.td_priority, status.td_policy, status.td_type,
|
2023-03-02 15:35:00 +01:00
|
|
|
status.td_flags, status.td_state, status.td_event,
|
|
|
|
status.td_sigmask);
|
2015-11-28 22:03:21 +01:00
|
|
|
|
2022-07-04 10:31:39 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
2022-01-22 13:24:32 +01:00
|
|
|
/* Get the Heap AllocSize */
|
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/heap", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
|
|
|
vtbl->iobuffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer,
|
|
|
|
IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
nextline = vtbl->iobuffer;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Find the beginning of the next line and NUL-terminate the
|
|
|
|
* current line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line = nextline;
|
|
|
|
for (nextline++;
|
|
|
|
*nextline != '\n' && *nextline != '\0';
|
|
|
|
nextline++);
|
|
|
|
|
|
|
|
if (*nextline == '\n')
|
|
|
|
{
|
|
|
|
*nextline++ = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the current line
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* 111111111122222222223
|
|
|
|
* 123456789012345678901234567890
|
|
|
|
* AllocSize: xxxx
|
|
|
|
* AllocBlks: xxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strncmp(line, g_heapsize, strlen(g_heapsize)) == 0)
|
|
|
|
{
|
2022-07-17 08:05:23 +02:00
|
|
|
heap_size = strtoul(&line[12], NULL, 0);
|
2022-01-22 13:24:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nextline != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2016-06-30 15:28:42 +02:00
|
|
|
/* Get the StackSize and StackUsed */
|
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/stack", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2016-06-30 15:28:42 +02:00
|
|
|
vtbl->iobuffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer,
|
|
|
|
IOBUFFERSIZE);
|
2016-06-30 15:28:42 +02:00
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
nextline = vtbl->iobuffer;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Find the beginning of the next line and NUL-terminate the
|
|
|
|
* current line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line = nextline;
|
|
|
|
for (nextline++;
|
|
|
|
*nextline != '\n' && *nextline != '\0';
|
|
|
|
nextline++);
|
|
|
|
|
|
|
|
if (*nextline == '\n')
|
|
|
|
{
|
|
|
|
*nextline++ = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the current line
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* 111111111122222222223
|
|
|
|
* 123456789012345678901234567890
|
|
|
|
* StackBase: xxxxxxxxxx
|
|
|
|
* StackSize: xxxx
|
|
|
|
* StackUsed: xxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strncmp(line, g_stacksize, strlen(g_stacksize)) == 0)
|
|
|
|
{
|
2022-07-17 08:05:23 +02:00
|
|
|
stack_size = strtoul(&line[12], NULL, 0);
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
else if (strncmp(line, g_stackused, strlen(g_stackused)) == 0)
|
|
|
|
{
|
2022-07-17 08:05:23 +02:00
|
|
|
stack_used = strtoul(&line[12], NULL, 0);
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
|
|
|
while (nextline != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2016-07-12 14:48:30 +02:00
|
|
|
if (stack_size > 0 && stack_used > 0)
|
2016-06-30 15:28:42 +02:00
|
|
|
{
|
2016-07-12 14:48:30 +02:00
|
|
|
/* Use fixed-point math with one decimal place */
|
|
|
|
|
|
|
|
stack_filled = 10 * 100 * stack_used / stack_size;
|
2016-06-30 15:28:42 +02:00
|
|
|
}
|
2016-07-11 01:49:58 +02:00
|
|
|
#endif
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2016-07-11 01:49:58 +02:00
|
|
|
|
2015-11-28 22:03:21 +01:00
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
|
|
|
/* Get the CPU load */
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
filepath = NULL;
|
2015-11-28 22:03:21 +01:00
|
|
|
ret = asprintf(&filepath, "%s/%s/loadavg", dirpath, entryp->d_name);
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2015-11-28 22:03:21 +01:00
|
|
|
vtbl->iobuffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
vtbl->iobuffer[0] = '\0';
|
2014-09-02 00:47:20 +02:00
|
|
|
}
|
2013-02-08 01:17:54 +01:00
|
|
|
}
|
2023-03-02 15:35:00 +01:00
|
|
|
#endif
|
2013-02-08 01:17:54 +01:00
|
|
|
|
2023-03-02 15:35:00 +01:00
|
|
|
#if defined(PS_SHOW_HEAPSIZE) || defined (PS_SHOW_STACKSIZE) || \
|
|
|
|
defined (PS_SHOW_STACKUSAGE) || defined (NSH_HAVE_CPULOAD)
|
|
|
|
nsh_output(vtbl,
|
|
|
|
#ifdef PS_SHOW_HEAPSIZE
|
|
|
|
"%08lu "
|
|
|
|
#endif
|
|
|
|
#ifdef PS_SHOW_STACKSIZE
|
|
|
|
"%06lu "
|
|
|
|
#endif
|
|
|
|
#ifdef PS_SHOW_STACKUSAGE
|
|
|
|
"%06lu "
|
|
|
|
"%3lu.%lu%%%c "
|
|
|
|
#endif
|
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
|
|
|
"%6s "
|
|
|
|
#endif
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
|
|
|
, heap_size
|
|
|
|
#endif
|
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
|
|
|
, stack_size
|
|
|
|
#endif
|
|
|
|
#ifdef PS_SHOW_STACKUSAGE
|
|
|
|
, stack_used,
|
|
|
|
stack_filled / 10,
|
|
|
|
stack_filled % 10,
|
|
|
|
(stack_filled >= 10 * 80 ? '!' : ' ')
|
|
|
|
#endif
|
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
|
|
|
, nsh_trimspaces(vtbl->iobuffer)
|
|
|
|
#endif
|
|
|
|
);
|
2015-11-28 22:03:21 +01:00
|
|
|
#endif
|
|
|
|
|
2019-11-08 14:52:22 +01:00
|
|
|
/* Read the task/thread command line */
|
2015-11-28 22:03:21 +01:00
|
|
|
|
|
|
|
filepath = NULL;
|
|
|
|
ret = asprintf(&filepath, "%s/%s/cmdline", dirpath, entryp->d_name);
|
|
|
|
|
|
|
|
if (ret < 0 || filepath == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
|
2015-11-28 22:03:21 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
|
|
|
|
free(filepath);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:39:33 +01:00
|
|
|
nsh_output(vtbl, "%s\n", nsh_trimspaces(vtbl->iobuffer));
|
2015-11-28 22:03:21 +01:00
|
|
|
return OK;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_exec
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_EXEC
|
2015-11-28 15:29:27 +01:00
|
|
|
int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
FAR char *endptr;
|
2011-06-17 20:20:14 +02:00
|
|
|
uintptr_t addr;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2011-06-17 20:20:14 +02:00
|
|
|
addr = (uintptr_t)strtol(argv[1], &endptr, 0);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (!addr || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2019-11-08 14:52:22 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
2022-09-19 13:29:57 +02:00
|
|
|
nsh_output(vtbl, "Calling %p\n", (void *)addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ((exec_t)addr)();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ps
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PS
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
|
2023-03-02 15:35:00 +01:00
|
|
|
nsh_output(vtbl, "%5s %5s "
|
2016-02-19 22:33:32 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2023-03-02 15:35:00 +01:00
|
|
|
"%3s "
|
2016-02-19 22:33:32 +01:00
|
|
|
#endif
|
2023-03-02 15:35:00 +01:00
|
|
|
"%3s %-8s %-7s %3s %-8s %-9s "
|
2023-03-29 10:15:48 +02:00
|
|
|
"%-16s "
|
2022-07-04 10:31:39 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
2023-03-02 15:35:00 +01:00
|
|
|
"%8s "
|
2022-01-22 13:24:32 +01:00
|
|
|
#endif
|
2016-07-01 21:45:00 +02:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
2023-03-02 15:35:00 +01:00
|
|
|
"%6s "
|
2016-07-13 15:06:32 +02:00
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
2023-03-02 15:35:00 +01:00
|
|
|
"%6s "
|
|
|
|
"%7s "
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
|
|
|
"%6s "
|
|
|
|
#endif
|
|
|
|
"%s\n",
|
|
|
|
"PID", "GROUP",
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
"CPU",
|
|
|
|
#endif
|
|
|
|
"PRI", "POLICY", "TYPE", "NPX", "STATE", "EVENT",
|
|
|
|
"SIGMASK",
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE)
|
|
|
|
"HEAP",
|
|
|
|
#endif
|
|
|
|
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
|
|
|
|
"STACK",
|
|
|
|
#ifdef CONFIG_STACK_COLORATION
|
|
|
|
"USED",
|
|
|
|
"FILLED",
|
2016-06-30 15:28:42 +02:00
|
|
|
#endif
|
2016-07-13 15:06:32 +02:00
|
|
|
#endif
|
2015-11-28 14:23:08 +01:00
|
|
|
#ifdef NSH_HAVE_CPULOAD
|
2023-03-02 15:35:00 +01:00
|
|
|
"CPU",
|
2014-02-27 18:11:24 +01:00
|
|
|
#endif
|
2023-03-02 15:35:00 +01:00
|
|
|
"COMMAND"
|
|
|
|
);
|
2015-11-28 22:03:21 +01:00
|
|
|
|
|
|
|
return nsh_foreach_direntry(vtbl, "ps", CONFIG_NSH_PROC_MOUNTPOINT,
|
|
|
|
ps_callback, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-11 13:09:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_pidof
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_NSH_DISABLE_PIDOF)
|
|
|
|
int cmd_pidof(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|
|
|
{
|
|
|
|
FAR const char *name = argv[argc - 1];
|
|
|
|
pid_t pids[8];
|
|
|
|
ssize_t ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = nsh_getpid(vtbl, name, pids, nitems(pids));
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtnosuch, argv[0], "task", name);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%d ", pids[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_kill
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_KILL
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_kill(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR char *ptr;
|
|
|
|
FAR char *endptr;
|
2011-03-18 20:46:25 +01:00
|
|
|
long signal;
|
|
|
|
long pid;
|
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
/* kill will send SIGTERM to the task in case no signal is selected by
|
|
|
|
* -<signal> option
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
if (argc == 3) /* kill -<signal> <pid> */
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2021-06-01 05:12:41 +02:00
|
|
|
/* Check incoming parameters.
|
|
|
|
* The first parameter should be "-<signal>"
|
|
|
|
*/
|
|
|
|
|
|
|
|
ptr = argv[1];
|
|
|
|
if (*ptr != '-' || ptr[1] < '0' || ptr[1] > '9')
|
|
|
|
{
|
|
|
|
goto invalid_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the signal number */
|
|
|
|
|
|
|
|
signal = strtol(&ptr[1], &endptr, 0);
|
|
|
|
|
|
|
|
/* The second parameter should be <pid> */
|
|
|
|
|
|
|
|
ptr = argv[2];
|
|
|
|
|
|
|
|
if (*ptr < '0' || *ptr > '9')
|
|
|
|
{
|
|
|
|
goto invalid_arg;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2021-06-01 05:12:41 +02:00
|
|
|
else if (argc == 2) /* kill <pid> */
|
|
|
|
{
|
|
|
|
/* uses default signal number as SIGTERM */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
signal = (long) SIGTERM; /* SIGTERM is always defined in signal.h */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
/* The first parameter should be <pid> */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
ptr = argv[1];
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-06-01 05:12:41 +02:00
|
|
|
if (*ptr < '0' || *ptr > '9')
|
|
|
|
{
|
|
|
|
goto invalid_arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2021-06-01 05:12:41 +02:00
|
|
|
/* invalid number of arguments */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
goto invalid_arg;
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:52:22 +01:00
|
|
|
/* Extract the pid */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
pid = strtol(ptr, &endptr, 0);
|
|
|
|
|
|
|
|
/* Send the signal. Kill return values:
|
|
|
|
*
|
|
|
|
* EINVAL An invalid signal was specified.
|
|
|
|
* EPERM The process does not have permission to send the signal to any
|
|
|
|
* of the target processes.
|
|
|
|
* ESRCH The pid or process group does not exist.
|
|
|
|
* ENOSYS Do not support sending signals to process groups.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (kill((pid_t)pid, (int)signal) == 0)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
goto invalid_arg;
|
|
|
|
|
|
|
|
case ESRCH:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnosuch, argv[0], "task", argv[2]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
case EPERM:
|
|
|
|
case ENOSYS:
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "kill", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid_arg:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_sleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_SLEEP
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR char *endptr;
|
2011-03-18 20:46:25 +01:00
|
|
|
long secs;
|
|
|
|
|
|
|
|
secs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!secs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2019-11-08 14:52:22 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-08-10 21:11:31 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
sleep(secs);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_usleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_USLEEP
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR char *endptr;
|
2011-03-18 20:46:25 +01:00
|
|
|
long usecs;
|
|
|
|
|
|
|
|
usecs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!usecs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2019-11-08 14:52:22 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2016-06-30 15:28:42 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
usleep(usecs);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
apps/nshlib: add uptime command support
run uptime command on sim:
nsh>
nsh>
nsh> uptime
19:35:01 up 1:40, load average: 0.00, 0.00, 0.00
nsh>
nsh>
nsh> uptime -s
2022-09-16 17:54:26
nsh>
nsh>
nsh> uptime -p
up 1 hour, 40 minutes
nsh>
nsh>
nsh> uptime -h
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> uptime -abc
uptime: invalid option -- -abc
Usage:
uptime [options]
Options:
-p, show uptime in pretty format
-h, display this help and exit
-s, system up since
nsh>
nsh>
nsh> date
Fri, Sep 16 19:35:18 2022
nsh>
nsh>
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-12 09:43:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_uptime
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
|
|
|
int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|
|
|
{
|
|
|
|
uint32_t updays;
|
|
|
|
uint32_t uphours;
|
|
|
|
uint32_t upminutes;
|
|
|
|
|
|
|
|
time_t current_time_seconds;
|
|
|
|
FAR struct tm *current_time;
|
|
|
|
|
|
|
|
struct sysinfo sys_info;
|
|
|
|
|
|
|
|
time_t uptime = 0;
|
|
|
|
|
|
|
|
bool pretty_format_opt = false;
|
|
|
|
bool system_load_opt = false;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
system_load_opt = true;
|
|
|
|
|
|
|
|
current_time_seconds = time(NULL);
|
|
|
|
current_time = localtime(¤t_time_seconds);
|
|
|
|
nsh_output(vtbl, "%02u:%02u:%02u ", current_time->tm_hour,
|
|
|
|
current_time->tm_min, current_time->tm_sec);
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "-p") == 0)
|
|
|
|
{
|
|
|
|
pretty_format_opt = true;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "-s") == 0)
|
|
|
|
{
|
|
|
|
sysinfo(&sys_info);
|
|
|
|
time(¤t_time_seconds);
|
|
|
|
current_time_seconds -= sys_info.uptime;
|
|
|
|
current_time = localtime(¤t_time_seconds);
|
|
|
|
nsh_output(vtbl, "%04u-%02u-%02u %02u:%02u:%02u\n",
|
|
|
|
current_time->tm_year + 1900, current_time->tm_mon + 1,
|
|
|
|
current_time->tm_mday, current_time->tm_hour,
|
|
|
|
current_time->tm_min, current_time->tm_sec);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "-h") != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "uptime: invalid option -- %s\n", argv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_output(vtbl, "Usage:\n");
|
|
|
|
nsh_output(vtbl, "uptime [options]\n");
|
|
|
|
|
|
|
|
nsh_output(vtbl, "Options:\n");
|
|
|
|
nsh_output(vtbl, "-p, show uptime in pretty format\n");
|
|
|
|
nsh_output(vtbl, "-h, display this help and exit\n");
|
|
|
|
nsh_output(vtbl, "-s, system up since\n");
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
sysinfo(&sys_info);
|
|
|
|
uptime = sys_info.uptime;
|
|
|
|
|
|
|
|
updays = uptime / 86400;
|
|
|
|
uptime -= updays * 86400;
|
|
|
|
uphours = uptime / 3600;
|
|
|
|
uptime -= uphours * 3600;
|
|
|
|
upminutes = uptime / 60;
|
|
|
|
|
|
|
|
nsh_output(vtbl, "up ");
|
|
|
|
|
|
|
|
if (updays)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%" PRIu32 " day%s, ", updays,
|
|
|
|
(updays > 1) ? "s" : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pretty_format_opt)
|
|
|
|
{
|
|
|
|
if (uphours)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%" PRIu32 " hour%s, ", uphours,
|
|
|
|
(uphours > 1) ? "s" : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_output(vtbl, "%" PRIu32 " minute%s", upminutes,
|
|
|
|
(upminutes > 1) ? "s" : "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%2" PRIu32 ":" "%02" PRIu32, uphours, upminutes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (system_load_opt)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, ", load average: %lu.%02lu, %lu.%02lu, %lu.%02lu",
|
|
|
|
LOAD_INT(sys_info.loads[0]), LOAD_FRAC(sys_info.loads[0]),
|
|
|
|
LOAD_INT(sys_info.loads[1]), LOAD_FRAC(sys_info.loads[1]),
|
|
|
|
LOAD_INT(sys_info.loads[2]), LOAD_FRAC(sys_info.loads[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|