1 Star 0 Fork 0

张恩寿/nuttx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fs_procfsproc.c 57.65 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
/****************************************************************************
* fs/procfs/fs_procfsproc.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <malloc.h>
#ifdef CONFIG_SCHED_CRITMONITOR
# include <time.h>
#endif
#include <nuttx/irq.h>
#include <nuttx/tls.h>
#include <nuttx/sched.h>
#include <nuttx/kmalloc.h>
#include <nuttx/environ.h>
#include <nuttx/signal.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/mm/mm.h>
#if !defined(CONFIG_SCHED_CPULOAD_NONE) || defined(CONFIG_SCHED_CRITMONITOR)
# include <nuttx/clock.h>
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Determines the size of an intermediate buffer that must be large enough
* to handle the longest line generated by this logic.
*/
#define STATUS_LINELEN PATH_MAX
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* This enumeration identifies all of the task/thread nodes that can be
* accessed via the procfs file system.
*/
enum proc_node_e
{
PROC_LEVEL0 = 0, /* The top-level directory */
PROC_STATUS, /* Task/thread status */
PROC_CMDLINE, /* Task command line */
#ifndef CONFIG_SCHED_CPULOAD_NONE
PROC_LOADAVG, /* Average CPU utilization */
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
PROC_CRITMON, /* Critical section monitor */
#endif
#if CONFIG_MM_BACKTRACE >= 0
PROC_HEAP, /* Task heap info */
#endif
#ifdef CONFIG_DEBUG_MM
PROC_HEAP_CHECK, /* Task heap check flag */
#endif
PROC_STACK, /* Task stack info */
PROC_GROUP, /* Group directory */
PROC_GROUP_STATUS, /* Task group status */
PROC_GROUP_FD /* Group file descriptors */
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
, PROC_GROUP_ENV /* Group environment variables */
#endif
};
/* This structure associates a relative path name with an node in the task
* procfs
*/
struct proc_node_s
{
FAR const char *relpath; /* Relative path to the node */
FAR const char *name; /* Terminal node segment name */
uint8_t node; /* Type of node (see enum proc_node_e) */
uint8_t dtype; /* dirent type (see include/dirent.h) */
};
/* This structure describes one open "file" */
struct proc_file_s
{
struct procfs_file_s base; /* Base open file structure */
FAR const struct proc_node_s *node; /* Describes the file node */
pid_t pid; /* Task/thread ID */
char line[STATUS_LINELEN]; /* Pre-allocated buffer for formatted lines */
};
/* This structure describes one open "directory" */
struct proc_dir_s
{
struct procfs_dir_priv_s base; /* Base directory private data */
FAR const struct proc_node_s *node; /* Directory node description */
pid_t pid; /* ID of task/thread for attributes */
};
/* This structure used with the env_foreach() callback */
struct proc_envinfo_s
{
FAR struct proc_file_s *procfile;
FAR char *buffer;
FAR off_t offset;
size_t buflen;
size_t remaining;
size_t totalsize;
};
/****************************************************************************
* Private Data
****************************************************************************/
static FAR const char *g_policy[4] =
{
"SCHED_FIFO", "SCHED_RR", "SCHED_SPORADIC"
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Helpers */
static FAR const struct proc_node_s *
proc_findnode(FAR const char *relpath);
static ssize_t proc_status(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
#ifndef CONFIG_SCHED_CPULOAD_NONE
static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
static ssize_t proc_critmon(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
#endif
#if CONFIG_MM_BACKTRACE >= 0
static ssize_t proc_heap(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset);
#endif
#ifdef CONFIG_DEBUG_MM
static ssize_t proc_heapcheck(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset);
static ssize_t proc_heapcheck_write(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR const char *buffer,
size_t buflen, off_t offset);
#endif
static ssize_t proc_stack(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
static int proc_groupenv_callback(FAR void *arg, FAR const char *pair);
static ssize_t proc_groupenv(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset);
#endif
/* File system methods */
static int proc_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int proc_close(FAR struct file *filep);
static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t proc_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int proc_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int proc_opendir(const char *relpath,
FAR struct fs_dirent_s **dir);
static int proc_closedir(FAR struct fs_dirent_s *dir);
static int proc_readdir(FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
static int proc_rewinddir(FAR struct fs_dirent_s *dir);
static int proc_stat(FAR const char *relpath, FAR struct stat *buf);
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* See fs_mount.c -- this structure is explicitly externed there.
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct procfs_operations g_proc_operations =
{
proc_open, /* open */
proc_close, /* close */
proc_read, /* read */
proc_write, /* write */
proc_dup, /* dup */
proc_opendir, /* opendir */
proc_closedir, /* closedir */
proc_readdir, /* readdir */
proc_rewinddir, /* rewinddir */
proc_stat /* stat */
};
/* These structures provide information about every node */
static const struct proc_node_s g_level0node =
{
"", "", (uint8_t)PROC_LEVEL0, DTYPE_DIRECTORY /* Top-level directory */
};
static const struct proc_node_s g_status =
{
"status", "status", (uint8_t)PROC_STATUS, DTYPE_FILE /* Task/thread status */
};
static const struct proc_node_s g_cmdline =
{
"cmdline", "cmdline", (uint8_t)PROC_CMDLINE, DTYPE_FILE /* Task command line */
};
#ifndef CONFIG_SCHED_CPULOAD_NONE
static const struct proc_node_s g_loadavg =
{
"loadavg", "loadavg", (uint8_t)PROC_LOADAVG, DTYPE_FILE /* Average CPU utilization */
};
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
static const struct proc_node_s g_critmon =
{
"critmon", "critmon", (uint8_t)PROC_CRITMON, DTYPE_FILE /* Critical Section Monitor */
};
#endif
#if CONFIG_MM_BACKTRACE >= 0
static const struct proc_node_s g_heap =
{
"heap", "heap", (uint8_t)PROC_HEAP, DTYPE_FILE /* Task heap info */
};
#endif
#ifdef CONFIG_DEBUG_MM
static const struct proc_node_s g_heapcheck =
{
"heapcheck", "heapcheck", (uint8_t)PROC_HEAP_CHECK, DTYPE_FILE /* Task heap info */
};
#endif
static const struct proc_node_s g_stack =
{
"stack", "stack", (uint8_t)PROC_STACK, DTYPE_FILE /* Task stack info */
};
static const struct proc_node_s g_group =
{
"group", "group", (uint8_t)PROC_GROUP, DTYPE_DIRECTORY /* Group directory */
};
static const struct proc_node_s g_groupstatus =
{
"group/status", "status", (uint8_t)PROC_GROUP_STATUS, DTYPE_FILE /* Task group status */
};
static const struct proc_node_s g_groupfd =
{
"group/fd", "fd", (uint8_t)PROC_GROUP_FD, DTYPE_FILE /* Group file descriptors */
};
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
static const struct proc_node_s g_groupenv =
{
"group/env", "env", (uint8_t)PROC_GROUP_ENV, DTYPE_FILE /* Group environment variables */
};
#endif
/* This is the list of all nodes */
static FAR const struct proc_node_s * const g_nodeinfo[] =
{
&g_status, /* Task/thread status */
&g_cmdline, /* Task command line */
#ifndef CONFIG_SCHED_CPULOAD_NONE
&g_loadavg, /* Average CPU utilization */
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
&g_critmon, /* Critical section Monitor */
#endif
#if CONFIG_MM_BACKTRACE >= 0
&g_heap, /* Task heap info */
#endif
#ifdef CONFIG_DEBUG_MM
&g_heapcheck, /* Task heap check flag */
#endif
&g_stack, /* Task stack info */
&g_group, /* Group directory */
&g_groupstatus, /* Task group status */
&g_groupfd /* Group file descriptors */
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
, &g_groupenv /* Group environment variables */
#endif
};
#define PROC_NNODES (sizeof(g_nodeinfo)/sizeof(FAR const struct proc_node_s * const))
/* This is the list of all level0 nodes */
static const struct proc_node_s * const g_level0info[] =
{
&g_status, /* Task/thread status */
&g_cmdline, /* Task command line */
#ifndef CONFIG_SCHED_CPULOAD_NONE
&g_loadavg, /* Average CPU utilization */
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
&g_critmon, /* Critical section monitor */
#endif
#if CONFIG_MM_BACKTRACE >= 0
&g_heap, /* Task heap info */
#endif
#ifdef CONFIG_DEBUG_MM
&g_heapcheck, /* Task heap check flag */
#endif
&g_stack, /* Task stack info */
&g_group, /* Group directory */
};
#define PROC_NLEVEL0NODES (sizeof(g_level0info)/sizeof(FAR const struct proc_node_s * const))
/* This is the list of all group sub-directory nodes */
static FAR const struct proc_node_s * const g_groupinfo[] =
{
&g_groupstatus, /* Task group status */
&g_groupfd /* Group file descriptors */
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
, &g_groupenv /* Group environment variables */
#endif
};
#define PROC_NGROUPNODES (sizeof(g_groupinfo)/sizeof(FAR const struct proc_node_s * const))
static FAR const char * const g_ttypenames[4] =
{
"Task",
"pthread",
"Kthread",
"Invalid"
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: proc_findnode
****************************************************************************/
static FAR const struct proc_node_s *proc_findnode(FAR const char *relpath)
{
int i;
/* Search every string in g_nodeinfo or until a match is found */
for (i = 0; i < PROC_NNODES; i++)
{
size_t len = strlen(g_nodeinfo[i]->relpath);
if (strncmp(g_nodeinfo[i]->relpath, relpath, len) == 0 &&
(relpath[len] == '\0' || (relpath[len] == '/' &&
relpath[len + 1] == '\0' &&
g_nodeinfo[i]->dtype == DTYPE_DIRECTORY)))
{
return g_nodeinfo[i];
}
}
/* Not found */
return NULL;
}
/****************************************************************************
* Name: proc_status
*
* Description:
* Format:
*
* 111111111122222222223
* 123456789012345678901234567890
* Name: xxxx... Task/thread name (See
* CONFIG_TASK_NAME_SIZE)
* Type: xxxxxxx {Task, pthread, Kthread, Invalid}
* PPID: xxxxx Parent thread ID
* Group: xxxxx Group ID
* CPU: xxx CPU (CONFIG_SMP only)
* 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}
* Sigmask: nnnnnnnn Hexadecimal, 32-bit
*
****************************************************************************/
static ssize_t proc_status(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
FAR const char *policy;
FAR const char *name;
char state[32];
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
remaining = buflen;
totalsize = 0;
/* Show the task name */
#if CONFIG_TASK_NAME_SIZE > 0
name = tcb->name;
#else
name = "<noname>";
#endif
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%.18s\n", "Name:", name);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the thread type */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n",
"Type:",
g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK)
>> TCB_FLAG_TTYPE_SHIFT]);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%d\n", "Group:",
tcb->group ? tcb->group->tg_pid : -1);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
#ifdef CONFIG_SMP
if (tcb->task_state >= FIRST_ASSIGNED_STATE &&
tcb->task_state <= LAST_ASSIGNED_STATE)
{
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%d\n", "CPU:", tcb->cpu);
}
else
{
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s---\n", "CPU:");
}
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
#endif
/* Show the thread state */
nxsched_get_stateinfo(tcb, state, sizeof(state));
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%s\n", "State:", state);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show task flags */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%c\n", "Flags:",
tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-');
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the thread priority */
#ifdef CONFIG_PRIORITY_INHERITANCE
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%d (%d)\n", "Priority:",
tcb->sched_priority, tcb->base_priority);
#else
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
"Priority:", tcb->sched_priority);
#endif
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the scheduler policy */
policy = g_policy[(tcb->flags & TCB_FLAG_POLICY_MASK) >>
TCB_FLAG_POLICY_SHIFT];
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n",
"Scheduler:", policy);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the signal mask. Note: sigset_t is uint32_t on NuttX. */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s" SIGSET_FMT "\n",
"SigMask:", SIGSET_ELEM(&tcb->sigprocmask));
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
return totalsize;
}
/****************************************************************************
* Name: proc_cmdline
****************************************************************************/
static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
FAR const char *name;
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
remaining = buflen;
totalsize = 0;
/* Show the task name */
#if CONFIG_TASK_NAME_SIZE > 0
name = tcb->name;
#else
name = "<noname>";
#endif
linesize = strlen(name);
memcpy(procfile->line, name, linesize);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the task / thread argument list (skipping over the name) */
linesize = group_argvstr(tcb, procfile->line, remaining);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "\n");
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
return totalsize;
}
/****************************************************************************
* Name: proc_loadavg
****************************************************************************/
#ifndef CONFIG_SCHED_CPULOAD_NONE
static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
struct cpuload_s cpuload;
uint32_t intpart;
uint32_t fracpart;
size_t linesize;
size_t copysize;
/* Sample the counts for the thread. clock_cpuload should only fail if
* the PID is not valid. This could happen if the thread exited sometime
* after the procfs entry was opened.
*/
clock_cpuload(procfile->pid, &cpuload);
/* On the simulator, you may hit cpuload.total == 0, but probably never on
* real hardware.
*/
if (cpuload.total > 0)
{
uint32_t tmp;
tmp = (1000 * cpuload.active) / cpuload.total;
intpart = tmp / 10;
fracpart = tmp - 10 * intpart;
}
else
{
intpart = 0;
fracpart = 0;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%3" PRId32 ".%01" PRId32 "%%\n",
intpart, fracpart);
copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
&offset);
return copysize;
}
#endif
/****************************************************************************
* Name: proc_critmon
****************************************************************************/
#ifdef CONFIG_SCHED_CRITMONITOR
static ssize_t proc_critmon(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
struct timespec maxtime;
struct timespec runtime;
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
remaining = buflen;
totalsize = 0;
/* Convert the for maximum time pre-emption disabled */
if (tcb->premp_max > 0)
{
perf_convert(tcb->premp_max, &maxtime);
}
else
{
maxtime.tv_sec = 0;
maxtime.tv_nsec = 0;
}
/* Reset the maximum */
tcb->premp_max = 0;
/* Generate output for maximum time pre-emption disabled */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%lu.%09lu,",
(unsigned long)maxtime.tv_sec,
(unsigned long)maxtime.tv_nsec);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Convert and generate output for maximum time in a critical section */
if (tcb->crit_max > 0)
{
perf_convert(tcb->crit_max, &maxtime);
}
else
{
maxtime.tv_sec = 0;
maxtime.tv_nsec = 0;
}
/* Reset the maximum */
tcb->crit_max = 0;
/* Generate output for maximum time in a critical section */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%lu.%09lu,",
(unsigned long)maxtime.tv_sec,
(unsigned long)maxtime.tv_nsec);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Convert and generate output for maximum time thread running */
if (tcb->run_max > 0)
{
perf_convert(tcb->run_max, &maxtime);
}
else
{
maxtime.tv_sec = 0;
maxtime.tv_nsec = 0;
}
/* Reset the maximum */
tcb->run_max = 0;
perf_convert(tcb->run_time, &runtime);
/* Output the maximum time the thread has run and
* the total time the thread has run
*/
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%lu.%09lu,%lu.%09lu\n",
(unsigned long)maxtime.tv_sec,
(unsigned long)maxtime.tv_nsec,
(unsigned long)runtime.tv_sec,
(unsigned long)(runtime.tv_nsec));
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
return totalsize;
}
#endif
/****************************************************************************
* Name: proc_heap
****************************************************************************/
#if CONFIG_MM_BACKTRACE >= 0
static ssize_t proc_heap(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
size_t remaining = buflen;
size_t linesize;
size_t copysize;
size_t totalsize = 0;
struct mallinfo_task info;
struct malltask task;
task.pid = tcb->pid;
task.seqmin = 0;
task.seqmax = ULONG_MAX;
#ifdef CONFIG_MM_KERNEL_HEAP
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
{
info = kmm_mallinfo_task(&task);
}
else
#endif
{
info = mallinfo_task(&task);
}
/* Show the heap alloc size */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%d\n", "AllocSize:", info.uordblks);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the heap alloc block */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%d\n", "AllocBlks:", info.aordblks);
totalsize += procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
return totalsize;
}
#endif
#ifdef CONFIG_DEBUG_MM
static ssize_t proc_heapcheck(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
size_t remaining = buflen;
size_t linesize;
size_t copysize;
size_t totalsize = 0;
size_t heapcheck = 0;
if (tcb->flags & TCB_FLAG_HEAP_CHECK)
{
heapcheck = 1;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%zu\n",
"HeapCheck:", heapcheck);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
return totalsize;
}
static ssize_t proc_heapcheck_write(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb,
FAR const char *buffer,
size_t buflen, off_t offset)
{
switch (atoi(buffer))
{
case 0:
tcb->flags &= ~TCB_FLAG_HEAP_CHECK;
break;
case 1:
tcb->flags |= TCB_FLAG_HEAP_CHECK;
break;
default:
ferr("ERROR: invalid argument\n");
return -EINVAL;
break;
}
return buflen;
}
#endif
/****************************************************************************
* Name: proc_stack
****************************************************************************/
static ssize_t proc_stack(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
#if CONFIG_SCHED_STACK_RECORD > 0
int i;
#endif
remaining = buflen;
totalsize = 0;
/* Show the stack alloc address */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%p\n",
"StackAlloc:", tcb->stack_alloc_ptr);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the stack base address */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%p\n",
"StackBase:", tcb->stack_base_ptr);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the stack size */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
"StackSize:", (long)tcb->adj_stack_size);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
#if CONFIG_SCHED_STACK_RECORD > 0
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%zu\n",
"StackMax: ",
tcb->stack_base_ptr +
tcb->adj_stack_size - tcb->sp_deepest);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%-s\n",
"Size", "Backtrace");
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
for (i = tcb->level_deepest - 1; i > 0; i--)
{
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12zu%-pS\n",
tcb->stackrecord_sp_deepest[i - 1] -
tcb->stackrecord_sp_deepest[i],
tcb->stackrecord_pc_deepest[i]);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
}
if (tcb->caller_deepest)
{
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"Warning stack record buffer too small\n"
"Max: %u Overflow: %zu\n",
CONFIG_SCHED_STACK_RECORD,
tcb->caller_deepest);
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
}
#endif
#ifdef CONFIG_STACK_COLORATION
if (totalsize >= buflen)
{
return totalsize;
}
/* Show the stack size */
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
"StackUsed:", (long)up_check_tcbstack(tcb));
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
#endif
return totalsize;
}
/****************************************************************************
* Name: proc_groupstatus
****************************************************************************/
static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
FAR struct task_group_s *group = tcb->group;
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
#ifdef HAVE_GROUP_MEMBERS
int i;
#endif
DEBUGASSERT(group != NULL);
remaining = buflen;
totalsize = 0;
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
"Main task:", group->tg_pid);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
"Parent:", group->tg_ppid);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s0x%02x\n",
"Flags:", group->tg_flags);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
"Members:", group->tg_nmembers);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
#ifdef HAVE_GROUP_MEMBERS
if (totalsize >= buflen)
{
return totalsize;
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"Member IDs:");
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
for (i = 0; i < group->tg_nmembers; i++)
{
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, " %d",
group->tg_members[i]);
copysize = procfs_memcpy(procfile->line, linesize, buffer,
remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "\n");
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
#endif
return totalsize;
}
/****************************************************************************
* Name: proc_groupfd
****************************************************************************/
static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer,
size_t buflen, off_t offset)
{
FAR struct task_group_s *group = tcb->group;
FAR struct file *filep;
char path[PATH_MAX];
size_t remaining;
size_t linesize;
size_t copysize;
size_t totalsize;
int count;
int i;
DEBUGASSERT(group != NULL);
count = files_countlist(&group->tg_filelist);
if (count == 0)
{
return 0;
}
remaining = buflen;
totalsize = 0;
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"\n%-3s %-7s %-4s %-9s %s\n",
"FD", "OFLAGS", "TYPE", "POS", "PATH");
copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
&offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
/* Examine each open file descriptor */
for (i = 0; i < count; i++)
{
filep = files_fget(&group->tg_filelist, i);
/* Is there an inode associated with the file descriptor? */
if (filep->f_inode == NULL)
{
continue;
}
if (file_ioctl(filep, FIOC_FILEPATH, path) < 0)
{
path[0] = '\0';
}
linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-3d %-7d %-4x %-9ld %s\n",
i, filep->f_oflags,
INODE_GET_TYPE(filep->f_inode),
(long)filep->f_pos, path);
copysize = procfs_memcpy(procfile->line, linesize,
buffer, remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
}
return totalsize;
}
/****************************************************************************
* Name: proc_groupenv_callback
****************************************************************************/
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
static int proc_groupenv_callback(FAR void *arg, FAR const char *pair)
{
FAR struct proc_envinfo_s *info = (FAR struct proc_envinfo_s *)arg;
FAR const char *src;
FAR const char *value;
FAR char *dest;
char name[16 + 1];
size_t linesize;
size_t copysize;
int namelen;
DEBUGASSERT(arg != NULL && pair != NULL);
/* Parse the name from the name/value pair */
value = NULL;
namelen = 0;
for (src = pair, dest = name; *src != '=' && *src != '\0'; src++)
{
if (namelen < 16)
{
*dest++ = *src;
namelen++;
}
}
/* NUL terminate the name string */
*dest = '\0';
/* Skip over the '=' to get the value */
if (*src == '=')
{
value = src + 1;
}
else
{
value = "";
}
/* Output the header */
linesize = procfs_snprintf(info->procfile->line,
STATUS_LINELEN, "%s=%s\n",
name, value);
copysize = procfs_memcpy(info->procfile->line, linesize,
info->buffer, info->remaining,
&info->offset);
info->totalsize += copysize;
info->buffer += copysize;
info->remaining -= copysize;
if (info->totalsize >= info->buflen)
{
return 1;
}
return 0;
}
#endif
/****************************************************************************
* Name: proc_groupenv
****************************************************************************/
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
static ssize_t proc_groupenv(FAR struct proc_file_s *procfile,
FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
off_t offset)
{
FAR struct task_group_s *group = tcb->group;
struct proc_envinfo_s info;
DEBUGASSERT(group != NULL);
/* Initialize the info structure */
info.procfile = procfile;
info.buffer = buffer;
info.offset = offset;
info.buflen = buflen;
info.remaining = buflen;
info.totalsize = 0;
/* Generate output for each environment variable */
env_foreach(group, proc_groupenv_callback, &info);
return info.totalsize;
}
#endif
/****************************************************************************
* Name: proc_open
****************************************************************************/
static int proc_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct proc_file_s *procfile;
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
FAR char *ptr;
unsigned long tmp;
pid_t pid;
finfo("Open '%s'\n", relpath);
/* The first segment of the relative path should be a task/thread ID or
* the string "self".
*/
ptr = NULL;
if (strncmp(relpath, "self", 4) == 0)
{
tmp = nxsched_gettid(); /* Get the TID of the calling task */
ptr = (FAR char *)relpath + 4; /* Discard const */
}
else
{
tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
}
if (ptr == NULL || *ptr != '/')
{
ferr("ERROR: Invalid path \"%s\"\n", relpath);
return -ENOENT;
}
/* Skip over the slash */
ptr++;
/* A valid PID would be in the range of 0-INT_MAX (0 is reserved for the
* IDLE thread).
*/
if (tmp > INT_MAX)
{
ferr("ERROR: Invalid PID %ld\n", tmp);
return -ENOENT;
}
/* Now verify that a task with this task/thread ID exists */
pid = (pid_t)tmp;
tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
}
/* The remaining segments of the relpath should be a well known node in
* the task/thread tree.
*/
node = proc_findnode(ptr);
if (node == NULL)
{
ferr("ERROR: Invalid path \"%s\"\n", relpath);
return -ENOENT;
}
/* The node must be a file, not a directory */
if (!DIRENT_ISFILE(node->dtype))
{
ferr("ERROR: Path \"%s\" is not a regular file\n", relpath);
return -EISDIR;
}
/* Allocate a container to hold the task and node selection */
procfile = (FAR struct proc_file_s *)
kmm_zalloc(sizeof(struct proc_file_s));
if (procfile == NULL)
{
ferr("ERROR: Failed to allocate file container\n");
return -ENOMEM;
}
/* Initialize the file container */
procfile->pid = pid;
procfile->node = node;
/* Save the index as the open-specific state in filep->f_priv */
filep->f_priv = (FAR void *)procfile;
return OK;
}
/****************************************************************************
* Name: proc_close
****************************************************************************/
static int proc_close(FAR struct file *filep)
{
FAR struct proc_file_s *procfile;
/* Recover our private data from the struct file instance */
procfile = (FAR struct proc_file_s *)filep->f_priv;
DEBUGASSERT(procfile != NULL);
/* Release the file container structure */
kmm_free(procfile);
filep->f_priv = NULL;
return OK;
}
/****************************************************************************
* Name: proc_read
****************************************************************************/
static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct proc_file_s *procfile;
FAR struct tcb_s *tcb;
ssize_t ret;
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
/* Recover our private data from the struct file instance */
procfile = (FAR struct proc_file_s *)filep->f_priv;
DEBUGASSERT(procfile != NULL);
/* Verify that the thread is still valid */
tcb = nxsched_get_tcb(procfile->pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is not valid\n", procfile->pid);
return -ENODEV;
}
/* Provide the requested data */
switch (procfile->node->node)
{
case PROC_STATUS: /* Task/thread status */
ret = proc_status(procfile, tcb, buffer, buflen, filep->f_pos);
break;
case PROC_CMDLINE: /* Task command line */
ret = proc_cmdline(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#ifndef CONFIG_SCHED_CPULOAD_NONE
case PROC_LOADAVG: /* Average CPU utilization */
ret = proc_loadavg(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#endif
#ifdef CONFIG_SCHED_CRITMONITOR
case PROC_CRITMON: /* Critical section monitor */
ret = proc_critmon(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#endif
#if CONFIG_MM_BACKTRACE >= 0
case PROC_HEAP: /* Task heap info */
ret = proc_heap(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#endif
#ifdef CONFIG_DEBUG_MM
case PROC_HEAP_CHECK: /* Task heap check flag */
ret = proc_heapcheck(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#endif
case PROC_STACK: /* Task stack info */
ret = proc_stack(procfile, tcb, buffer, buflen, filep->f_pos);
break;
case PROC_GROUP_STATUS: /* Task group status */
ret = proc_groupstatus(procfile, tcb, buffer, buflen, filep->f_pos);
break;
case PROC_GROUP_FD: /* Group file descriptors */
ret = proc_groupfd(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
case PROC_GROUP_ENV: /* Group environment variables */
ret = proc_groupenv(procfile, tcb, buffer, buflen, filep->f_pos);
break;
#endif
default:
ret = -EINVAL;
break;
}
/* Update the file offset */
if (ret > 0)
{
filep->f_pos += ret;
}
return ret;
}
/****************************************************************************
* Name: proc_write
****************************************************************************/
static ssize_t proc_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
FAR struct proc_file_s *procfile;
FAR struct tcb_s *tcb;
ssize_t ret;
DEBUGASSERT(buffer != NULL && buflen > 0);
procfile = (FAR struct proc_file_s *)filep->f_priv;
DEBUGASSERT(procfile != NULL);
/* Verify that the thread is still valid */
tcb = nxsched_get_tcb(procfile->pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is not valid\n", procfile->pid);
return -ENODEV;
}
/* Provide the requested data */
switch (procfile->node->node)
{
#ifdef CONFIG_DEBUG_MM
case PROC_HEAP_CHECK:
ret = proc_heapcheck_write(procfile, tcb, buffer, buflen,
filep->f_pos);
break;
#endif
default:
ret = -EINVAL;
break;
}
return ret;
}
/****************************************************************************
* Name: proc_dup
*
* Description:
* Duplicate open file data in the new file structure.
*
****************************************************************************/
static int proc_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct proc_file_s *oldfile;
FAR struct proc_file_s *newfile;
finfo("Dup %p->%p\n", oldp, newp);
/* Recover our private data from the old struct file instance */
oldfile = (FAR struct proc_file_s *)oldp->f_priv;
DEBUGASSERT(oldfile != NULL);
/* Allocate a new container to hold the task and node selection */
newfile = kmm_malloc(sizeof(struct proc_file_s));
if (newfile == NULL)
{
ferr("ERROR: Failed to allocate file container\n");
return -ENOMEM;
}
/* The copy the file information from the old container to the new */
memcpy(newfile, oldfile, sizeof(struct proc_file_s));
/* Save the new container in the new file structure */
newp->f_priv = (FAR void *)newfile;
return OK;
}
/****************************************************************************
* Name: proc_opendir
*
* Description:
* Open a directory for read access
*
****************************************************************************/
static int proc_opendir(FAR const char *relpath,
FAR struct fs_dirent_s **dir)
{
FAR struct proc_dir_s *procdir;
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
unsigned long tmp;
FAR char *ptr;
pid_t pid;
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
DEBUGASSERT(relpath != NULL);
/* The relative must be either:
*
* (1) "<pid>" - The sub-directory of task/thread attributes,
* (2) "self" - Which refers to the PID of the calling task, or
* (3) The name of a directory node under either of those
*/
/* Otherwise, the relative path should be a valid task/thread ID */
ptr = NULL;
if (strncmp(relpath, "self", 4) == 0)
{
tmp = nxsched_gettid(); /* Get the TID of the calling task */
ptr = (FAR char *)relpath + 4; /* Discard const */
}
else
{
tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
}
if (ptr == NULL || (*ptr != '\0' && *ptr != '/'))
{
/* strtoul failed or there is something in the path after the pid */
ferr("ERROR: Invalid path \"%s\"\n", relpath);
return -ENOENT;
}
/* A valid PID would be in the range of 0-INT_MAX (0 is reserved for the
* IDLE thread).
*/
if (tmp > INT_MAX)
{
ferr("ERROR: Invalid PID %ld\n", tmp);
return -ENOENT;
}
/* Now verify that a task with this task/thread ID exists */
pid = (pid_t)tmp;
tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is not valid\n", pid);
return -ENOENT;
}
/* Allocate the directory structure. Note that the index and procentry
* pointer are implicitly nullified by kmm_zalloc(). Only the remaining,
* non-zero entries will need be initialized.
*/
procdir = kmm_zalloc(sizeof(struct proc_dir_s));
if (procdir == NULL)
{
ferr("ERROR: Failed to allocate the directory structure\n");
return -ENOMEM;
}
/* Was the <pid> the final element of the path? */
if (*ptr != '\0' && strcmp(ptr, "/") != 0)
{
/* There is something in the path after the pid. Skip over the path
* segment delimiter and see if we can identify the node of interest.
*/
ptr++;
node = proc_findnode(ptr);
if (node == NULL)
{
ferr("ERROR: Invalid path \"%s\"\n", relpath);
kmm_free(procdir);
return -ENOENT;
}
/* The node must be a directory, not a file */
if (!DIRENT_ISDIRECTORY(node->dtype))
{
ferr("ERROR: Path \"%s\" is not a directory\n", relpath);
kmm_free(procdir);
return -ENOTDIR;
}
/* This is a second level directory */
procdir->base.level = 2;
procdir->base.nentries = PROC_NGROUPNODES;
procdir->node = node;
}
else
{
/* Use the special level0 node */
procdir->base.level = 1;
procdir->base.nentries = PROC_NLEVEL0NODES;
procdir->node = &g_level0node;
}
procdir->pid = pid;
*dir = (FAR struct fs_dirent_s *)procdir;
return OK;
}
/****************************************************************************
* Name: proc_closedir
*
* Description: Close the directory listing
*
****************************************************************************/
static int proc_closedir(FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(dir != NULL);
kmm_free(dir);
return OK;
}
/****************************************************************************
* Name: proc_readdir
*
* Description: Read the next directory entry
*
****************************************************************************/
static int proc_readdir(FAR struct fs_dirent_s *dir,
FAR struct dirent *entry)
{
FAR struct proc_dir_s *procdir;
FAR const struct proc_node_s *node = NULL;
FAR struct tcb_s *tcb;
unsigned int index;
pid_t pid;
int ret;
DEBUGASSERT(dir != NULL);
procdir = (FAR struct proc_dir_s *)dir;
/* Have we reached the end of the directory */
index = procdir->base.index;
if (index >= procdir->base.nentries)
{
/* We signal the end of the directory by returning the special
* error -ENOENT
*/
finfo("Entry %d: End of directory\n", index);
ret = -ENOENT;
}
/* No, we are not at the end of the directory */
else
{
/* Verify that the pid still refers to an active task/thread */
pid = procdir->pid;
tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
}
/* The TCB is still valid (or at least was when we entered this
* function)
*
* Handle the directory listing by the node type.
*/
switch (procdir->node->node)
{
case PROC_LEVEL0: /* Top level directory */
DEBUGASSERT(procdir->base.level == 1);
node = g_level0info[index];
break;
case PROC_GROUP: /* Group sub-directory */
DEBUGASSERT(procdir->base.level == 2);
node = g_groupinfo[index];
break;
default:
return -ENOENT;
}
/* Save the filename and file type */
entry->d_type = node->dtype;
strlcpy(entry->d_name, node->name, sizeof(entry->d_name));
/* Set up the next directory entry offset. NOTE that we could use the
* standard f_pos instead of our own private index.
*/
procdir->base.index = index + 1;
ret = OK;
}
return ret;
}
/****************************************************************************
* Name: proc_rewindir
*
* Description: Reset directory read to the first entry
*
****************************************************************************/
static int proc_rewinddir(struct fs_dirent_s *dir)
{
FAR struct proc_dir_s *priv;
DEBUGASSERT(dir != NULL);
priv = (FAR struct proc_dir_s *)dir;
priv->base.index = 0;
return OK;
}
/****************************************************************************
* Name: proc_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int proc_stat(const char *relpath, struct stat *buf)
{
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
unsigned long tmp;
FAR char *ptr;
pid_t pid;
/* Two path forms are accepted:
*
* "<pid>" - If <pid> refers to a currently active task/thread, then it
* is a directory
* "<pid>/<node>" - If <node> is a recognized node then, then it
* is a file or directory.
*/
ptr = NULL;
if (strncmp(relpath, "self", 4) == 0)
{
tmp = nxsched_gettid(); /* Get the TID of the calling task */
ptr = (FAR char *)relpath + 4; /* Discard const */
}
else
{
tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
}
if (ptr == NULL)
{
ferr("ERROR: Invalid path \"%s\"\n", relpath);
return -ENOENT;
}
/* A valid PID would be in the range of 0-INT_MAX (0 is reserved for the
* IDLE thread).
*/
if (tmp > INT_MAX)
{
ferr("ERROR: Invalid PID %ld\n", tmp);
return -ENOENT;
}
/* Now verify that a task with this task/thread ID exists */
pid = (pid_t)tmp;
tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
}
/* Was the <pid> the final element of the path? */
memset(buf, 0, sizeof(struct stat));
if (*ptr == '\0' || strcmp(ptr, "/") == 0)
{
/* Yes ... It's a read-only directory */
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
}
/* Verify that the process ID is followed by valid path segment delimiter */
else if (*ptr != '/')
{
/* We are required to return -ENOENT all all invalid paths */
ferr("ERROR: Bad delimiter '%c' in relpath '%s'\n", *ptr, relpath);
return -ENOENT;
}
else
{
/* Otherwise, the second segment of the relpath should be a well
* known node of the task/thread directory structure.
*/
/* Skip over the path segment delimiter */
ptr++;
/* Lookup the well-known node associated with the relative path. */
node = proc_findnode(ptr);
if (node == NULL)
{
ferr("ERROR: Invalid path \"%s\"\n", relpath);
return -ENOENT;
}
/* If the node exists, it is the name for a read-only file or
* directory.
*/
if (node->dtype == DTYPE_FILE)
{
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
}
else
{
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
}
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
#endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhang_en/nuttx.git
git@gitee.com:zhang_en/nuttx.git
zhang_en
nuttx
nuttx
master

搜索帮助

371d5123 14472233 46e8bd33 14472233