12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- /****************************************************************************
- * fs/procfs/fs_procfsproc.c
- *
- * Copyright (C) 2013-2017 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <gnutt@nuttx.org>
- *
- * 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 <sys/types.h>
- #include <sys/statfs.h>
- #include <sys/stat.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <assert.h>
- #include <errno.h>
- #include <debug.h>
- #include <nuttx/irq.h>
- #include <nuttx/arch.h>
- #include <nuttx/sched.h>
- #include <nuttx/kmalloc.h>
- #include <nuttx/fs/fs.h>
- #include <nuttx/fs/procfs.h>
- #include <nuttx/fs/dirent.h>
- #ifdef CONFIG_SCHED_CPULOAD
- # include <nuttx/clock.h>
- #endif
- #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
- #ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
- /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
- /* 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
- /* Determines the size of an intermediate buffer that must be large enough
- * to handle the longest line generated by this logic.
- */
- #define STATUS_LINELEN 32
- /****************************************************************************
- * 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 */
- #ifdef CONFIG_SCHED_CPULOAD
- PROC_LOADAVG, /* Average CPU utilization */
- #endif
- PROC_STACK, /* Task stack info */
- PROC_GROUP, /* Group directory */
- PROC_GROUP_STATUS, /* Task group status */
- PROC_GROUP_FD /* Group file descriptors */
- };
- /* 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 */
- };
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- static FAR const char *g_policy[4] =
- {
- "SCHED_FIFO", "SCHED_RR", "SCHED_SPORADIC", "SCHED_OTHER"
- };
- /****************************************************************************
- * 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);
- #ifdef CONFIG_SCHED_CPULOAD
- 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
- 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);
- /* 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 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);
- 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 proc_operations =
- {
- proc_open, /* open */
- proc_close, /* close */
- proc_read, /* read */
- NULL, /* 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 */
- };
- #ifdef CONFIG_SCHED_CPULOAD
- static const struct proc_node_s g_loadavg =
- {
- "loadavg", "loadavg", (uint8_t)PROC_LOADAVG, DTYPE_FILE /* Average CPU utilization */
- };
- #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 */
- };
- /* 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 */
- #ifdef CONFIG_SCHED_CPULOAD
- &g_loadavg, /* Average CPU utilization */
- #endif
- &g_stack, /* Task stack info */
- &g_group, /* Group directory */
- &g_groupstatus, /* Task group status */
- &g_groupfd /* Group file descriptors */
- };
- #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 */
- #ifdef CONFIG_SCHED_CPULOAD
- &g_loadavg, /* Average CPU utilization */
- #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 */
- };
- #define PROC_NGROUPNODES (sizeof(g_groupinfo)/sizeof(FAR const struct proc_node_s * const))
- /* Names of task/thread states */
- static FAR const char *g_statenames[] =
- {
- "Invalid",
- "Waiting,Unlock",
- "Ready",
- #ifdef CONFIG_SMP
- "Assigned",
- #endif
- "Running",
- "Inactive",
- "Waiting,Semaphore",
- #ifndef CONFIG_DISABLE_SIGNALS
- "Waiting,Signal",
- #endif
- #ifndef CONFIG_DISABLE_MQUEUE
- "Waiting,MQ empty",
- "Waiting,MQ full"
- #endif
- };
- static FAR const char *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++)
- {
- if (strcmp(g_nodeinfo[i]->relpath, relpath) == 0)
- {
- 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, SCHED_OTHER}
- * 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)
- {
- #ifdef CONFIG_SCHED_HAVE_PARENT
- FAR struct task_group_s *group;
- #endif
- FAR const char *policy;
- 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 = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\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 = 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;
- }
- #ifdef CONFIG_SCHED_HAVE_PARENT
- group = tcb->group;
- DEBUGASSERT(group);
- #ifdef HAVE_GROUPID
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "Group:",
- group->tg_pgid);
- #else
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "PPID:",
- group->tg_ppid);
- #endif
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- #endif
- #ifdef CONFIG_SMP
- if (tcb->task_state >= FIRST_ASSIGNED_STATE &&
- tcb->task_state <= LAST_ASSIGNED_STATE)
- {
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "CPU:",
- tcb->cpu);
- }
- else
- {
- linesize = 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 */
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "State:",
- g_statenames[tcb->task_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 = snprintf(procfile->line, STATUS_LINELEN, "%-12s%c%c%c\n", "Flags:",
- tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : '-',
- tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : '-',
- 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 = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d (%d)\n", "Priority:",
- tcb->sched_priority, tcb->base_priority);
- #else
- linesize = 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 = 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 */
- #ifndef CONFIG_DISABLE_SIGNALS
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%08x\n", "SigMask:",
- tcb->sigprocmask);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- #endif
- 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 struct task_tcb_s *ttcb;
- FAR const char *name;
- FAR char **argv;
- 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;
- }
- #ifndef CONFIG_DISABLE_PTHREAD
- /* Show the pthread argument */
- if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
- {
- FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
- linesize = snprintf(procfile->line, STATUS_LINELEN, " 0x%p\n", ptcb->arg);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- return totalsize;
- }
- #endif
- /* Show the task argument list (skipping over the name) */
- ttcb = (FAR struct task_tcb_s *)tcb;
- for (argv = ttcb->argv + 1; *argv; argv++)
- {
- linesize = snprintf(procfile->line, STATUS_LINELEN, " %s", *argv);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- }
- linesize = snprintf(procfile->line, STATUS_LINELEN, "\n");
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- return totalsize;
- }
- /****************************************************************************
- * Name: proc_loadavg
- ****************************************************************************/
- #ifdef CONFIG_SCHED_CPULOAD
- 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.
- */
- (void)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 = snprintf(procfile->line, STATUS_LINELEN, "%3d.%01d%%",
- intpart, fracpart);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset);
- return copysize;
- }
- #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;
- remaining = buflen;
- totalsize = 0;
- /* Show the stack base address */
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s0x%p\n",
- "StackBase:", tcb->adj_stack_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 = 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;
- #ifdef CONFIG_STACK_COLORATION
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- /* Show the stack size */
- linesize = 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);
- remaining = buflen;
- totalsize = 0;
- /* Show the group IDs */
- #ifdef HAVE_GROUP_MEMBERS
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
- "Group ID:", group->tg_gid);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
- "Parent ID:", group->tg_pgid);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- #endif
- #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
- "Main task:", group->tg_task);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- #endif
- linesize = 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 = 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 = 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 = 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 = 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;
- #if CONFIG_NFILE_DESCRIPTORS > 0 /* Guaranteed to be true */
- FAR struct file *file;
- #endif
- #if CONFIG_NSOCKET_DESCRIPTORS > 0
- FAR struct socket *socket;
- #endif
- size_t remaining;
- size_t linesize;
- size_t copysize;
- size_t totalsize;
- int i;
- DEBUGASSERT(group);
- remaining = buflen;
- totalsize = 0;
- #if CONFIG_NFILE_DESCRIPTORS > 0 /* Guaranteed to be true */
- linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-8s %s\n",
- "FD", "POS", "OFLAGS");
- 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, file = group->tg_filelist.fl_files; i < CONFIG_NFILE_DESCRIPTORS; i++, file++)
- {
- /* Is there an inode associated with the file descriptor? */
- if (file->f_inode)
- {
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d %8ld %04x\n",
- i, (long)file->f_pos, file->f_oflags);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- }
- }
- #endif
- #if CONFIG_NSOCKET_DESCRIPTORS > 0
- linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-2s %-3s %s\n",
- "SD", "RF", "TYP", "FLAGS");
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- /* Examine each open socket descriptor */
- for (i = 0, socket = group->tg_socketlist.sl_sockets; i < CONFIG_NSOCKET_DESCRIPTORS; i++, socket++)
- {
- /* Is there an connection associated with the socket descriptor? */
- if (socket->s_conn)
- {
- linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d %2d %3d %02x",
- i + CONFIG_NFILE_DESCRIPTORS,
- socket->s_crefs, socket->s_type, socket->s_flags);
- copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
- totalsize += copysize;
- buffer += copysize;
- remaining -= copysize;
- if (totalsize >= buflen)
- {
- return totalsize;
- }
- }
- }
- #endif
- return totalsize;
- }
- /****************************************************************************
- * 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;
- irqstate_t flags;
- unsigned long tmp;
- pid_t pid;
- finfo("Open '%s'\n", relpath);
- /* PROCFS is read-only. Any attempt to open with any kind of write
- * access is not permitted.
- *
- * REVISIT: Write-able proc files could be quite useful.
- */
- if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
- {
- ferr("ERROR: Only O_RDONLY supported\n");
- return -EACCES;
- }
- /* The first segment of the relative path should be a task/thread ID */
- ptr = NULL;
- tmp = strtoul(relpath, &ptr, 10);
- if (!ptr || *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-32767 (0 is reserved for the
- * IDLE thread).
- */
- if (tmp >= 32768)
- {
- ferr("ERROR: Invalid PID %ld\n", tmp);
- return -ENOENT;
- }
- /* Now verify that a task with this task/thread ID exists */
- pid = (pid_t)tmp;
- flags = enter_critical_section();
- tcb = sched_gettcb(pid);
- leave_critical_section(flags);
- if (!tcb)
- {
- ferr("ERROR: PID %d is no longer valid\n", (int)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)
- {
- 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)
- {
- 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);
- /* 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;
- irqstate_t flags;
- 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);
- /* Verify that the thread is still valid */
- flags = enter_critical_section();
- tcb = sched_gettcb(procfile->pid);
- if (!tcb)
- {
- ferr("ERROR: PID %d is not valid\n", (int)procfile->pid);
- leave_critical_section(flags);
- 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;
- #ifdef CONFIG_SCHED_CPULOAD
- case PROC_LOADAVG: /* Average CPU utilization */
- ret = proc_loadavg(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;
- default:
- ret = -EINVAL;
- break;
- }
- leave_critical_section(flags);
- /* Update the file offset */
- if (ret > 0)
- {
- filep->f_pos += ret;
- }
- 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);
- /* Allocate a new container to hold the task and node selection */
- newfile = (FAR struct proc_file_s *)kmm_malloc(sizeof(struct proc_file_s));
- if (!newfile)
- {
- 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;
- irqstate_t flags;
- unsigned long tmp;
- FAR char *ptr;
- pid_t pid;
- finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
- DEBUGASSERT(relpath && dir && !dir->u.procfs);
- /* The relative must be either:
- *
- * (1) "<pid>" - The sub-directory of task/thread attributes, or
- * (2) The name of a directory node under <pid>
- */
- /* Otherwise, the relative path should be a valid task/thread ID */
- ptr = NULL;
- tmp = strtoul(relpath, &ptr, 10);
- if (!ptr || (*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-32767 (0 is reserved for the
- * IDLE thread).
- */
- if (tmp >= 32768)
- {
- ferr("ERROR: Invalid PID %ld\n", tmp);
- return -ENOENT;
- }
- /* Now verify that a task with this task/thread ID exists */
- pid = (pid_t)tmp;
- flags = enter_critical_section();
- tcb = sched_gettcb(pid);
- leave_critical_section(flags);
- if (!tcb)
- {
- ferr("ERROR: PID %d is not valid\n", (int)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 = (FAR struct proc_dir_s *)kmm_zalloc(sizeof(struct proc_dir_s));
- if (!procdir)
- {
- 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)
- {
- 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->u.procfs = (FAR void *)procdir;
- return OK;
- }
- /****************************************************************************
- * Name: proc_closedir
- *
- * Description: Close the directory listing
- *
- ****************************************************************************/
- static int proc_closedir(FAR struct fs_dirent_s *dir)
- {
- FAR struct proc_dir_s *priv;
- DEBUGASSERT(dir && dir->u.procfs);
- priv = dir->u.procfs;
- if (priv)
- {
- kmm_free(priv);
- }
- dir->u.procfs = NULL;
- return OK;
- }
- /****************************************************************************
- * Name: proc_readdir
- *
- * Description: Read the next directory entry
- *
- ****************************************************************************/
- static int proc_readdir(struct fs_dirent_s *dir)
- {
- FAR struct proc_dir_s *procdir;
- FAR const struct proc_node_s *node = NULL;
- FAR struct tcb_s *tcb;
- unsigned int index;
- irqstate_t flags;
- pid_t pid;
- int ret;
- DEBUGASSERT(dir && dir->u.procfs);
- procdir = dir->u.procfs;
- /* 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;
- flags = enter_critical_section();
- tcb = sched_gettcb(pid);
- leave_critical_section(flags);
- if (!tcb)
- {
- ferr("ERROR: PID %d is no longer valid\n", (int)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 */
- dir->fd_dir.d_type = node->dtype;
- strncpy(dir->fd_dir.d_name, node->name, NAME_MAX+1);
- /* 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 && dir->u.procfs);
- priv = dir->u.procfs;
- 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;
- irqstate_t flags;
- 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;
- tmp = strtoul(relpath, &ptr, 10);
- if (!ptr)
- {
- ferr("ERROR: Invalid path \"%s\"\n", relpath);
- return -ENOENT;
- }
- /* A valid PID would be in the range of 0-32767 (0 is reserved for the
- * IDLE thread).
- */
- if (tmp >= 32768)
- {
- ferr("ERROR: Invalid PID %ld\n", tmp);
- return -ENOENT;
- }
- /* Now verify that a task with this task/thread ID exists */
- pid = (pid_t)tmp;
- flags = enter_critical_section();
- tcb = sched_gettcb(pid);
- leave_critical_section(flags);
- if (!tcb)
- {
- ferr("ERROR: PID %d is no longer valid\n", (int)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)
- {
- 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 */
|