Skip to content

Commit 8d2a7e3

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
nshlib: Add ppid support for command ps
Add parent process ID support for command ps, get from "/proc/<PID>/group/status:Parent". For example nsh> sh nsh> ps PID PPID GROUP CPU PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK COMMAND ... ... 3 0 3 --- 100 RR Task - Waiting Signal 0000000000000000 0008136 nsh_main ... ... 9 3 9 0 100 RR Task - Running 0000000000000000 0004064 sh nsh> sleep 100 & sh [10:100] nsh> ps PID PPID GROUP CPU PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK COMMAND ... ... 3 0 3 --- 100 RR Task - Waiting Signal 0000000000000000 0008136 nsh_main ... ... 9 3 9 0 100 RR Task - Running 0000000000000000 0004064 sh 10 9 10 --- 100 RR Task - Waiting Signal 0000000000000000 0004040 sh -c sleep Signed-off-by: wangjianyu3 <[email protected]>
1 parent 7cc7cb8 commit 8d2a7e3

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

nshlib/nsh_proccmds.c

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
****************************************************************************/
2626

2727
#include <nuttx/config.h>
28+
#include <nuttx/sched.h>
2829

2930
#include <stdio.h>
3031
#include <stdlib.h>
@@ -100,6 +101,7 @@ struct nsh_taskstatus_s
100101
#endif
101102
FAR char *td_cmdline; /* Command line */
102103
int td_pid; /* Task ID */
104+
int td_ppid; /* Parent task ID */
103105
#ifdef NSH_HAVE_CPULOAD
104106
FAR const char *td_cpuload; /* CPU load */
105107
#endif
@@ -141,6 +143,7 @@ static const char g_scheduler[] = "Scheduler:";
141143
#ifndef CONFIG_NSH_DISABLE_PSSIGMASK
142144
static const char g_sigmask[] = "SigMask:";
143145
#endif
146+
static const char g_ppid[] = "Parent:";
144147
# ifdef PS_SHOW_HEAPSIZE
145148
static const char g_heapsize[] = "AllocSize:";
146149
# endif /* PS_SHOW_HEAPSIZE */
@@ -257,6 +260,28 @@ static void nsh_parse_statusline(FAR char *line,
257260
}
258261
#endif
259262
}
263+
264+
static void nsh_parse_gstatusline(FAR char *line,
265+
FAR struct nsh_taskstatus_s *status)
266+
{
267+
/* Parse the group status.
268+
*
269+
* Format:
270+
*
271+
* 111111111122222222223
272+
* 123456789012345678901234567890
273+
* Main task: nnnnn PID
274+
* Parent: nnnnn Parent PID
275+
* Flags: 0x** Group flags, See GROUP_FLAG_*
276+
* Members: nnnn... Count of members
277+
* Member IDs: nnnnn,nnnnn,... List of members{PID0, PID1, ...)
278+
*/
279+
280+
if (strncmp(line, g_ppid, strlen(g_ppid)) == 0)
281+
{
282+
status->td_ppid = atoi(&line[12]);
283+
}
284+
}
260285
#endif
261286

262287
/****************************************************************************
@@ -355,6 +380,7 @@ static int ps_record(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
355380
#endif
356381
status->td_cmdline = "";
357382
status->td_pid = atoi(entryp->d_name);
383+
status->td_ppid = INVALID_PROCESS_ID;
358384
#ifdef NSH_HAVE_CPULOAD
359385
status->td_cpuload = "";
360386
#endif
@@ -395,6 +421,39 @@ static int ps_record(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
395421
while (nextline != NULL);
396422
}
397423

424+
ret = ps_readprocfs(vtbl, "group/status", dirpath, entryp, status);
425+
if (ret >= 0)
426+
{
427+
/* Parse the group status. */
428+
429+
nextline = status->td_buf + status->td_bufpos;
430+
do
431+
{
432+
/* Find the beginning of the next line and NUL-terminate the
433+
* current line.
434+
*/
435+
436+
line = nextline;
437+
for (nextline++;
438+
*nextline != '\n' && *nextline != '\0';
439+
nextline++);
440+
441+
if (*nextline == '\n')
442+
{
443+
*nextline++ = '\0';
444+
}
445+
else
446+
{
447+
nextline = NULL;
448+
}
449+
450+
/* Parse the current line */
451+
452+
nsh_parse_gstatusline(line, status);
453+
}
454+
while (nextline != NULL);
455+
}
456+
398457
#ifdef PS_SHOW_HEAPSIZE
399458
if (heap)
400459
{
@@ -553,7 +612,7 @@ static void ps_title(FAR struct nsh_vtbl_s *vtbl, bool heap)
553612
#endif
554613

555614
nsh_output(vtbl,
556-
"%5s %5s "
615+
"%5s %5s %5s "
557616
#ifdef CONFIG_SMP
558617
"%3s "
559618
#endif
@@ -574,7 +633,7 @@ static void ps_title(FAR struct nsh_vtbl_s *vtbl, bool heap)
574633
"%6s "
575634
#endif
576635
"%s\n"
577-
, "PID", "GROUP"
636+
, "PID", "PPID", "GROUP"
578637
#ifdef CONFIG_SMP
579638
, "CPU"
580639
#endif
@@ -620,7 +679,7 @@ static void ps_output(FAR struct nsh_vtbl_s *vtbl, bool heap,
620679
#endif
621680

622681
nsh_output(vtbl,
623-
"%5d %5s "
682+
"%5d %5d %5s "
624683
#ifdef CONFIG_SMP
625684
"%3s "
626685
#endif
@@ -641,7 +700,7 @@ static void ps_output(FAR struct nsh_vtbl_s *vtbl, bool heap,
641700
"%5s "
642701
#endif
643702
"%s\n"
644-
, status->td_pid, status->td_groupid
703+
, status->td_pid, status->td_ppid, status->td_groupid
645704
#ifdef CONFIG_SMP
646705
, status->td_cpu
647706
#endif

0 commit comments

Comments
 (0)