fs_procfsproc.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. /****************************************************************************
  2. * fs/procfs/fs_procfsproc.c
  3. *
  4. * Copyright (C) 2013-2019 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. #ifdef CONFIG_SCHED_CRITMONITOR
  52. # include <time.h>
  53. #endif
  54. #include <nuttx/irq.h>
  55. #include <nuttx/arch.h>
  56. #include <nuttx/sched.h>
  57. #include <nuttx/kmalloc.h>
  58. #include <nuttx/environ.h>
  59. #include <nuttx/fs/fs.h>
  60. #include <nuttx/fs/procfs.h>
  61. #include <nuttx/fs/dirent.h>
  62. #if defined(CONFIG_SCHED_CPULOAD) || defined(CONFIG_SCHED_CRITMONITOR)
  63. # include <nuttx/clock.h>
  64. #endif
  65. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
  66. #ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
  67. /****************************************************************************
  68. * Pre-processor Definitions
  69. ****************************************************************************/
  70. /* See include/nuttx/sched.h: */
  71. #undef HAVE_GROUPID
  72. #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
  73. # define HAVE_GROUPID 1
  74. #endif
  75. #ifdef CONFIG_DISABLE_PTHREAD
  76. # undef HAVE_GROUPID
  77. #endif
  78. /* Determines the size of an intermediate buffer that must be large enough
  79. * to handle the longest line generated by this logic.
  80. */
  81. #define STATUS_LINELEN 32
  82. /****************************************************************************
  83. * Private Type Definitions
  84. ****************************************************************************/
  85. /* This enumeration identifies all of the task/thread nodes that can be
  86. * accessed via the procfs file system.
  87. */
  88. enum proc_node_e
  89. {
  90. PROC_LEVEL0 = 0, /* The top-level directory */
  91. PROC_STATUS, /* Task/thread status */
  92. PROC_CMDLINE, /* Task command line */
  93. #ifdef CONFIG_SCHED_CPULOAD
  94. PROC_LOADAVG, /* Average CPU utilization */
  95. #endif
  96. #ifdef CONFIG_SCHED_CRITMONITOR
  97. PROC_CRITMON, /* Critical section monitor */
  98. #endif
  99. PROC_STACK, /* Task stack info */
  100. PROC_GROUP, /* Group directory */
  101. PROC_GROUP_STATUS, /* Task group status */
  102. PROC_GROUP_FD /* Group file descriptors */
  103. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  104. , PROC_GROUP_ENV /* Group environment variables */
  105. #endif
  106. };
  107. /* This structure associates a relative path name with an node in the task
  108. * procfs
  109. */
  110. struct proc_node_s
  111. {
  112. FAR const char *relpath; /* Relative path to the node */
  113. FAR const char *name; /* Terminal node segment name */
  114. uint8_t node; /* Type of node (see enum proc_node_e) */
  115. uint8_t dtype; /* dirent type (see include/dirent.h) */
  116. };
  117. /* This structure describes one open "file" */
  118. struct proc_file_s
  119. {
  120. struct procfs_file_s base; /* Base open file structure */
  121. FAR const struct proc_node_s *node; /* Describes the file node */
  122. pid_t pid; /* Task/thread ID */
  123. char line[STATUS_LINELEN]; /* Pre-allocated buffer for formatted lines */
  124. };
  125. /* This structure describes one open "directory" */
  126. struct proc_dir_s
  127. {
  128. struct procfs_dir_priv_s base; /* Base directory private data */
  129. FAR const struct proc_node_s *node; /* Directory node description */
  130. pid_t pid; /* ID of task/thread for attributes */
  131. };
  132. /* This structure used with the env_foreach() callback */
  133. struct proc_envinfo_s
  134. {
  135. FAR struct proc_file_s *procfile;
  136. FAR char *buffer;
  137. FAR off_t offset;
  138. size_t buflen;
  139. size_t remaining;
  140. size_t totalsize;
  141. };
  142. /****************************************************************************
  143. * Private Data
  144. ****************************************************************************/
  145. static FAR const char *g_policy[4] =
  146. {
  147. "SCHED_FIFO", "SCHED_RR", "SCHED_SPORADIC", "SCHED_OTHER"
  148. };
  149. /****************************************************************************
  150. * Private Function Prototypes
  151. ****************************************************************************/
  152. /* Helpers */
  153. static FAR const struct proc_node_s *
  154. proc_findnode(FAR const char *relpath);
  155. static ssize_t proc_status(FAR struct proc_file_s *procfile,
  156. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  157. off_t offset);
  158. static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
  159. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  160. off_t offset);
  161. #ifdef CONFIG_SCHED_CPULOAD
  162. static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
  163. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  164. off_t offset);
  165. #endif
  166. #ifdef CONFIG_SCHED_CRITMONITOR
  167. static ssize_t proc_critmon(FAR struct proc_file_s *procfile,
  168. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  169. off_t offset);
  170. #endif
  171. static ssize_t proc_stack(FAR struct proc_file_s *procfile,
  172. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  173. off_t offset);
  174. static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
  175. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  176. off_t offset);
  177. static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
  178. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  179. off_t offset);
  180. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  181. static int proc_groupenv_callback(FAR void *arg, FAR const char *pair);
  182. static ssize_t proc_groupenv(FAR struct proc_file_s *procfile,
  183. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  184. off_t offset);
  185. #endif
  186. /* File system methods */
  187. static int proc_open(FAR struct file *filep, FAR const char *relpath,
  188. int oflags, mode_t mode);
  189. static int proc_close(FAR struct file *filep);
  190. static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
  191. size_t buflen);
  192. static int proc_dup(FAR const struct file *oldp,
  193. FAR struct file *newp);
  194. static int proc_opendir(const char *relpath, FAR struct fs_dirent_s *dir);
  195. static int proc_closedir(FAR struct fs_dirent_s *dir);
  196. static int proc_readdir(FAR struct fs_dirent_s *dir);
  197. static int proc_rewinddir(FAR struct fs_dirent_s *dir);
  198. static int proc_stat(FAR const char *relpath, FAR struct stat *buf);
  199. /****************************************************************************
  200. * Private Data
  201. ****************************************************************************/
  202. /****************************************************************************
  203. * Public Data
  204. ****************************************************************************/
  205. /* See fs_mount.c -- this structure is explicitly externed there.
  206. * We use the old-fashioned kind of initializers so that this will compile
  207. * with any compiler.
  208. */
  209. const struct procfs_operations proc_operations =
  210. {
  211. proc_open, /* open */
  212. proc_close, /* close */
  213. proc_read, /* read */
  214. NULL, /* write */
  215. proc_dup, /* dup */
  216. proc_opendir, /* opendir */
  217. proc_closedir, /* closedir */
  218. proc_readdir, /* readdir */
  219. proc_rewinddir, /* rewinddir */
  220. proc_stat /* stat */
  221. };
  222. /* These structures provide information about every node */
  223. static const struct proc_node_s g_level0node =
  224. {
  225. "", "", (uint8_t)PROC_LEVEL0, DTYPE_DIRECTORY /* Top-level directory */
  226. };
  227. static const struct proc_node_s g_status =
  228. {
  229. "status", "status", (uint8_t)PROC_STATUS, DTYPE_FILE /* Task/thread status */
  230. };
  231. static const struct proc_node_s g_cmdline =
  232. {
  233. "cmdline", "cmdline", (uint8_t)PROC_CMDLINE, DTYPE_FILE /* Task command line */
  234. };
  235. #ifdef CONFIG_SCHED_CPULOAD
  236. static const struct proc_node_s g_loadavg =
  237. {
  238. "loadavg", "loadavg", (uint8_t)PROC_LOADAVG, DTYPE_FILE /* Average CPU utilization */
  239. };
  240. #endif
  241. #ifdef CONFIG_SCHED_CRITMONITOR
  242. static const struct proc_node_s g_critmon =
  243. {
  244. "critmon", "critmon", (uint8_t)PROC_CRITMON, DTYPE_FILE /* Critical Section Monitor */
  245. };
  246. #endif
  247. static const struct proc_node_s g_stack =
  248. {
  249. "stack", "stack", (uint8_t)PROC_STACK, DTYPE_FILE /* Task stack info */
  250. };
  251. static const struct proc_node_s g_group =
  252. {
  253. "group", "group", (uint8_t)PROC_GROUP, DTYPE_DIRECTORY /* Group directory */
  254. };
  255. static const struct proc_node_s g_groupstatus =
  256. {
  257. "group/status", "status", (uint8_t)PROC_GROUP_STATUS, DTYPE_FILE /* Task group status */
  258. };
  259. static const struct proc_node_s g_groupfd =
  260. {
  261. "group/fd", "fd", (uint8_t)PROC_GROUP_FD, DTYPE_FILE /* Group file descriptors */
  262. };
  263. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  264. static const struct proc_node_s g_groupenv =
  265. {
  266. "group/env", "env", (uint8_t)PROC_GROUP_ENV, DTYPE_FILE /* Group environment variables */
  267. };
  268. #endif
  269. /* This is the list of all nodes */
  270. static FAR const struct proc_node_s * const g_nodeinfo[] =
  271. {
  272. &g_status, /* Task/thread status */
  273. &g_cmdline, /* Task command line */
  274. #ifdef CONFIG_SCHED_CPULOAD
  275. &g_loadavg, /* Average CPU utilization */
  276. #endif
  277. #ifdef CONFIG_SCHED_CRITMONITOR
  278. &g_critmon, /* Critical section Monitor */
  279. #endif
  280. &g_stack, /* Task stack info */
  281. &g_group, /* Group directory */
  282. &g_groupstatus, /* Task group status */
  283. &g_groupfd /* Group file descriptors */
  284. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  285. , &g_groupenv /* Group environment variables */
  286. #endif
  287. };
  288. #define PROC_NNODES (sizeof(g_nodeinfo)/sizeof(FAR const struct proc_node_s * const))
  289. /* This is the list of all level0 nodes */
  290. static const struct proc_node_s * const g_level0info[] =
  291. {
  292. &g_status, /* Task/thread status */
  293. &g_cmdline, /* Task command line */
  294. #ifdef CONFIG_SCHED_CPULOAD
  295. &g_loadavg, /* Average CPU utilization */
  296. #endif
  297. #ifdef CONFIG_SCHED_CRITMONITOR
  298. &g_critmon, /* Critical section monitor */
  299. #endif
  300. &g_stack, /* Task stack info */
  301. &g_group, /* Group directory */
  302. };
  303. #define PROC_NLEVEL0NODES (sizeof(g_level0info)/sizeof(FAR const struct proc_node_s * const))
  304. /* This is the list of all group sub-directory nodes */
  305. static FAR const struct proc_node_s * const g_groupinfo[] =
  306. {
  307. &g_groupstatus, /* Task group status */
  308. &g_groupfd /* Group file descriptors */
  309. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  310. , &g_groupenv /* Group environment variables */
  311. #endif
  312. };
  313. #define PROC_NGROUPNODES (sizeof(g_groupinfo)/sizeof(FAR const struct proc_node_s * const))
  314. /* Names of task/thread states */
  315. static FAR const char *g_statenames[] =
  316. {
  317. "Invalid",
  318. "Waiting,Unlock",
  319. "Ready",
  320. #ifdef CONFIG_SMP
  321. "Assigned",
  322. #endif
  323. "Running",
  324. "Inactive",
  325. "Waiting,Semaphore",
  326. "Waiting,Signal"
  327. #ifndef CONFIG_DISABLE_MQUEUE
  328. , "Waiting,MQ empty"
  329. , "Waiting,MQ full"
  330. #endif
  331. #ifdef CONFIG_SIG_SIGSTOP_ACTION
  332. , "Stopped"
  333. #endif
  334. };
  335. static FAR const char *g_ttypenames[4] =
  336. {
  337. "Task",
  338. "pthread",
  339. "Kthread",
  340. "Invalid"
  341. };
  342. /****************************************************************************
  343. * Private Functions
  344. ****************************************************************************/
  345. /****************************************************************************
  346. * Name: proc_findnode
  347. ****************************************************************************/
  348. static FAR const struct proc_node_s *proc_findnode(FAR const char *relpath)
  349. {
  350. int i;
  351. /* Search every string in g_nodeinfo or until a match is found */
  352. for (i = 0; i < PROC_NNODES; i++)
  353. {
  354. if (strcmp(g_nodeinfo[i]->relpath, relpath) == 0)
  355. {
  356. return g_nodeinfo[i];
  357. }
  358. }
  359. /* Not found */
  360. return NULL;
  361. }
  362. /****************************************************************************
  363. * Name: proc_status
  364. *
  365. * Description:
  366. * Format:
  367. *
  368. * 111111111122222222223
  369. * 123456789012345678901234567890
  370. * Name: xxxx... Task/thread name (See
  371. * CONFIG_TASK_NAME_SIZE)
  372. * Type: xxxxxxx {Task, pthread, Kthread, Invalid}
  373. * PPID: xxxxx Parent thread ID
  374. * Group: xxxxx Group ID
  375. * CPU: xxx CPU (CONFIG_SMP only)
  376. * State: xxxxxxxx,xxxxxxxxx {Invalid, Waiting, Ready, Running,
  377. * Inactive},
  378. * {Unlock, Semaphore, Signal, MQ empty,
  379. * MQ full}
  380. * Flags: xxx N,P,X
  381. * Priority: nnn Decimal, 0-255
  382. * Scheduler: xxxxxxxxxxxxxx {SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,
  383. * SCHED_OTHER}
  384. * Sigmask: nnnnnnnn Hexadecimal, 32-bit
  385. *
  386. ****************************************************************************/
  387. static ssize_t proc_status(FAR struct proc_file_s *procfile,
  388. FAR struct tcb_s *tcb, FAR char *buffer,
  389. size_t buflen, off_t offset)
  390. {
  391. #ifdef CONFIG_SCHED_HAVE_PARENT
  392. FAR struct task_group_s *group;
  393. #endif
  394. FAR const char *policy;
  395. FAR const char *name;
  396. size_t remaining;
  397. size_t linesize;
  398. size_t copysize;
  399. size_t totalsize;
  400. remaining = buflen;
  401. totalsize = 0;
  402. /* Show the task name */
  403. #if CONFIG_TASK_NAME_SIZE > 0
  404. name = tcb->name;
  405. #else
  406. name = "<noname>";
  407. #endif
  408. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n",
  409. "Name:", name);
  410. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  411. totalsize += copysize;
  412. buffer += copysize;
  413. remaining -= copysize;
  414. if (totalsize >= buflen)
  415. {
  416. return totalsize;
  417. }
  418. /* Show the thread type */
  419. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "Type:",
  420. g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >>
  421. TCB_FLAG_TTYPE_SHIFT]);
  422. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  423. totalsize += copysize;
  424. buffer += copysize;
  425. remaining -= copysize;
  426. if (totalsize >= buflen)
  427. {
  428. return totalsize;
  429. }
  430. #ifdef CONFIG_SCHED_HAVE_PARENT
  431. group = tcb->group;
  432. DEBUGASSERT(group != NULL);
  433. #ifdef HAVE_GROUPID
  434. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "Group:",
  435. group->tg_pgrpid);
  436. #else
  437. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "PPID:",
  438. group->tg_ppid);
  439. #endif
  440. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  441. totalsize += copysize;
  442. buffer += copysize;
  443. remaining -= copysize;
  444. if (totalsize >= buflen)
  445. {
  446. return totalsize;
  447. }
  448. #endif
  449. #ifdef CONFIG_SMP
  450. if (tcb->task_state >= FIRST_ASSIGNED_STATE &&
  451. tcb->task_state <= LAST_ASSIGNED_STATE)
  452. {
  453. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "CPU:",
  454. tcb->cpu);
  455. }
  456. else
  457. {
  458. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s---\n", "CPU:");
  459. }
  460. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  461. totalsize += copysize;
  462. buffer += copysize;
  463. remaining -= copysize;
  464. if (totalsize >= buflen)
  465. {
  466. return totalsize;
  467. }
  468. #endif
  469. /* Show the thread state */
  470. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "State:",
  471. g_statenames[tcb->task_state]);
  472. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  473. totalsize += copysize;
  474. buffer += copysize;
  475. remaining -= copysize;
  476. if (totalsize >= buflen)
  477. {
  478. return totalsize;
  479. }
  480. /* Show task flags */
  481. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%c%c%c\n", "Flags:",
  482. tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : '-',
  483. tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : '-',
  484. tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-');
  485. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  486. totalsize += copysize;
  487. buffer += copysize;
  488. remaining -= copysize;
  489. if (totalsize >= buflen)
  490. {
  491. return totalsize;
  492. }
  493. /* Show the thread priority */
  494. #ifdef CONFIG_PRIORITY_INHERITANCE
  495. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d (%d)\n", "Priority:",
  496. tcb->sched_priority, tcb->base_priority);
  497. #else
  498. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n", "Priority:",
  499. tcb->sched_priority);
  500. #endif
  501. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  502. totalsize += copysize;
  503. buffer += copysize;
  504. remaining -= copysize;
  505. if (totalsize >= buflen)
  506. {
  507. return totalsize;
  508. }
  509. /* Show the scheduler policy */
  510. policy = g_policy[(tcb->flags & TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT];
  511. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "Scheduler:",
  512. policy);
  513. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  514. totalsize += copysize;
  515. buffer += copysize;
  516. remaining -= copysize;
  517. if (totalsize >= buflen)
  518. {
  519. return totalsize;
  520. }
  521. /* Show the signal mask */
  522. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%08x\n", "SigMask:",
  523. tcb->sigprocmask);
  524. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  525. totalsize += copysize;
  526. return totalsize;
  527. }
  528. /****************************************************************************
  529. * Name: proc_cmdline
  530. ****************************************************************************/
  531. static ssize_t proc_cmdline(FAR struct proc_file_s *procfile,
  532. FAR struct tcb_s *tcb, FAR char *buffer,
  533. size_t buflen, off_t offset)
  534. {
  535. FAR struct task_tcb_s *ttcb;
  536. FAR const char *name;
  537. FAR char **argv;
  538. size_t remaining;
  539. size_t linesize;
  540. size_t copysize;
  541. size_t totalsize;
  542. remaining = buflen;
  543. totalsize = 0;
  544. /* Show the task name */
  545. #if CONFIG_TASK_NAME_SIZE > 0
  546. name = tcb->name;
  547. #else
  548. name = "<noname>";
  549. #endif
  550. linesize = strlen(name);
  551. memcpy(procfile->line, name, linesize);
  552. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  553. totalsize += copysize;
  554. buffer += copysize;
  555. remaining -= copysize;
  556. if (totalsize >= buflen)
  557. {
  558. return totalsize;
  559. }
  560. #ifndef CONFIG_DISABLE_PTHREAD
  561. /* Show the pthread argument */
  562. if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
  563. {
  564. FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
  565. linesize = snprintf(procfile->line, STATUS_LINELEN, " 0x%p\n",
  566. ptcb->arg);
  567. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  568. remaining, &offset);
  569. totalsize += copysize;
  570. buffer += copysize;
  571. remaining -= copysize;
  572. return totalsize;
  573. }
  574. #endif
  575. /* Show the task argument list (skipping over the name) */
  576. ttcb = (FAR struct task_tcb_s *)tcb;
  577. for (argv = ttcb->argv + 1; *argv; argv++)
  578. {
  579. linesize = snprintf(procfile->line, STATUS_LINELEN, " %s", *argv);
  580. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  581. remaining, &offset);
  582. totalsize += copysize;
  583. buffer += copysize;
  584. remaining -= copysize;
  585. if (totalsize >= buflen)
  586. {
  587. return totalsize;
  588. }
  589. }
  590. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n");
  591. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  592. remaining, &offset);
  593. totalsize += copysize;
  594. return totalsize;
  595. }
  596. /****************************************************************************
  597. * Name: proc_loadavg
  598. ****************************************************************************/
  599. #ifdef CONFIG_SCHED_CPULOAD
  600. static ssize_t proc_loadavg(FAR struct proc_file_s *procfile,
  601. FAR struct tcb_s *tcb, FAR char *buffer,
  602. size_t buflen, off_t offset)
  603. {
  604. struct cpuload_s cpuload;
  605. uint32_t intpart;
  606. uint32_t fracpart;
  607. size_t linesize;
  608. size_t copysize;
  609. /* Sample the counts for the thread. clock_cpuload should only fail if
  610. * the PID is not valid. This could happen if the thread exited sometime
  611. * after the procfs entry was opened.
  612. */
  613. (void)clock_cpuload(procfile->pid, &cpuload);
  614. /* On the simulator, you may hit cpuload.total == 0, but probably never on
  615. * real hardware.
  616. */
  617. if (cpuload.total > 0)
  618. {
  619. uint32_t tmp;
  620. tmp = (1000 * cpuload.active) / cpuload.total;
  621. intpart = tmp / 10;
  622. fracpart = tmp - 10 * intpart;
  623. }
  624. else
  625. {
  626. intpart = 0;
  627. fracpart = 0;
  628. }
  629. linesize = snprintf(procfile->line, STATUS_LINELEN, "%3d.%01d%%",
  630. intpart, fracpart);
  631. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset);
  632. return copysize;
  633. }
  634. #endif
  635. /****************************************************************************
  636. * Name: proc_critmon
  637. ****************************************************************************/
  638. #ifdef CONFIG_SCHED_CRITMONITOR
  639. static ssize_t proc_critmon(FAR struct proc_file_s *procfile,
  640. FAR struct tcb_s *tcb, FAR char *buffer,
  641. size_t buflen, off_t offset)
  642. {
  643. struct timespec maxtime;
  644. size_t remaining;
  645. size_t linesize;
  646. size_t copysize;
  647. size_t totalsize;
  648. remaining = buflen;
  649. totalsize = 0;
  650. /* Convert the for maximum time pre-emption disabled */
  651. if (tcb->premp_max > 0)
  652. {
  653. up_critmon_convert(tcb->premp_max, &maxtime);
  654. }
  655. else
  656. {
  657. maxtime.tv_sec = 0;
  658. maxtime.tv_nsec = 0;
  659. }
  660. /* Reset the maximum */
  661. tcb->premp_max = 0;
  662. /* Generate output for maximum time pre-emption disabled */
  663. linesize = snprintf(procfile->line, STATUS_LINELEN, "%lu.%09lu,",
  664. (unsigned long)maxtime.tv_sec,
  665. (unsigned long)maxtime.tv_nsec);
  666. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset);
  667. totalsize += copysize;
  668. buffer += copysize;
  669. remaining -= copysize;
  670. if (totalsize >= buflen)
  671. {
  672. return totalsize;
  673. }
  674. /* Convert and generate output for maximum time in a critical section */
  675. if (tcb->crit_max > 0)
  676. {
  677. up_critmon_convert(tcb->crit_max, &maxtime);
  678. }
  679. else
  680. {
  681. maxtime.tv_sec = 0;
  682. maxtime.tv_nsec = 0;
  683. }
  684. /* Reset the maximum */
  685. tcb->crit_max = 0;
  686. /* Generate output for maximum time in a critical section */
  687. linesize = snprintf(procfile->line, STATUS_LINELEN, "%lu.%09lu\n",
  688. (unsigned long)maxtime.tv_sec,
  689. (unsigned long)maxtime.tv_nsec);
  690. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset);
  691. totalsize += copysize;
  692. return totalsize;
  693. }
  694. #endif
  695. /****************************************************************************
  696. * Name: proc_stack
  697. ****************************************************************************/
  698. static ssize_t proc_stack(FAR struct proc_file_s *procfile,
  699. FAR struct tcb_s *tcb, FAR char *buffer,
  700. size_t buflen, off_t offset)
  701. {
  702. size_t remaining;
  703. size_t linesize;
  704. size_t copysize;
  705. size_t totalsize;
  706. remaining = buflen;
  707. totalsize = 0;
  708. /* Show the stack base address */
  709. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s0x%p\n",
  710. "StackBase:", tcb->adj_stack_ptr);
  711. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  712. totalsize += copysize;
  713. buffer += copysize;
  714. remaining -= copysize;
  715. if (totalsize >= buflen)
  716. {
  717. return totalsize;
  718. }
  719. /* Show the stack size */
  720. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
  721. "StackSize:", (long)tcb->adj_stack_size);
  722. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  723. totalsize += copysize;
  724. buffer += copysize;
  725. remaining -= copysize;
  726. #ifdef CONFIG_STACK_COLORATION
  727. if (totalsize >= buflen)
  728. {
  729. return totalsize;
  730. }
  731. /* Show the stack size */
  732. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%ld\n",
  733. "StackUsed:", (long)up_check_tcbstack(tcb));
  734. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  735. totalsize += copysize;
  736. buffer += copysize;
  737. remaining -= copysize;
  738. #endif
  739. return totalsize;
  740. }
  741. /****************************************************************************
  742. * Name: proc_groupstatus
  743. ****************************************************************************/
  744. static ssize_t proc_groupstatus(FAR struct proc_file_s *procfile,
  745. FAR struct tcb_s *tcb, FAR char *buffer,
  746. size_t buflen, off_t offset)
  747. {
  748. FAR struct task_group_s *group = tcb->group;
  749. size_t remaining;
  750. size_t linesize;
  751. size_t copysize;
  752. size_t totalsize;
  753. #ifdef HAVE_GROUP_MEMBERS
  754. int i;
  755. #endif
  756. DEBUGASSERT(group != NULL);
  757. remaining = buflen;
  758. totalsize = 0;
  759. /* Show the group IDs */
  760. #ifdef HAVE_GROUP_MEMBERS
  761. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  762. "Group ID:", group->tg_grpid);
  763. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  764. remaining, &offset);
  765. totalsize += copysize;
  766. buffer += copysize;
  767. remaining -= copysize;
  768. if (totalsize >= buflen)
  769. {
  770. return totalsize;
  771. }
  772. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  773. "Parent ID:", group->tg_pgrpid);
  774. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  775. remaining, &offset);
  776. totalsize += copysize;
  777. buffer += copysize;
  778. remaining -= copysize;
  779. if (totalsize >= buflen)
  780. {
  781. return totalsize;
  782. }
  783. #endif
  784. #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
  785. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  786. "Main task:", group->tg_task);
  787. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  788. remaining, &offset);
  789. totalsize += copysize;
  790. buffer += copysize;
  791. remaining -= copysize;
  792. if (totalsize >= buflen)
  793. {
  794. return totalsize;
  795. }
  796. #endif
  797. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s0x%02x\n",
  798. "Flags:", group->tg_flags);
  799. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  800. remaining, &offset);
  801. totalsize += copysize;
  802. buffer += copysize;
  803. remaining -= copysize;
  804. if (totalsize >= buflen)
  805. {
  806. return totalsize;
  807. }
  808. linesize = snprintf(procfile->line, STATUS_LINELEN, "%-12s%d\n",
  809. "Members:", group->tg_nmembers);
  810. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  811. remaining, &offset);
  812. totalsize += copysize;
  813. buffer += copysize;
  814. remaining -= copysize;
  815. #ifdef HAVE_GROUP_MEMBERS
  816. if (totalsize >= buflen)
  817. {
  818. return totalsize;
  819. }
  820. linesize = snprintf(procfile->line, STATUS_LINELEN, "Member IDs:");
  821. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  822. remaining, &offset);
  823. totalsize += copysize;
  824. buffer += copysize;
  825. remaining -= copysize;
  826. if (totalsize >= buflen)
  827. {
  828. return totalsize;
  829. }
  830. for (i = 0; i < group->tg_nmembers; i++)
  831. {
  832. linesize = snprintf(procfile->line, STATUS_LINELEN, " %d",
  833. group->tg_members[i]);
  834. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  835. remaining, &offset);
  836. totalsize += copysize;
  837. buffer += copysize;
  838. remaining -= copysize;
  839. if (totalsize >= buflen)
  840. {
  841. return totalsize;
  842. }
  843. }
  844. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n");
  845. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  846. totalsize += copysize;
  847. buffer += copysize;
  848. remaining -= copysize;
  849. #endif
  850. return totalsize;
  851. }
  852. /****************************************************************************
  853. * Name: proc_groupfd
  854. ****************************************************************************/
  855. static ssize_t proc_groupfd(FAR struct proc_file_s *procfile,
  856. FAR struct tcb_s *tcb, FAR char *buffer,
  857. size_t buflen, off_t offset)
  858. {
  859. FAR struct task_group_s *group = tcb->group;
  860. FAR struct file *file;
  861. #ifdef CONFIG_NET
  862. FAR struct socket *socket;
  863. #endif
  864. size_t remaining;
  865. size_t linesize;
  866. size_t copysize;
  867. size_t totalsize;
  868. int i;
  869. DEBUGASSERT(group != NULL);
  870. remaining = buflen;
  871. totalsize = 0;
  872. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-8s %s\n",
  873. "FD", "POS", "OFLAGS");
  874. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  875. totalsize += copysize;
  876. buffer += copysize;
  877. remaining -= copysize;
  878. if (totalsize >= buflen)
  879. {
  880. return totalsize;
  881. }
  882. /* Examine each open file descriptor */
  883. for (i = 0, file = group->tg_filelist.fl_files;
  884. i < CONFIG_NFILE_DESCRIPTORS;
  885. i++, file++)
  886. {
  887. /* Is there an inode associated with the file descriptor? */
  888. if (file->f_inode)
  889. {
  890. linesize = snprintf(procfile->line, STATUS_LINELEN,
  891. "%3d %8ld %04x\n", i, (long)file->f_pos,
  892. file->f_oflags);
  893. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  894. remaining, &offset);
  895. totalsize += copysize;
  896. buffer += copysize;
  897. remaining -= copysize;
  898. if (totalsize >= buflen)
  899. {
  900. return totalsize;
  901. }
  902. }
  903. }
  904. #ifdef CONFIG_NET
  905. linesize = snprintf(procfile->line, STATUS_LINELEN, "\n%-3s %-2s %-3s %s\n",
  906. "SD", "RF", "TYP", "FLAGS");
  907. copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset);
  908. totalsize += copysize;
  909. buffer += copysize;
  910. remaining -= copysize;
  911. if (totalsize >= buflen)
  912. {
  913. return totalsize;
  914. }
  915. /* Examine each open socket descriptor */
  916. for (i = 0, socket = group->tg_socketlist.sl_sockets;
  917. i < CONFIG_NSOCKET_DESCRIPTORS;
  918. i++, socket++)
  919. {
  920. /* Is there an connection associated with the socket descriptor? */
  921. if (socket->s_conn)
  922. {
  923. linesize = snprintf(procfile->line, STATUS_LINELEN,
  924. "%3d %2d %3d %02x",
  925. i + CONFIG_NFILE_DESCRIPTORS,
  926. socket->s_crefs, socket->s_type,
  927. socket->s_flags);
  928. copysize = procfs_memcpy(procfile->line, linesize, buffer,
  929. remaining, &offset);
  930. totalsize += copysize;
  931. buffer += copysize;
  932. remaining -= copysize;
  933. if (totalsize >= buflen)
  934. {
  935. return totalsize;
  936. }
  937. }
  938. }
  939. #endif
  940. return totalsize;
  941. }
  942. /****************************************************************************
  943. * Name: proc_groupenv_callback
  944. ****************************************************************************/
  945. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  946. static int proc_groupenv_callback(FAR void *arg, FAR const char *pair)
  947. {
  948. FAR struct proc_envinfo_s *info = (FAR struct proc_envinfo_s *)arg;
  949. FAR const char *src;
  950. FAR const char *value;
  951. FAR char *dest;
  952. char name[16 + 1];
  953. size_t linesize;
  954. size_t copysize;
  955. int namelen;
  956. DEBUGASSERT(arg != NULL && pair != NULL);
  957. /* Parse the name from the name/value pair */
  958. value = NULL;
  959. namelen = 0;
  960. for (src = pair, dest = name; *src != '=' && *src != '\0'; src++)
  961. {
  962. if (namelen < 16)
  963. {
  964. *dest++ = *src;
  965. namelen++;
  966. }
  967. }
  968. /* NUL terminate the name string */
  969. *dest = '\0';
  970. /* Skip over the '=' to get the value */
  971. if (*src == '=')
  972. {
  973. value = src + 1;
  974. }
  975. else
  976. {
  977. value = "";
  978. }
  979. /* Output the header */
  980. linesize = snprintf(info->procfile->line, STATUS_LINELEN, "%s=%s\n",
  981. name, value);
  982. copysize = procfs_memcpy(info->procfile->line, linesize, info->buffer,
  983. info->remaining, &info->offset);
  984. info->totalsize += copysize;
  985. info->buffer += copysize;
  986. info->remaining -= copysize;
  987. if (info->totalsize >= info->buflen)
  988. {
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. #endif
  994. /****************************************************************************
  995. * Name: proc_groupenv
  996. ****************************************************************************/
  997. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  998. static ssize_t proc_groupenv(FAR struct proc_file_s *procfile,
  999. FAR struct tcb_s *tcb, FAR char *buffer, size_t buflen,
  1000. off_t offset)
  1001. {
  1002. FAR struct task_group_s *group = tcb->group;
  1003. struct proc_envinfo_s info;
  1004. DEBUGASSERT(group != NULL);
  1005. /* Initialize the info structure */
  1006. info.procfile = procfile;
  1007. info.buffer = buffer;
  1008. info.offset = offset;
  1009. info.buflen = buflen;
  1010. info.remaining = buflen;
  1011. info.totalsize = 0;
  1012. /* Generate output for each environment variable */
  1013. (void)env_foreach(group, proc_groupenv_callback, &info);
  1014. return info.totalsize;
  1015. }
  1016. #endif
  1017. /****************************************************************************
  1018. * Name: proc_open
  1019. ****************************************************************************/
  1020. static int proc_open(FAR struct file *filep, FAR const char *relpath,
  1021. int oflags, mode_t mode)
  1022. {
  1023. FAR struct proc_file_s *procfile;
  1024. FAR const struct proc_node_s *node;
  1025. FAR struct tcb_s *tcb;
  1026. FAR char *ptr;
  1027. unsigned long tmp;
  1028. pid_t pid;
  1029. finfo("Open '%s'\n", relpath);
  1030. /* PROCFS is read-only. Any attempt to open with any kind of write
  1031. * access is not permitted.
  1032. *
  1033. * REVISIT: Write-able proc files could be quite useful.
  1034. */
  1035. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  1036. {
  1037. ferr("ERROR: Only O_RDONLY supported\n");
  1038. return -EACCES;
  1039. }
  1040. /* The first segment of the relative path should be a task/thread ID or
  1041. * the string "self".
  1042. */
  1043. ptr = NULL;
  1044. if (strncmp(relpath, "self", 4) == 0)
  1045. {
  1046. tmp = (unsigned long)getpid(); /* Get the PID of the calling task */
  1047. ptr = (FAR char *)relpath + 4; /* Discard const */
  1048. }
  1049. else
  1050. {
  1051. tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
  1052. }
  1053. if (ptr == NULL || *ptr != '/')
  1054. {
  1055. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1056. return -ENOENT;
  1057. }
  1058. /* Skip over the slash */
  1059. ptr++;
  1060. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  1061. * IDLE thread).
  1062. */
  1063. if (tmp >= 32768)
  1064. {
  1065. ferr("ERROR: Invalid PID %ld\n", tmp);
  1066. return -ENOENT;
  1067. }
  1068. /* Now verify that a task with this task/thread ID exists */
  1069. pid = (pid_t)tmp;
  1070. tcb = sched_gettcb(pid);
  1071. if (tcb == NULL)
  1072. {
  1073. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  1074. return -ENOENT;
  1075. }
  1076. /* The remaining segments of the relpath should be a well known node in
  1077. * the task/thread tree.
  1078. */
  1079. node = proc_findnode(ptr);
  1080. if (node == NULL)
  1081. {
  1082. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1083. return -ENOENT;
  1084. }
  1085. /* The node must be a file, not a directory */
  1086. if (!DIRENT_ISFILE(node->dtype))
  1087. {
  1088. ferr("ERROR: Path \"%s\" is not a regular file\n", relpath);
  1089. return -EISDIR;
  1090. }
  1091. /* Allocate a container to hold the task and node selection */
  1092. procfile = (FAR struct proc_file_s *)kmm_zalloc(sizeof(struct proc_file_s));
  1093. if (procfile == NULL)
  1094. {
  1095. ferr("ERROR: Failed to allocate file container\n");
  1096. return -ENOMEM;
  1097. }
  1098. /* Initialize the file container */
  1099. procfile->pid = pid;
  1100. procfile->node = node;
  1101. /* Save the index as the open-specific state in filep->f_priv */
  1102. filep->f_priv = (FAR void *)procfile;
  1103. return OK;
  1104. }
  1105. /****************************************************************************
  1106. * Name: proc_close
  1107. ****************************************************************************/
  1108. static int proc_close(FAR struct file *filep)
  1109. {
  1110. FAR struct proc_file_s *procfile;
  1111. /* Recover our private data from the struct file instance */
  1112. procfile = (FAR struct proc_file_s *)filep->f_priv;
  1113. DEBUGASSERT(procfile != NULL);
  1114. /* Release the file container structure */
  1115. kmm_free(procfile);
  1116. filep->f_priv = NULL;
  1117. return OK;
  1118. }
  1119. /****************************************************************************
  1120. * Name: proc_read
  1121. ****************************************************************************/
  1122. static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
  1123. size_t buflen)
  1124. {
  1125. FAR struct proc_file_s *procfile;
  1126. FAR struct tcb_s *tcb;
  1127. ssize_t ret;
  1128. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  1129. /* Recover our private data from the struct file instance */
  1130. procfile = (FAR struct proc_file_s *)filep->f_priv;
  1131. DEBUGASSERT(procfile != NULL);
  1132. /* Verify that the thread is still valid */
  1133. tcb = sched_gettcb(procfile->pid);
  1134. if (tcb == NULL)
  1135. {
  1136. ferr("ERROR: PID %d is not valid\n", (int)procfile->pid);
  1137. return -ENODEV;
  1138. }
  1139. /* Provide the requested data */
  1140. switch (procfile->node->node)
  1141. {
  1142. case PROC_STATUS: /* Task/thread status */
  1143. ret = proc_status(procfile, tcb, buffer, buflen, filep->f_pos);
  1144. break;
  1145. case PROC_CMDLINE: /* Task command line */
  1146. ret = proc_cmdline(procfile, tcb, buffer, buflen, filep->f_pos);
  1147. break;
  1148. #ifdef CONFIG_SCHED_CPULOAD
  1149. case PROC_LOADAVG: /* Average CPU utilization */
  1150. ret = proc_loadavg(procfile, tcb, buffer, buflen, filep->f_pos);
  1151. break;
  1152. #endif
  1153. #ifdef CONFIG_SCHED_CRITMONITOR
  1154. case PROC_CRITMON: /* Critical section monitor */
  1155. ret = proc_critmon(procfile, tcb, buffer, buflen, filep->f_pos);
  1156. break;
  1157. #endif
  1158. case PROC_STACK: /* Task stack info */
  1159. ret = proc_stack(procfile, tcb, buffer, buflen, filep->f_pos);
  1160. break;
  1161. case PROC_GROUP_STATUS: /* Task group status */
  1162. ret = proc_groupstatus(procfile, tcb, buffer, buflen, filep->f_pos);
  1163. break;
  1164. case PROC_GROUP_FD: /* Group file descriptors */
  1165. ret = proc_groupfd(procfile, tcb, buffer, buflen, filep->f_pos);
  1166. break;
  1167. #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
  1168. case PROC_GROUP_ENV: /* Group environment variables */
  1169. ret = proc_groupenv(procfile, tcb, buffer, buflen, filep->f_pos);
  1170. break;
  1171. #endif
  1172. default:
  1173. ret = -EINVAL;
  1174. break;
  1175. }
  1176. /* Update the file offset */
  1177. if (ret > 0)
  1178. {
  1179. filep->f_pos += ret;
  1180. }
  1181. return ret;
  1182. }
  1183. /****************************************************************************
  1184. * Name: proc_dup
  1185. *
  1186. * Description:
  1187. * Duplicate open file data in the new file structure.
  1188. *
  1189. ****************************************************************************/
  1190. static int proc_dup(FAR const struct file *oldp, FAR struct file *newp)
  1191. {
  1192. FAR struct proc_file_s *oldfile;
  1193. FAR struct proc_file_s *newfile;
  1194. finfo("Dup %p->%p\n", oldp, newp);
  1195. /* Recover our private data from the old struct file instance */
  1196. oldfile = (FAR struct proc_file_s *)oldp->f_priv;
  1197. DEBUGASSERT(oldfile != NULL);
  1198. /* Allocate a new container to hold the task and node selection */
  1199. newfile = (FAR struct proc_file_s *)kmm_malloc(sizeof(struct proc_file_s));
  1200. if (newfile == NULL)
  1201. {
  1202. ferr("ERROR: Failed to allocate file container\n");
  1203. return -ENOMEM;
  1204. }
  1205. /* The copy the file information from the old container to the new */
  1206. memcpy(newfile, oldfile, sizeof(struct proc_file_s));
  1207. /* Save the new container in the new file structure */
  1208. newp->f_priv = (FAR void *)newfile;
  1209. return OK;
  1210. }
  1211. /****************************************************************************
  1212. * Name: proc_opendir
  1213. *
  1214. * Description:
  1215. * Open a directory for read access
  1216. *
  1217. ****************************************************************************/
  1218. static int proc_opendir(FAR const char *relpath, FAR struct fs_dirent_s *dir)
  1219. {
  1220. FAR struct proc_dir_s *procdir;
  1221. FAR const struct proc_node_s *node;
  1222. FAR struct tcb_s *tcb;
  1223. unsigned long tmp;
  1224. FAR char *ptr;
  1225. pid_t pid;
  1226. finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
  1227. DEBUGASSERT(relpath != NULL && dir != NULL && dir->u.procfs == NULL);
  1228. /* The relative must be either:
  1229. *
  1230. * (1) "<pid>" - The sub-directory of task/thread attributes,
  1231. * (2) "self" - Which refers to the PID of the calling task, or
  1232. * (3) The name of a directory node under either of those
  1233. */
  1234. /* Otherwise, the relative path should be a valid task/thread ID */
  1235. ptr = NULL;
  1236. if (strncmp(relpath, "self", 4) == 0)
  1237. {
  1238. tmp = (unsigned long)getpid(); /* Get the PID of the calling task */
  1239. ptr = (FAR char *)relpath + 4; /* Discard const */
  1240. }
  1241. else
  1242. {
  1243. tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
  1244. }
  1245. if (ptr == NULL || (*ptr != '\0' && *ptr != '/'))
  1246. {
  1247. /* strtoul failed or there is something in the path after the pid */
  1248. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1249. return -ENOENT;
  1250. }
  1251. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  1252. * IDLE thread).
  1253. */
  1254. if (tmp >= 32768)
  1255. {
  1256. ferr("ERROR: Invalid PID %ld\n", tmp);
  1257. return -ENOENT;
  1258. }
  1259. /* Now verify that a task with this task/thread ID exists */
  1260. pid = (pid_t)tmp;
  1261. tcb = sched_gettcb(pid);
  1262. if (tcb == NULL)
  1263. {
  1264. ferr("ERROR: PID %d is not valid\n", (int)pid);
  1265. return -ENOENT;
  1266. }
  1267. /* Allocate the directory structure. Note that the index and procentry
  1268. * pointer are implicitly nullified by kmm_zalloc(). Only the remaining,
  1269. * non-zero entries will need be initialized.
  1270. */
  1271. procdir = (FAR struct proc_dir_s *)kmm_zalloc(sizeof(struct proc_dir_s));
  1272. if (procdir == NULL)
  1273. {
  1274. ferr("ERROR: Failed to allocate the directory structure\n");
  1275. return -ENOMEM;
  1276. }
  1277. /* Was the <pid> the final element of the path? */
  1278. if (*ptr != '\0' && strcmp(ptr, "/") != 0)
  1279. {
  1280. /* There is something in the path after the pid. Skip over the path
  1281. * segment delimiter and see if we can identify the node of interest.
  1282. */
  1283. ptr++;
  1284. node = proc_findnode(ptr);
  1285. if (node == NULL)
  1286. {
  1287. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1288. kmm_free(procdir);
  1289. return -ENOENT;
  1290. }
  1291. /* The node must be a directory, not a file */
  1292. if (!DIRENT_ISDIRECTORY(node->dtype))
  1293. {
  1294. ferr("ERROR: Path \"%s\" is not a directory\n", relpath);
  1295. kmm_free(procdir);
  1296. return -ENOTDIR;
  1297. }
  1298. /* This is a second level directory */
  1299. procdir->base.level = 2;
  1300. procdir->base.nentries = PROC_NGROUPNODES;
  1301. procdir->node = node;
  1302. }
  1303. else
  1304. {
  1305. /* Use the special level0 node */
  1306. procdir->base.level = 1;
  1307. procdir->base.nentries = PROC_NLEVEL0NODES;
  1308. procdir->node = &g_level0node;
  1309. }
  1310. procdir->pid = pid;
  1311. dir->u.procfs = (FAR void *)procdir;
  1312. return OK;
  1313. }
  1314. /****************************************************************************
  1315. * Name: proc_closedir
  1316. *
  1317. * Description: Close the directory listing
  1318. *
  1319. ****************************************************************************/
  1320. static int proc_closedir(FAR struct fs_dirent_s *dir)
  1321. {
  1322. FAR struct proc_dir_s *priv;
  1323. DEBUGASSERT(dir != NULL && dir->u.procfs != NULL);
  1324. priv = dir->u.procfs;
  1325. if (priv)
  1326. {
  1327. kmm_free(priv);
  1328. }
  1329. dir->u.procfs = NULL;
  1330. return OK;
  1331. }
  1332. /****************************************************************************
  1333. * Name: proc_readdir
  1334. *
  1335. * Description: Read the next directory entry
  1336. *
  1337. ****************************************************************************/
  1338. static int proc_readdir(struct fs_dirent_s *dir)
  1339. {
  1340. FAR struct proc_dir_s *procdir;
  1341. FAR const struct proc_node_s *node = NULL;
  1342. FAR struct tcb_s *tcb;
  1343. unsigned int index;
  1344. pid_t pid;
  1345. int ret;
  1346. DEBUGASSERT(dir != NULL && dir->u.procfs != NULL);
  1347. procdir = dir->u.procfs;
  1348. /* Have we reached the end of the directory */
  1349. index = procdir->base.index;
  1350. if (index >= procdir->base.nentries)
  1351. {
  1352. /* We signal the end of the directory by returning the special
  1353. * error -ENOENT
  1354. */
  1355. finfo("Entry %d: End of directory\n", index);
  1356. ret = -ENOENT;
  1357. }
  1358. /* No, we are not at the end of the directory */
  1359. else
  1360. {
  1361. /* Verify that the pid still refers to an active task/thread */
  1362. pid = procdir->pid;
  1363. tcb = sched_gettcb(pid);
  1364. if (tcb == NULL)
  1365. {
  1366. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  1367. return -ENOENT;
  1368. }
  1369. /* The TCB is still valid (or at least was when we entered this
  1370. * function)
  1371. *
  1372. * Handle the directory listing by the node type.
  1373. */
  1374. switch (procdir->node->node)
  1375. {
  1376. case PROC_LEVEL0: /* Top level directory */
  1377. DEBUGASSERT(procdir->base.level == 1);
  1378. node = g_level0info[index];
  1379. break;
  1380. case PROC_GROUP: /* Group sub-directory */
  1381. DEBUGASSERT(procdir->base.level == 2);
  1382. node = g_groupinfo[index];
  1383. break;
  1384. default:
  1385. return -ENOENT;
  1386. }
  1387. /* Save the filename and file type */
  1388. dir->fd_dir.d_type = node->dtype;
  1389. strncpy(dir->fd_dir.d_name, node->name, NAME_MAX + 1);
  1390. /* Set up the next directory entry offset. NOTE that we could use the
  1391. * standard f_pos instead of our own private index.
  1392. */
  1393. procdir->base.index = index + 1;
  1394. ret = OK;
  1395. }
  1396. return ret;
  1397. }
  1398. /****************************************************************************
  1399. * Name: proc_rewindir
  1400. *
  1401. * Description: Reset directory read to the first entry
  1402. *
  1403. ****************************************************************************/
  1404. static int proc_rewinddir(struct fs_dirent_s *dir)
  1405. {
  1406. FAR struct proc_dir_s *priv;
  1407. DEBUGASSERT(dir != NULL && dir->u.procfs != NULL);
  1408. priv = dir->u.procfs;
  1409. priv->base.index = 0;
  1410. return OK;
  1411. }
  1412. /****************************************************************************
  1413. * Name: proc_stat
  1414. *
  1415. * Description: Return information about a file or directory
  1416. *
  1417. ****************************************************************************/
  1418. static int proc_stat(const char *relpath, struct stat *buf)
  1419. {
  1420. FAR const struct proc_node_s *node;
  1421. FAR struct tcb_s *tcb;
  1422. unsigned long tmp;
  1423. FAR char *ptr;
  1424. pid_t pid;
  1425. /* Two path forms are accepted:
  1426. *
  1427. * "<pid>" - If <pid> refers to a currently active task/thread, then it
  1428. * is a directory
  1429. * "<pid>/<node>" - If <node> is a recognized node then, then it
  1430. * is a file or directory.
  1431. */
  1432. ptr = NULL;
  1433. if (strncmp(relpath, "self", 4) == 0)
  1434. {
  1435. tmp = (unsigned long)getpid(); /* Get the PID of the calling task */
  1436. ptr = (FAR char *)relpath + 4; /* Discard const */
  1437. }
  1438. else
  1439. {
  1440. tmp = strtoul(relpath, &ptr, 10); /* Extract the PID from path */
  1441. }
  1442. if (ptr == NULL)
  1443. {
  1444. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1445. return -ENOENT;
  1446. }
  1447. /* A valid PID would be in the range of 0-32767 (0 is reserved for the
  1448. * IDLE thread).
  1449. */
  1450. if (tmp >= 32768)
  1451. {
  1452. ferr("ERROR: Invalid PID %ld\n", tmp);
  1453. return -ENOENT;
  1454. }
  1455. /* Now verify that a task with this task/thread ID exists */
  1456. pid = (pid_t)tmp;
  1457. tcb = sched_gettcb(pid);
  1458. if (tcb == NULL)
  1459. {
  1460. ferr("ERROR: PID %d is no longer valid\n", (int)pid);
  1461. return -ENOENT;
  1462. }
  1463. /* Was the <pid> the final element of the path? */
  1464. memset(buf, 0, sizeof(struct stat));
  1465. if (*ptr == '\0' || strcmp(ptr, "/") == 0)
  1466. {
  1467. /* Yes ... It's a read-only directory */
  1468. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
  1469. }
  1470. /* Verify that the process ID is followed by valid path segment delimiter */
  1471. else if (*ptr != '/')
  1472. {
  1473. /* We are required to return -ENOENT all all invalid paths */
  1474. ferr("ERROR: Bad delimiter '%c' in relpath '%s'\n", *ptr, relpath);
  1475. return -ENOENT;
  1476. }
  1477. else
  1478. {
  1479. /* Otherwise, the second segment of the relpath should be a well
  1480. * known node of the task/thread directory structure.
  1481. */
  1482. /* Skip over the path segment delimiter */
  1483. ptr++;
  1484. /* Lookup the well-known node associated with the relative path. */
  1485. node = proc_findnode(ptr);
  1486. if (node == NULL)
  1487. {
  1488. ferr("ERROR: Invalid path \"%s\"\n", relpath);
  1489. return -ENOENT;
  1490. }
  1491. /* If the node exists, it is the name for a read-only file or
  1492. * directory.
  1493. */
  1494. if (node->dtype == DTYPE_FILE)
  1495. {
  1496. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  1497. }
  1498. else
  1499. {
  1500. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
  1501. }
  1502. }
  1503. return OK;
  1504. }
  1505. /****************************************************************************
  1506. * Public Functions
  1507. ****************************************************************************/
  1508. #endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
  1509. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */