-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_proccmds.c
724 lines (596 loc) · 19 KB
/
nsh_proccmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/****************************************************************************
* apps/nshlib/nsh_proccmds.c
*
* Copyright (C) 2007-2009, 2011-2012, 2014-2015, 2019 Gregory Nutt. All
* rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include "nsh.h"
#include "nsh_console.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NSH_PROC_MOUNTPOINT
# define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
#endif
/* See include/nuttx/sched.h: */
#undef HAVE_GROUPID
#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
# define HAVE_GROUPID 1
#endif
#ifdef CONFIG_DISABLE_PTHREAD
# undef HAVE_GROUPID
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* The returned value should be zero for success or TRUE or non zero for
* failure or FALSE.
*/
typedef int (*exec_t)(void);
/* This structure represents the parsed task characteristics */
struct nsh_taskstatus_s
{
FAR const char *td_type; /* Thread type */
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
FAR const char *td_groupid; /* Group ID */
#else
FAR const char *td_ppid; /* Parent thread ID */
#endif
#endif
#ifdef CONFIG_SMP
FAR const char *td_cpu; /* CPU */
#endif
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 */
};
/* Status strings */
#ifndef CONFIG_NSH_DISABLE_PS
#if 0 /* Not used */
static const char g_name[] = "Name:";
#endif
static const char g_type[] = "Type:";
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
static const char g_groupid[] = "Group:";
#else
static const char g_ppid[] = "PPID:";
#endif
#endif /* CONFIG_SCHED_HAVE_PARENT */
#ifdef CONFIG_SMP
static const char g_cpu[] = "CPU:";
#endif
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:";
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
static const char g_stacksize[] = "StackSize:";
#ifdef CONFIG_STACK_COLORATION
static const char g_stackused[] = "StackUsed:";
#endif
#endif /* !CONFIG_NSH_DISABLE_PSSTACKUSAGE */
#endif /* !CONFIG_NSH_DISABLE_PS */
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nsh_parse_statusline
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_PS
static void nsh_parse_statusline(FAR char *line,
FAR struct nsh_taskstatus_s *status)
{
/* Parse the task status.
*
* Format:
*
* 111111111122222222223
* 123456789012345678901234567890
* Name: xxxx... Task/thread name
* (See CONFIG_TASK_NAME_SIZE)
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
* PPID: xxxxx Parent thread ID
* State: xxxxxxxx,xxxxxxxxx {Invalid, Waiting, Ready, Running,
* Inactive}, {Unlock, Semaphore, Signal,
* MQ empty, MQ full}
* Flags: xxx N,P,X
* Priority: nnn Decimal, 0-255
* Scheduler: xxxxxxxxxxxxxx {SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,
* SCHED_OTHER}
* Sigmask: nnnnnnnn Hexadecimal, 32-bit
*/
#if 0 /* Not used */
/* Task name */
if (strncmp(line, g_name, strlen(g_name)) == 0)
{
/* Not used */
}
else
#endif
/* Task/thread type */
if (strncmp(line, g_type, strlen(g_type)) == 0)
{
/* Save the thread type */
status->td_type = nsh_trimspaces(&line[12]);
}
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
else if (strncmp(line, g_groupid, strlen(g_groupid)) == 0)
{
/* Save the Group ID */
status->td_groupid = nsh_trimspaces(&line[12]);
}
#else
else if (strncmp(line, g_ppid, strlen(g_ppid)) == 0)
{
/* Save the parent thread id */
status->td_ppid = nsh_trimspaces(&line[12]);
}
#endif
#endif
#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
else if (strncmp(line, g_state, strlen(g_state)) == 0)
{
FAR char *ptr;
/* Save the thread state */
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)
{
FAR char *ptr = nsh_trimspaces(&line[12]);
status->td_priority = ptr;
/* If priority inheritance is enabled, use current pri, ignore base */
while (isdigit(*ptr))
{
++ptr;
}
*ptr = '\0';
}
else if (strncmp(line, g_scheduler, strlen(g_scheduler)) == 0)
{
/* Skip over the SCHED_ part of the policy. Result is max 8 bytes. */
status->td_policy = nsh_trimspaces(&line[12 + 6]);
}
else if (strncmp(line, g_sigmask, strlen(g_sigmask)) == 0)
{
status->td_sigmask = nsh_trimspaces(&line[12]);
}
}
#endif
/****************************************************************************
* Name: ps_callback
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_PS
static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
FAR struct dirent *entryp, FAR void *pvarg)
{
struct nsh_taskstatus_s status;
FAR char *filepath;
FAR char *line;
FAR char *nextline;
int ret;
int i;
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
uint32_t stack_size;
#ifdef CONFIG_STACK_COLORATION
uint32_t stack_used;
uint32_t stack_filled;
#endif
#endif
/* Task/thread entries in the /proc directory will all be (1) directories
* with (2) all numeric names.
*/
if (!DIRENT_ISDIRECTORY(entryp->d_type))
{
/* Not a directory... skip this entry */
return OK;
}
/* Check each character in the name */
for (i = 0; i < NAME_MAX && entryp->d_name[i] != '\0'; i++)
{
if (!isdigit(entryp->d_name[i]))
{
/* Name contains something other than a numeric character */
return OK;
}
}
/* Set all pointers to the empty string. */
status.td_type = "";
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
status.td_groupid = "";
#else
status.td_ppid = "";
#endif
#endif
#ifdef CONFIG_SMP
status.td_cpu = "";
#endif
status.td_state = "";
status.td_event = "";
status.td_flags = "";
status.td_priority = "";
status.td_policy = "";
status.td_sigmask = "";
/* Read the task status */
filepath = NULL;
ret = asprintf(&filepath, "%s/%s/status", dirpath, entryp->d_name);
if (ret < 0 || filepath == NULL)
{
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
}
else
{
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
free(filepath);
if (ret >= 0)
{
/* Parse the task status. */
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 */
nsh_parse_statusline(line, &status);
}
while (nextline != NULL);
}
}
/* Finally, print the status information */
nsh_output(vtbl, "%5s ", entryp->d_name);
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
nsh_output(vtbl, "%5s ", status.td_groupid);
#else
nsh_output(vtbl, "%5s ", status.td_ppid);
#endif
#endif
#ifdef CONFIG_SMP
nsh_output(vtbl, "%3s ", status.td_cpu);
#endif
nsh_output(vtbl, "%3s %-8s %-7s %3s %-8s %-9s ",
status.td_priority, status.td_policy, status.td_type,
status.td_flags, status.td_state, status.td_event);
nsh_output(vtbl, "%-8s ", status.td_sigmask);
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
/* Get the StackSize and StackUsed */
stack_size = 0;
#ifdef CONFIG_STACK_COLORATION
stack_used = 0;
#endif
filepath = NULL;
ret = asprintf(&filepath, "%s/%s/stack", 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
* StackBase: xxxxxxxxxx
* StackSize: xxxx
* StackUsed: xxxx
*/
if (strncmp(line, g_stacksize, strlen(g_stacksize)) == 0)
{
stack_size = (uint32_t)atoi(&line[12]);
}
#ifdef CONFIG_STACK_COLORATION
else if (strncmp(line, g_stackused, strlen(g_stackused)) == 0)
{
stack_used = (uint32_t)atoi(&line[12]);
}
#endif
}
while (nextline != NULL);
}
}
nsh_output(vtbl, "%06u ", (unsigned int)stack_size);
#ifdef CONFIG_STACK_COLORATION
nsh_output(vtbl, "%06u ", (unsigned int)stack_used);
stack_filled = 0;
if (stack_size > 0 && stack_used > 0)
{
/* Use fixed-point math with one decimal place */
stack_filled = 10 * 100 * stack_used / stack_size;
}
/* Additionally print a '!' if the stack is filled more than 80% */
nsh_output(vtbl, "%3d.%1d%%%c ",
stack_filled / 10, stack_filled % 10,
(stack_filled >= 10 * 80 ? '!' : ' '));
#endif
#endif
#ifdef NSH_HAVE_CPULOAD
/* Get the CPU load */
filepath = NULL;
ret = asprintf(&filepath, "%s/%s/loadavg", 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)
{
vtbl->iobuffer[0] = '\0';
}
}
nsh_output(vtbl, "%6s ", nsh_trimspaces(vtbl->iobuffer));
#endif
/* Read the task/thread command line */
filepath = NULL;
ret = asprintf(&filepath, "%s/%s/cmdline", dirpath, entryp->d_name);
if (ret < 0 || filepath == NULL)
{
nsh_error(vtbl, g_fmtcmdfailed, "ps", "asprintf", NSH_ERRNO);
return ERROR;
}
ret = nsh_readfile(vtbl, "ps", filepath, vtbl->iobuffer, IOBUFFERSIZE);
free(filepath);
if (ret < 0)
{
return ERROR;
}
nsh_output(vtbl, "%s\n", nsh_trimspaces(vtbl->iobuffer));
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_exec
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_EXEC
int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
FAR char *endptr;
uintptr_t addr;
addr = (uintptr_t)strtol(argv[1], &endptr, 0);
if (!addr || endptr == argv[1] || *endptr != '\0')
{
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
nsh_output(vtbl, "Calling %p\n", (exec_t)addr);
return ((exec_t)addr)();
}
#endif
/****************************************************************************
* Name: cmd_ps
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_PS
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
nsh_output(vtbl, "%5s ", "PID");
#ifdef CONFIG_SCHED_HAVE_PARENT
#ifdef HAVE_GROUPID
nsh_output(vtbl, "%5s ", "GROUP");
#else
nsh_output(vtbl, "%5s ", "PPID");
#endif
#endif
#ifdef CONFIG_SMP
nsh_output(vtbl, "%3s ", "CPU");
#endif
nsh_output(vtbl, "%3s %-8s %-7s %3s %-8s %-9s ",
"PRI", "POLICY", "TYPE", "NPX", "STATE", "EVENT");
nsh_output(vtbl, "%-8s ", "SIGMASK");
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
nsh_output(vtbl, "%6s ", "STACK");
#ifdef CONFIG_STACK_COLORATION
nsh_output(vtbl, "%6s ", "USED");
nsh_output(vtbl, "%7s ", "FILLED");
#endif
#endif
#ifdef NSH_HAVE_CPULOAD
nsh_output(vtbl, "%6s ", "CPU");
#endif
nsh_output(vtbl, "%s\n", "COMMAND");
return nsh_foreach_direntry(vtbl, "ps", CONFIG_NSH_PROC_MOUNTPOINT,
ps_callback, NULL);
}
#endif
/****************************************************************************
* Name: cmd_kill
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_KILL
int cmd_kill(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
char *ptr;
char *endptr;
long signal;
long pid;
/* 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;
}
/* Extract the pid */
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:
nsh_error(vtbl, g_fmtnosuch, argv[0], "task", argv[2]);
return ERROR;
case EPERM:
case ENOSYS:
default:
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "kill", NSH_ERRNO);
return ERROR;
}
invalid_arg:
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
#endif
/****************************************************************************
* Name: cmd_sleep
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_SLEEP
int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
char *endptr;
long secs;
secs = strtol(argv[1], &endptr, 0);
if (!secs || endptr == argv[1] || *endptr != '\0')
{
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
sleep(secs);
return OK;
}
#endif
/****************************************************************************
* Name: cmd_usleep
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_USLEEP
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
char *endptr;
long usecs;
usecs = strtol(argv[1], &endptr, 0);
if (!usecs || endptr == argv[1] || *endptr != '\0')
{
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
usleep(usecs);
return OK;
}
#endif