fs_procfsproc.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /****************************************************************************
  2. * fs/procfs/fs_procfsproc.c
  3. *
  4. * Copyright (C) 2013-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/statfs.h>
  41. #include <sys/stat.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <fcntl.h>
  48. #include <assert.h>
  49. #include <errno.h>
  50. #include <debug.h>
  51. #include <nuttx/irq.h>
  52. #include <nuttx/arch.h>
  53. #include <nuttx/sched.h>
  54. #include <nuttx/kmalloc.h>
  55. #include <nuttx/fs/fs.h>
  56. #include <nuttx/fs/procfs.h>
  57. #include <nuttx/fs/dirent.h>
  58. #ifdef CONFIG_SCHED_CPULOAD
  59. # include <nuttx/clock.h>
  60. #endif
  61. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
  62. #ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
  63. /****************************************************************************
  64. * Pre-processor Definitions
  65. ****************************************************************************/
  66. /* See include/nuttx/sched.h: */
  67. #undef HAVE_GROUPID
  68. #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
  69. # define HAVE_GROUPID 1
  70. #endif
  71. #ifdef CONFIG_DISABLE_PTHREAD
  72. # undef HAVE_GROUPID
  73. #endif
  74. /* Determines the size of an intermediate buffer that must be large enough
  75. * to handle the longest line generated by this logic.
  76. */
  77. #define STATUS_LINELEN 32
  78. /****************************************************************************
  79. * Private Type Definitions
  80. ****************************************************************************/
  81. /* This enumeration identifies all of the task/thread nodes that can be
  82. * accessed via the procfs file system.
  83. */
  84. enum proc_node_e
  85. {
  86. PROC_LEVEL0 = 0, /* The top-level directory */
  87. PROC_STATUS, /* Task/thread status */
  88. PROC_CMDLINE, /* Task command line */
  89. #ifdef CONFIG_SCHED_CPULOAD
  90. PROC_LOADAVG, /* Average CPU utilization */
  91. #endif
  92. PROC_STACK, /* Task stack info */
  93. PROC_GROUP, /* Group directory */
  94. PROC_GROUP_STATUS, /* Task group status */
  95. PROC_GROUP_FD /* Group file descriptors */
  96. };
  97. /* This structure associates a relative path name with an node in the task
  98. * procfs
  99. */
  100. struct proc_node_s
  101. {
  102. FAR const char *relpath; /* Relative path to the node */
  103. FAR const char *name; /* Terminal node segment name */
  104. uint8_t node; /* Type of node (see enum proc_node_e) */
  105. uint8_t dtype; /* dirent type (see include/dirent.h) */
  106. };
  107. /* This structure describes one open "file" */
  108. struct proc_file_s
  109. {
  110. struct procfs_file_s base; /* Base open file structure */
  111. FAR const struct proc_node_s *node; /* Describes the file node */
  112. pid_t pid; /* Task/thread ID */
  113. char line[STATUS_LINELEN]; /* Pre-allocated buffer for formatted lines */
  114. };
  115. /* This structure describes one open "directory" */
  116. struct proc_dir_s
  117. {
  118. struct procfs_dir_priv_s base; /* Base directory private data */
  119. FAR const struct proc_node_s *node; /* Directory node description */
  120. pid_t pid; /* ID of task/thread for attributes */
  121. };
  122. /****************************************************************************
  123. * Private Data
  124. ****************************************************************************/
  125. static FAR const char *g_policy[4] =
  126. {
  127. "SCHED_FIFO", "SCHED_RR", "SCHED_SPORADIC", "SCHED_OTHER"
  128. };
  129. /****************************************************************************
  130. * Private Function Prototypes
  131. ****************************************************************************/
  132. /* Helpers */
  133. static FAR const struct proc_node_s *
  134. proc_findnode(FAR const char *relpath);
  135. static ssize_t proc_status(FAR struct proc_file_s *procfile,
  136. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  137. off_t offset);
  138. static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
  139. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  140. off_t offset);
  141. #ifdef CONFIG_SCHED_CPULOAD
  142. static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
  143. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  144. off_t offset);
  145. #endif
  146. static ssize_t proc_stack(FAR struct proc_file_s *procfile,
  147. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  148. off_t offset);
  149. static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
  150. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  151. off_t offset);
  152. static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
  153. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  154. off_t offset);
  155. /* File system methods */
  156. static int proc_open(FAR struct file *filep, FAR const char *relpath,
  157. int oflags, mode_t mode);
  158. static int proc_close(FAR struct file *filep);
  159. static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
  160. size_t buflen);
  161. static int proc_dup(FAR const struct file *oldp,
  162. FAR struct file *newp);
  163. static int proc_opendir(const char *relpath, FAR struct fs_dirent_s *dir);
  164. static int proc_closedir(FAR struct fs_dirent_s *dir);
  165. static int proc_readdir(FAR struct fs_dirent_s *dir);
  166. static int proc_rewinddir(FAR struct fs_dirent_s *dir);
  167. static int proc_stat(FAR const char *relpath, FAR struct stat *buf);
  168. /****************************************************************************
  169. * Private Data
  170. ****************************************************************************/
  171. /****************************************************************************
  172. * Public Data
  173. ****************************************************************************/
  174. /* See fs_mount.c -- this structure is explicitly externed there.
  175. * We use the old-fashioned kind of initializers so that this will compile
  176. * with any compiler.
  177. */
  178. const struct procfs_operations proc_operations =
  179. {
  180. proc_open, /* open */
  181. proc_close, /* close */
  182. proc_read, /* read */
  183. NULL, /* write */
  184. proc_dup, /* dup */
  185. proc_opendir, /* opendir */
  186. proc_closedir, /* closedir */
  187. proc_readdir, /* readdir */
  188. proc_rewinddir, /* rewinddir */
  189. proc_stat /* stat */
  190. };
  191. /* These structures provide information about every node */
  192. static const struct proc_node_s g_level0node =
  193. {
  194. "", "", (uint8_t)PROC_LEVEL0, DTYPE_DIRECTORY /* Top-level directory */
  195. };
  196. static const struct proc_node_s g_status =
  197. {
  198. "status", "status", (uint8_t)PROC_STATUS, DTYPE_FILE /* Task/thread status */
  199. };
  200. static const struct proc_node_s g_cmdline =
  201. {
  202. "cmdline", "cmdline", (uint8_t)PROC_CMDLINE, DTYPE_FILE /* Task command line */
  203. };
  204. #ifdef CONFIG_SCHED_CPULOAD
  205. static const struct proc_node_s g_loadavg =
  206. {
  207. "loadavg", "loadavg", (uint8_t)PROC_LOADAVG, DTYPE_FILE /* Average CPU utilization */
  208. };
  209. #endif
  210. static const struct proc_node_s g_stack =
  211. {
  212. "stack", "stack", (uint8_t)PROC_STACK, DTYPE_FILE /* Task stack info */
  213. };
  214. static const struct proc_node_s g_group =
  215. {
  216. "group", "group", (uint8_t)PROC_GROUP, DTYPE_DIRECTORY /* Group directory */
  217. };
  218. static const struct proc_node_s g_groupstatus =
  219. {
  220. "group/status", "status", (uint8_t)PROC_GROUP_STATUS, DTYPE_FILE /* Task group status */
  221. };
  222. static const struct proc_node_s g_groupfd =
  223. {
  224. "group/fd", "fd", (uint8_t)PROC_GROUP_FD, DTYPE_FILE /* Group file descriptors */
  225. };
  226. /* This is the list of all nodes */
  227. static FAR const struct proc_node_s * const g_nodeinfo[] =
  228. {
  229. &g_status, /* Task/thread status */
  230. &g_cmdline, /* Task command line */
  231. #ifdef CONFIG_SCHED_CPULOAD
  232. &g_loadavg, /* Average CPU utilization */
  233. #endif
  234. &g_stack, /* Task stack info */
  235. &g_group, /* Group directory */
  236. &g_groupstatus, /* Task group status */
  237. &g_groupfd /* Group file descriptors */
  238. };
  239. #define PROC_NNODES (sizeof(g_nodeinfo)/sizeof(FAR const struct proc_node_s * const))
  240. /* This is the list of all level0 nodes */
  241. static const struct proc_node_s * const g_level0info[] =
  242. {
  243. &g_status, /* Task/thread status */
  244. &g_cmdline, /* Task command line */
  245. #ifdef CONFIG_SCHED_CPULOAD
  246. &g_loadavg, /* Average CPU utilization */
  247. #endif
  248. &g_stack, /* Task stack info */
  249. &g_group, /* Group directory */
  250. };
  251. #define PROC_NLEVEL0NODES (sizeof(g_level0info)/sizeof(FAR const struct proc_node_s * const))
  252. /* This is the list of all group sub-directory nodes */
  253. static FAR const struct proc_node_s * const g_groupinfo[] =
  254. {
  255. &g_groupstatus, /* Task group status */
  256. &g_groupfd /* Group file descriptors */
  257. };
  258. #define PROC_NGROUPNODES (sizeof(g_groupinfo)/sizeof(FAR const struct proc_node_s * const))
  259. /* Names of task/thread states */
  260. static FAR const char *g_statenames[] =
  261. {
  262. "Invalid",
  263. "Waiting,Unlock",
  264. "Ready",
  265. #ifdef CONFIG_SMP
  266. "Assigned",
  267. #endif
  268. "Running",
  269. "Inactive",
  270. "Waiting,Semaphore",
  271. #ifndef CONFIG_DISABLE_SIGNALS
  272. "Waiting,Signal",
  273. #endif
  274. #ifndef CONFIG_DISABLE_MQUEUE
  275. "Waiting,MQ empty",
  276. "Waiting,MQ full"
  277. #endif
  278. };
  279. static FAR const char *g_ttypenames[4] =
  280. {
  281. "Task",
  282. "pthread",
  283. "Kthread",
  284. "Invalid"
  285. };
  286. /****************************************************************************
  287. * Private Functions
  288. ****************************************************************************/
  289. /****************************************************************************
  290. * Name: proc_findnode
  291. ****************************************************************************/
  292. static FAR const struct proc_node_s *proc_findnode(FAR const char *relpath)
  293. {
  294. int i;
  295. /* Search every string in g_nodeinfo or until a match is found */
  296. for (i = 0; i < PROC_NNODES; i++)
  297. {
  298. if (strcmp(g_nodeinfo[i]->relpath, relpath) == 0)
  299. {
  300. return g_nodeinfo[i];
  301. }
  302. }
  303. /* Not found */
  304. return NULL;
  305. }
  306. /****************************************************************************
  307. * Name: proc_status
  308. *
  309. * Description:
  310. * Format:
  311. *
  312. * 111111111122222222223
  313. * 123456789012345678901234567890
  314. * Name: xxxx... Task/thread name (See CONFIG_TASK_NAME_SIZE)
  315. * Type: xxxxxxx {Task, pthread, Kthread, Invalid}
  316. * PPID: xxxxx Parent thread ID
  317. * Group: xxxxx Group ID
  318. * CPU: xxx CPU (CONFIG_SMP only)
  319. * State: xxxxxxxx,xxxxxxxxx {Invalid, Waiting, Ready, Running, Inactive},
  320. * {Unlock, Semaphore, Signal, MQ empty, MQ full}
  321. * Flags: xxx N,P,X
  322. * Priority: nnn Decimal, 0-255
  323. * Scheduler: xxxxxxxxxxxxxx {SCHED_FIFO, SCHED_RR, SCHED_SPORADIC, SCHED_OTHER}
  324. * Sigmask: nnnnnnnn Hexadecimal, 32-bit
  325. *
  326. ****************************************************************************/
  327. static ssize_t proc_status(FAR struct proc_file_s *procfile,
  328. FAR struct tcb_s *tcb, FAR char *buffer,
  329. size_t buflen, off_t offset)
  330. {
  331. #ifdef CONFIG_SCHED_HAVE_PARENT
  332. FAR struct task_group_s *group;
  333. #endif
  334. FAR const char *policy;
  335. FAR const char *name;
  336. size_t remaining;
  337. size_t linesize;
  338. size_t copysize;
  339. size_t totalsize;
  340. remaining = buflen;
  341. totalsize = 0;
  342. /* Show the task name */
  343. #if CONFIG_TASK_NAME_SIZE > 0
  344. name = tcb->name;
  345. #else
  346. name = "<noname>";
  347. #endif
  348. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n",
  349. "Name:", name);
  350. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  351. totalsize += copysize;
  352. buffer += copysize;
  353. remaining -= copysize;
  354. if (totalsize >= buflen)
  355. {
  356. return totalsize;
  357. }
  358. /* Show the thread type */
  359. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "Type:",
  360. g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >>
  361. TCB_FLAG_TTYPE_SHIFT]);
  362. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  363. totalsize += copysize;
  364. buffer += copysize;
  365. remaining -= copysize;
  366. if (totalsize >= buflen)
  367. {
  368. return totalsize;
  369. }
  370. #ifdef CONFIG_SCHED_HAVE_PARENT
  371. group = tcb->group;
  372. DEBUGASSERT(group);
  373. #ifdef HAVE_GROUPID
  374. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "Group:",
  375. group->tg_pgid);
  376. #else
  377. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "PPID:",
  378. group->tg_ppid);
  379. #endif
  380. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  381. totalsize += copysize;
  382. buffer += copysize;
  383. remaining -= copysize;
  384. if (totalsize >= buflen)
  385. {
  386. return totalsize;
  387. }
  388. #endif
  389. #ifdef CONFIG_SMP
  390. if (tcb->task_state >= FIRST_ASSIGNED_STATE &&
  391. tcb->task_state <= LAST_ASSIGNED_STATE)
  392. {
  393. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "CPU:",
  394. tcb->cpu);
  395. }
  396. else
  397. {
  398. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s---\n", "CPU:");
  399. }
  400. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  401. totalsize += copysize;
  402. buffer += copysize;
  403. remaining -= copysize;
  404. if (totalsize >= buflen)
  405. {
  406. return totalsize;
  407. }
  408. #endif
  409. /* Show the thread state */
  410. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "State:",
  411. g_statenames[tcb->task_state]);
  412. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  413. totalsize += copysize;
  414. buffer += copysize;
  415. remaining -= copysize;
  416. if (totalsize >= buflen)
  417. {
  418. return totalsize;
  419. }
  420. /* Show task flags */
  421. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%c%c%c\n", "Flags:",
  422. tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : '-',
  423. tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : '-',
  424. tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-');
  425. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  426. totalsize += copysize;
  427. buffer += copysize;
  428. remaining -= copysize;
  429. if (totalsize >= buflen)
  430. {
  431. return totalsize;
  432. }
  433. /* Show the thread priority */
  434. #ifdef CONFIG_PRIORITY_INHERITANCE
  435. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d (%d)\n", "Priority:",
  436. tcb->sched_priority, tcb->base_priority);
  437. #else
  438. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "Priority:",
  439. tcb->sched_priority);
  440. #endif
  441. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  442. totalsize += copysize;
  443. buffer += copysize;
  444. remaining -= copysize;
  445. if (totalsize >= buflen)
  446. {
  447. return totalsize;
  448. }
  449. /* Show the scheduler policy */
  450. policy = g_policy[(tcb->flags & TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT];
  451. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "Scheduler:",
  452. policy);
  453. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  454. totalsize += copysize;
  455. buffer += copysize;
  456. remaining -= copysize;
  457. if (totalsize >= buflen)
  458. {
  459. return totalsize;
  460. }
  461. /* Show the signal mask */
  462. #ifndef CONFIG_DISABLE_SIGNALS
  463. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%08x\n", "SigMask:",
  464. tcb->sigprocmask);
  465. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  466. totalsize += copysize;
  467. #endif
  468. return totalsize;
  469. }
  470. /****************************************************************************
  471. * Name: proc_cmdline
  472. ****************************************************************************/
  473. static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
  474. FAR struct tcb_s *tcb, FAR char *buffer,
  475. size_t buflen, off_t offset)
  476. {
  477. FAR struct task_tcb_s *ttcb;
  478. FAR const char *name;
  479. FAR char **argv;
  480. size_t remaining;
  481. size_t linesize;
  482. size_t copysize;
  483. size_t totalsize;
  484. remaining = buflen;
  485. totalsize = 0;
  486. /* Show the task name */
  487. #if CONFIG_TASK_NAME_SIZE > 0
  488. name = tcb->name;
  489. #else
  490. name = "<noname>";
  491. #endif
  492. linesize = strlen(name);
  493. memcpy(procfile->line, name, linesize);
  494. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  495. totalsize += copysize;
  496. buffer += copysize;
  497. remaining -= copysize;
  498. if (totalsize >= buflen)
  499. {
  500. return totalsize;
  501. }
  502. #ifndef CONFIG_DISABLE_PTHREAD
  503. /* Show the pthread argument */
  504. if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
  505. {
  506. FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
  507. linesize = snprintf(procfile->line, STATUS_LINELEN, " 0x%p\n", ptcb->arg);
  508. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  509. totalsize += copysize;
  510. buffer += copysize;
  511. remaining -= copysize;
  512. return totalsize;
  513. }
  514. #endif
  515. /* Show the task argument list (skipping over the name) */
  516. ttcb = (FAR struct task_tcb_s *)tcb;
  517. for (argv = ttcb->argv + 1; *argv; argv++)
  518. {
  519. linesize = snprintf(procfile->line, STATUS_LINELEN, " %s", *argv);
  520. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  521. totalsize += copysize;
  522. buffer += copysize;
  523. remaining -= copysize;
  524. if (totalsize >= buflen)
  525. {
  526. return totalsize;
  527. }
  528. }
  529. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n");
  530. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  531. totalsize += copysize;
  532. return totalsize;
  533. }
  534. /****************************************************************************
  535. * Name: proc_loadavg
  536. ****************************************************************************/
  537. #ifdef CONFIG_SCHED_CPULOAD
  538. static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
  539. FAR struct tcb_s *tcb, FAR char *buffer,
  540. size_t buflen, off_t offset)
  541. {
  542. struct cpuload_s cpuload;
  543. uint32_t intpart;
  544. uint32_t fracpart;
  545. size_t linesize;
  546. size_t copysize;
  547. /* Sample the counts for the thread. clock_cpuload should only fail if
  548. * the PID is not valid. This could happen if the thread exited sometime
  549. * after the procfs entry was opened.
  550. */
  551. (void)clock_cpuload(procfile->pid, &cpuload);
  552. /* On the simulator, you may hit cpuload.total == 0, but probably never on
  553. * real hardware.
  554. */
  555. if (cpuload.total > 0)
  556. {
  557. uint32_t tmp;
  558. tmp = (1000 * cpuload.active) / cpuload.total;
  559. intpart = tmp / 10;
  560. fracpart = tmp - 10 * intpart;
  561. }
  562. else
  563. {
  564. intpart = 0;
  565. fracpart = 0;
  566. }
  567. linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d.%01d%%",
  568. intpart, fracpart);
  569. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset);
  570. return copysize;
  571. }
  572. #endif
  573. /****************************************************************************
  574. * Name: proc_stack
  575. ****************************************************************************/
  576. static ssize_t proc_stack(FAR struct proc_file_s *procfile,
  577. FAR struct tcb_s *tcb, FAR char *buffer,
  578. size_t buflen, off_t offset)
  579. {
  580. size_t remaining;
  581. size_t linesize;
  582. size_t copysize;
  583. size_t totalsize;
  584. remaining = buflen;
  585. totalsize = 0;
  586. /* Show the stack base address */
  587. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s0x%p\n",
  588. "StackBase:", tcb->adj_stack_ptr);
  589. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  590. totalsize += copysize;
  591. buffer += copysize;
  592. remaining -= copysize;
  593. if (totalsize >= buflen)
  594. {
  595. return totalsize;
  596. }
  597. /* Show the stack size */
  598. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
  599. "StackSize:", (long)tcb->adj_stack_size);
  600. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  601. totalsize += copysize;
  602. buffer += copysize;
  603. remaining -= copysize;
  604. #ifdef CONFIG_STACK_COLORATION
  605. if (totalsize >= buflen)
  606. {
  607. return totalsize;
  608. }
  609. /* Show the stack size */
  610. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
  611. "StackUsed:", (long)up_check_tcbstack(tcb));
  612. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  613. totalsize += copysize;
  614. buffer += copysize;
  615. remaining -= copysize;
  616. #endif
  617. return totalsize;
  618. }
  619. /****************************************************************************
  620. * Name: proc_groupstatus
  621. ****************************************************************************/
  622. static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
  623. FAR struct tcb_s *tcb, FAR char *buffer,
  624. size_t buflen, off_t offset)
  625. {
  626. FAR struct task_group_s *group = tcb->group;
  627. size_t remaining;
  628. size_t linesize;
  629. size_t copysize;
  630. size_t totalsize;
  631. #ifdef HAVE_GROUP_MEMBERS
  632. int i;
  633. #endif
  634. DEBUGASSERT(group);
  635. remaining = buflen;
  636. totalsize = 0;
  637. /* Show the group IDs */
  638. #ifdef HAVE_GROUP_MEMBERS
  639. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  640. "Group ID:", group->tg_gid);
  641. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  642. totalsize += copysize;
  643. buffer += copysize;
  644. remaining -= copysize;
  645. if (totalsize >= buflen)
  646. {
  647. return totalsize;
  648. }
  649. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  650. "Parent ID:", group->tg_pgid);
  651. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  652. totalsize += copysize;
  653. buffer += copysize;
  654. remaining -= copysize;
  655. if (totalsize >= buflen)
  656. {
  657. return totalsize;
  658. }
  659. #endif
  660. #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
  661. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  662. "Main task:", group->tg_task);
  663. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  664. totalsize += copysize;
  665. buffer += copysize;
  666. remaining -= copysize;
  667. if (totalsize >= buflen)
  668. {
  669. return totalsize;
  670. }
  671. #endif
  672. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s0x%02x\n",
  673. "Flags:", group->tg_flags);
  674. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  675. totalsize += copysize;
  676. buffer += copysize;
  677. remaining -= copysize;
  678. if (totalsize >= buflen)
  679. {
  680. return totalsize;
  681. }
  682. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  683. "Members:", group->tg_nmembers);
  684. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  685. totalsize += copysize;
  686. buffer += copysize;
  687. remaining -= copysize;
  688. #ifdef HAVE_GROUP_MEMBERS
  689. if (totalsize >= buflen)
  690. {
  691. return totalsize;
  692. }
  693. linesize = snprintf(procfile->line, STATUS_LINELEN, "Member IDs:");
  694. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  695. totalsize += copysize;
  696. buffer += copysize;
  697. remaining -= copysize;
  698. if (totalsize >= buflen)
  699. {
  700. return totalsize;
  701. }
  702. for (i = 0; i < group->tg_nmembers; i++)
  703. {
  704. linesize = snprintf(procfile->line, STATUS_LINELEN, " %d", group->tg_members[i]);
  705. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  706. totalsize += copysize;
  707. buffer += copysize;
  708. remaining -= copysize;
  709. if (totalsize >= buflen)
  710. {
  711. return totalsize;
  712. }
  713. }
  714. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n");
  715. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  716. totalsize += copysize;
  717. buffer += copysize;
  718. remaining -= copysize;
  719. #endif
  720. return totalsize;
  721. }
  722. /****************************************************************************
  723. * Name: proc_groupfd
  724. ****************************************************************************/
  725. static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
  726. FAR struct tcb_s *tcb, FAR char *buffer,
  727. size_t buflen, off_t offset)
  728. {
  729. FAR struct task_group_s *group = tcb->group;
  730. #if CONFIG_NFILE_DESCRIPTORS > 0 /* Guaranteed to be true */
  731. FAR struct file *file;
  732. #endif
  733. #if CONFIG_NSOCKET_DESCRIPTORS > 0
  734. FAR struct socket *socket;
  735. #endif
  736. size_t remaining;
  737. size_t linesize;
  738. size_t copysize;
  739. size_t totalsize;
  740. int i;
  741. DEBUGASSERT(group);
  742. remaining = buflen;
  743. totalsize = 0;
  744. #if CONFIG_NFILE_DESCRIPTORS > 0 /* Guaranteed to be true */
  745. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-8s %s\n",
  746. "FD", "POS", "OFLAGS");
  747. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  748. totalsize += copysize;
  749. buffer += copysize;
  750. remaining -= copysize;
  751. if (totalsize >= buflen)
  752. {
  753. return totalsize;
  754. }
  755. /* Examine each open file descriptor */
  756. for (i = 0, file = group->tg_filelist.fl_files; i < CONFIG_NFILE_DESCRIPTORS; i++, file++)
  757. {
  758. /* Is there an inode associated with the file descriptor? */
  759. if (file->f_inode)
  760. {
  761. linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d %8ld %04x\n",
  762. i, (long)file->f_pos, file->f_oflags);
  763. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  764. totalsize += copysize;
  765. buffer += copysize;
  766. remaining -= copysize;
  767. if (totalsize >= buflen)
  768. {
  769. return totalsize;
  770. }
  771. }
  772. }
  773. #endif
  774. #if CONFIG_NSOCKET_DESCRIPTORS > 0
  775. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-2s %-3s %s\n",
  776. "SD", "RF", "TYP", "FLAGS");
  777. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  778. totalsize += copysize;
  779. buffer += copysize;
  780. remaining -= copysize;
  781. if (totalsize >= buflen)
  782. {
  783. return totalsize;
  784. }
  785. /* Examine each open socket descriptor */
  786. for (i = 0, socket = group->tg_socketlist.sl_sockets; i < CONFIG_NSOCKET_DESCRIPTORS; i++, socket++)
  787. {
  788. /* Is there an connection associated with the socket descriptor? */
  789. if (socket->s_conn)
  790. {
  791. linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d %2d %3d %02x",
  792. i + CONFIG_NFILE_DESCRIPTORS,
  793. socket->s_crefs, socket->s_type, socket->s_flags);
  794. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  795. totalsize += copysize;
  796. buffer += copysize;
  797. remaining -= copysize;
  798. if (totalsize >= buflen)
  799. {
  800. return totalsize;
  801. }
  802. }
  803. }
  804. #endif
  805. return totalsize;
  806. }
  807. /****************************************************************************
  808. * Name: proc_open
  809. ****************************************************************************/
  810. static int proc_open(FAR struct file *filep, FAR const char *relpath,
  811. int oflags, mode_t mode)
  812. {
  813. FAR struct proc_file_s *procfile;
  814. FAR const struct proc_node_s *node;
  815. FAR struct tcb_s *tcb;
  816. FAR char *ptr;
  817. irqstate_t flags;
  818. unsigned long tmp;
  819. pid_t pid;
  820. finfo("Open '%s'\n", relpath);
  821. /* PROCFS is read-only. Any attempt to open with any kind of write
  822. * access is not permitted.
  823. *
  824. * REVISIT: Write-able proc files could be quite useful.
  825. */
  826. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  827. {
  828. ferr("ERROR: Only O_RDONLY supported\n");
  829. return -EACCES;
  830. }
  831. /* The first segment of the relative path should be a task/thread ID */
  832. ptr = NULL;
  833. tmp = strtoul(relpath, &ptr, 10);
  834. if (!ptr || *ptr != '/')
  835. {
  836. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  837. return -ENOENT;
  838. }
  839. /* Skip over the slash */
  840. ptr++;
  841. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  842. * IDLE thread).
  843. */
  844. if (tmp >= 32768)
  845. {
  846. ferr("ERROR: Invalid PID %ld\n", tmp);
  847. return -ENOENT;
  848. }
  849. /* Now verify that a task with this task/thread ID exists */
  850. pid = (pid_t)tmp;
  851. flags = enter_critical_section();
  852. tcb = sched_gettcb(pid);
  853. leave_critical_section(flags);
  854. if (!tcb)
  855. {
  856. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  857. return -ENOENT;
  858. }
  859. /* The remaining segments of the relpath should be a well known node in
  860. * the task/thread tree.
  861. */
  862. node = proc_findnode(ptr);
  863. if (!node)
  864. {
  865. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  866. return -ENOENT;
  867. }
  868. /* The node must be a file, not a directory */
  869. if (!DIRENT_ISFILE(node->dtype))
  870. {
  871. ferr("ERROR: Path \"%s\" is not a regular file\n", relpath);
  872. return -EISDIR;
  873. }
  874. /* Allocate a container to hold the task and node selection */
  875. procfile = (FAR struct proc_file_s *)kmm_zalloc(sizeof(struct proc_file_s));
  876. if (!procfile)
  877. {
  878. ferr("ERROR: Failed to allocate file container\n");
  879. return -ENOMEM;
  880. }
  881. /* Initialize the file container */
  882. procfile->pid = pid;
  883. procfile->node = node;
  884. /* Save the index as the open-specific state in filep->f_priv */
  885. filep->f_priv = (FAR void *)procfile;
  886. return OK;
  887. }
  888. /****************************************************************************
  889. * Name: proc_close
  890. ****************************************************************************/
  891. static int proc_close(FAR struct file *filep)
  892. {
  893. FAR struct proc_file_s *procfile;
  894. /* Recover our private data from the struct file instance */
  895. procfile = (FAR struct proc_file_s *)filep->f_priv;
  896. DEBUGASSERT(procfile);
  897. /* Release the file container structure */
  898. kmm_free(procfile);
  899. filep->f_priv = NULL;
  900. return OK;
  901. }
  902. /****************************************************************************
  903. * Name: proc_read
  904. ****************************************************************************/
  905. static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
  906. size_t buflen)
  907. {
  908. FAR struct proc_file_s *procfile;
  909. FAR struct tcb_s *tcb;
  910. irqstate_t flags;
  911. ssize_t ret;
  912. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  913. /* Recover our private data from the struct file instance */
  914. procfile = (FAR struct proc_file_s *)filep->f_priv;
  915. DEBUGASSERT(procfile);
  916. /* Verify that the thread is still valid */
  917. flags = enter_critical_section();
  918. tcb = sched_gettcb(procfile->pid);
  919. if (!tcb)
  920. {
  921. ferr("ERROR: PID %d is not valid\n", (int)procfile->pid);
  922. leave_critical_section(flags);
  923. return -ENODEV;
  924. }
  925. /* Provide the requested data */
  926. switch (procfile->node->node)
  927. {
  928. case PROC_STATUS: /* Task/thread status */
  929. ret = proc_status(procfile, tcb, buffer, buflen, filep->f_pos);
  930. break;
  931. case PROC_CMDLINE: /* Task command line */
  932. ret = proc_cmdline(procfile, tcb, buffer, buflen, filep->f_pos);
  933. break;
  934. #ifdef CONFIG_SCHED_CPULOAD
  935. case PROC_LOADAVG: /* Average CPU utilization */
  936. ret = proc_loadavg(procfile, tcb, buffer, buflen, filep->f_pos);
  937. break;
  938. #endif
  939. case PROC_STACK: /* Task stack info */
  940. ret = proc_stack(procfile, tcb, buffer, buflen, filep->f_pos);
  941. break;
  942. case PROC_GROUP_STATUS: /* Task group status */
  943. ret = proc_groupstatus(procfile, tcb, buffer, buflen, filep->f_pos);
  944. break;
  945. case PROC_GROUP_FD: /* Group file descriptors */
  946. ret = proc_groupfd(procfile, tcb, buffer, buflen, filep->f_pos);
  947. break;
  948. default:
  949. ret = -EINVAL;
  950. break;
  951. }
  952. leave_critical_section(flags);
  953. /* Update the file offset */
  954. if (ret > 0)
  955. {
  956. filep->f_pos += ret;
  957. }
  958. return ret;
  959. }
  960. /****************************************************************************
  961. * Name: proc_dup
  962. *
  963. * Description:
  964. * Duplicate open file data in the new file structure.
  965. *
  966. ****************************************************************************/
  967. static int proc_dup(FAR const struct file *oldp, FAR struct file *newp)
  968. {
  969. FAR struct proc_file_s *oldfile;
  970. FAR struct proc_file_s *newfile;
  971. finfo("Dup %p->%p\n", oldp, newp);
  972. /* Recover our private data from the old struct file instance */
  973. oldfile = (FAR struct proc_file_s *)oldp->f_priv;
  974. DEBUGASSERT(oldfile);
  975. /* Allocate a new container to hold the task and node selection */
  976. newfile = (FAR struct proc_file_s *)kmm_malloc(sizeof(struct proc_file_s));
  977. if (!newfile)
  978. {
  979. ferr("ERROR: Failed to allocate file container\n");
  980. return -ENOMEM;
  981. }
  982. /* The copy the file information from the old container to the new */
  983. memcpy(newfile, oldfile, sizeof(struct proc_file_s));
  984. /* Save the new container in the new file structure */
  985. newp->f_priv = (FAR void *)newfile;
  986. return OK;
  987. }
  988. /****************************************************************************
  989. * Name: proc_opendir
  990. *
  991. * Description:
  992. * Open a directory for read access
  993. *
  994. ****************************************************************************/
  995. static int proc_opendir(FAR const char *relpath, FAR struct fs_dirent_s *dir)
  996. {
  997. FAR struct proc_dir_s *procdir;
  998. FAR const struct proc_node_s *node;
  999. FAR struct tcb_s *tcb;
  1000. irqstate_t flags;
  1001. unsigned long tmp;
  1002. FAR char *ptr;
  1003. pid_t pid;
  1004. finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
  1005. DEBUGASSERT(relpath && dir && !dir->u.procfs);
  1006. /* The relative must be either:
  1007. *
  1008. * (1) "<pid>" - The sub-directory of task/thread attributes, or
  1009. * (2) The name of a directory node under <pid>
  1010. */
  1011. /* Otherwise, the relative path should be a valid task/thread ID */
  1012. ptr = NULL;
  1013. tmp = strtoul(relpath, &ptr, 10);
  1014. if (!ptr || (*ptr != '\0' && *ptr != '/'))
  1015. {
  1016. /* strtoul failed or there is something in the path after the pid */
  1017. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1018. return -ENOENT;
  1019. }
  1020. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  1021. * IDLE thread).
  1022. */
  1023. if (tmp >= 32768)
  1024. {
  1025. ferr("ERROR: Invalid PID %ld\n", tmp);
  1026. return -ENOENT;
  1027. }
  1028. /* Now verify that a task with this task/thread ID exists */
  1029. pid = (pid_t)tmp;
  1030. flags = enter_critical_section();
  1031. tcb = sched_gettcb(pid);
  1032. leave_critical_section(flags);
  1033. if (!tcb)
  1034. {
  1035. ferr("ERROR: PID %d is not valid\n", (int)pid);
  1036. return -ENOENT;
  1037. }
  1038. /* Allocate the directory structure. Note that the index and procentry
  1039. * pointer are implicitly nullified by kmm_zalloc(). Only the remaining,
  1040. * non-zero entries will need be initialized.
  1041. */
  1042. procdir = (FAR struct proc_dir_s *)kmm_zalloc(sizeof(struct proc_dir_s));
  1043. if (!procdir)
  1044. {
  1045. ferr("ERROR: Failed to allocate the directory structure\n");
  1046. return -ENOMEM;
  1047. }
  1048. /* Was the <pid> the final element of the path? */
  1049. if (*ptr != '\0' && strcmp(ptr, "/") != 0)
  1050. {
  1051. /* There is something in the path after the pid. Skip over the path
  1052. * segment delimiter and see if we can identify the node of interest.
  1053. */
  1054. ptr++;
  1055. node = proc_findnode(ptr);
  1056. if (!node)
  1057. {
  1058. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1059. kmm_free(procdir);
  1060. return -ENOENT;
  1061. }
  1062. /* The node must be a directory, not a file */
  1063. if (!DIRENT_ISDIRECTORY(node->dtype))
  1064. {
  1065. ferr("ERROR: Path \"%s\" is not a directory\n", relpath);
  1066. kmm_free(procdir);
  1067. return -ENOTDIR;
  1068. }
  1069. /* This is a second level directory */
  1070. procdir->base.level = 2;
  1071. procdir->base.nentries = PROC_NGROUPNODES;
  1072. procdir->node = node;
  1073. }
  1074. else
  1075. {
  1076. /* Use the special level0 node */
  1077. procdir->base.level = 1;
  1078. procdir->base.nentries = PROC_NLEVEL0NODES;
  1079. procdir->node = &g_level0node;
  1080. }
  1081. procdir->pid = pid;
  1082. dir->u.procfs = (FAR void *)procdir;
  1083. return OK;
  1084. }
  1085. /****************************************************************************
  1086. * Name: proc_closedir
  1087. *
  1088. * Description: Close the directory listing
  1089. *
  1090. ****************************************************************************/
  1091. static int proc_closedir(FAR struct fs_dirent_s *dir)
  1092. {
  1093. FAR struct proc_dir_s *priv;
  1094. DEBUGASSERT(dir && dir->u.procfs);
  1095. priv = dir->u.procfs;
  1096. if (priv)
  1097. {
  1098. kmm_free(priv);
  1099. }
  1100. dir->u.procfs = NULL;
  1101. return OK;
  1102. }
  1103. /****************************************************************************
  1104. * Name: proc_readdir
  1105. *
  1106. * Description: Read the next directory entry
  1107. *
  1108. ****************************************************************************/
  1109. static int proc_readdir(struct fs_dirent_s *dir)
  1110. {
  1111. FAR struct proc_dir_s *procdir;
  1112. FAR const struct proc_node_s *node = NULL;
  1113. FAR struct tcb_s *tcb;
  1114. unsigned int index;
  1115. irqstate_t flags;
  1116. pid_t pid;
  1117. int ret;
  1118. DEBUGASSERT(dir && dir->u.procfs);
  1119. procdir = dir->u.procfs;
  1120. /* Have we reached the end of the directory */
  1121. index = procdir->base.index;
  1122. if (index >= procdir->base.nentries)
  1123. {
  1124. /* We signal the end of the directory by returning the special
  1125. * error -ENOENT
  1126. */
  1127. finfo("Entry %d: End of directory\n", index);
  1128. ret = -ENOENT;
  1129. }
  1130. /* No, we are not at the end of the directory */
  1131. else
  1132. {
  1133. /* Verify that the pid still refers to an active task/thread */
  1134. pid = procdir->pid;
  1135. flags = enter_critical_section();
  1136. tcb = sched_gettcb(pid);
  1137. leave_critical_section(flags);
  1138. if (!tcb)
  1139. {
  1140. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  1141. return -ENOENT;
  1142. }
  1143. /* The TCB is still valid (or at least was when we entered this function) */
  1144. /* Handle the directory listing by the node type */
  1145. switch (procdir->node->node)
  1146. {
  1147. case PROC_LEVEL0: /* Top level directory */
  1148. DEBUGASSERT(procdir->base.level == 1);
  1149. node = g_level0info[index];
  1150. break;
  1151. case PROC_GROUP: /* Group sub-directory */
  1152. DEBUGASSERT(procdir->base.level == 2);
  1153. node = g_groupinfo[index];
  1154. break;
  1155. default:
  1156. return -ENOENT;
  1157. }
  1158. /* Save the filename and file type */
  1159. dir->fd_dir.d_type = node->dtype;
  1160. strncpy(dir->fd_dir.d_name, node->name, NAME_MAX+1);
  1161. /* Set up the next directory entry offset. NOTE that we could use the
  1162. * standard f_pos instead of our own private index.
  1163. */
  1164. procdir->base.index = index + 1;
  1165. ret = OK;
  1166. }
  1167. return ret;
  1168. }
  1169. /****************************************************************************
  1170. * Name: proc_rewindir
  1171. *
  1172. * Description: Reset directory read to the first entry
  1173. *
  1174. ****************************************************************************/
  1175. static int proc_rewinddir(struct fs_dirent_s *dir)
  1176. {
  1177. FAR struct proc_dir_s *priv;
  1178. DEBUGASSERT(dir && dir->u.procfs);
  1179. priv = dir->u.procfs;
  1180. priv->base.index = 0;
  1181. return OK;
  1182. }
  1183. /****************************************************************************
  1184. * Name: proc_stat
  1185. *
  1186. * Description: Return information about a file or directory
  1187. *
  1188. ****************************************************************************/
  1189. static int proc_stat(const char *relpath, struct stat *buf)
  1190. {
  1191. FAR const struct proc_node_s *node;
  1192. FAR struct tcb_s *tcb;
  1193. unsigned long tmp;
  1194. FAR char *ptr;
  1195. irqstate_t flags;
  1196. pid_t pid;
  1197. /* Two path forms are accepted:
  1198. *
  1199. * "<pid>" - If <pid> refers to a currently active task/thread, then it
  1200. * is a directory
  1201. * "<pid>/<node>" - If <node> is a recognized node then, then it
  1202. * is a file or directory.
  1203. */
  1204. ptr = NULL;
  1205. tmp = strtoul(relpath, &ptr, 10);
  1206. if (!ptr)
  1207. {
  1208. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1209. return -ENOENT;
  1210. }
  1211. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  1212. * IDLE thread).
  1213. */
  1214. if (tmp >= 32768)
  1215. {
  1216. ferr("ERROR: Invalid PID %ld\n", tmp);
  1217. return -ENOENT;
  1218. }
  1219. /* Now verify that a task with this task/thread ID exists */
  1220. pid = (pid_t)tmp;
  1221. flags = enter_critical_section();
  1222. tcb = sched_gettcb(pid);
  1223. leave_critical_section(flags);
  1224. if (!tcb)
  1225. {
  1226. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  1227. return -ENOENT;
  1228. }
  1229. /* Was the <pid> the final element of the path? */
  1230. memset(buf, 0, sizeof(struct stat));
  1231. if (*ptr == '\0' || strcmp(ptr, "/") == 0)
  1232. {
  1233. /* Yes ... It's a read-only directory */
  1234. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
  1235. }
  1236. /* Verify that the process ID is followed by valid path segment delimiter */
  1237. else if (*ptr != '/')
  1238. {
  1239. /* We are required to return -ENOENT all all invalid paths */
  1240. ferr("ERROR: Bad delimiter '%c' in relpath '%s'\n", *ptr, relpath);
  1241. return -ENOENT;
  1242. }
  1243. else
  1244. {
  1245. /* Otherwise, the second segment of the relpath should be a well
  1246. * known node of the task/thread directory structure.
  1247. */
  1248. /* Skip over the path segment delimiter */
  1249. ptr++;
  1250. /* Lookup the well-known node associated with the relative path. */
  1251. node = proc_findnode(ptr);
  1252. if (!node)
  1253. {
  1254. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1255. return -ENOENT;
  1256. }
  1257. /* If the node exists, it is the name for a read-only file or
  1258. * directory.
  1259. */
  1260. if (node->dtype == DTYPE_FILE)
  1261. {
  1262. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  1263. }
  1264. else
  1265. {
  1266. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
  1267. }
  1268. }
  1269. return OK;
  1270. }
  1271. /****************************************************************************
  1272. * Public Functions
  1273. ****************************************************************************/
  1274. #endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
  1275. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */