2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/procfs/fs_procfs.c
|
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-11-13 22:59:14 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-11-13 22:59:14 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-11-13 22:59:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2021-08-01 10:54:08 +02:00
|
|
|
#include <fnmatch.h>
|
2013-11-13 22:59:14 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:43:47 +01:00
|
|
|
#include <nuttx/irq.h>
|
2013-11-13 22:59:14 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2013-12-12 16:21:55 +01:00
|
|
|
#include <nuttx/fs/procfs.h>
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2017-10-26 21:33:08 +02:00
|
|
|
#include "mount/mount.h"
|
2024-03-05 03:44:31 +01:00
|
|
|
#include "sched/sched.h"
|
2024-09-09 14:28:16 +02:00
|
|
|
#include "fs_heap.h"
|
2017-10-26 21:33:08 +02:00
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
2018-11-24 20:33:37 +01:00
|
|
|
* External Definitions
|
2013-11-13 22:59:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-29 04:59:04 +02:00
|
|
|
extern const struct procfs_operations g_clk_operations;
|
2023-03-15 13:48:40 +01:00
|
|
|
extern const struct procfs_operations g_cpuinfo_operations;
|
2023-04-17 20:23:18 +02:00
|
|
|
extern const struct procfs_operations g_cpuload_operations;
|
|
|
|
extern const struct procfs_operations g_critmon_operations;
|
2023-05-07 09:13:31 +02:00
|
|
|
extern const struct procfs_operations g_fdt_operations;
|
2023-04-17 20:33:48 +02:00
|
|
|
extern const struct procfs_operations g_iobinfo_operations;
|
|
|
|
extern const struct procfs_operations g_irq_operations;
|
2023-04-17 20:23:18 +02:00
|
|
|
extern const struct procfs_operations g_meminfo_operations;
|
|
|
|
extern const struct procfs_operations g_memdump_operations;
|
|
|
|
extern const struct procfs_operations g_mempool_operations;
|
|
|
|
extern const struct procfs_operations g_module_operations;
|
2023-04-17 20:33:48 +02:00
|
|
|
extern const struct procfs_operations g_pm_operations;
|
|
|
|
extern const struct procfs_operations g_proc_operations;
|
|
|
|
extern const struct procfs_operations g_tcbinfo_operations;
|
2023-11-07 13:10:19 +01:00
|
|
|
extern const struct procfs_operations g_thermal_operations;
|
2023-04-17 20:23:18 +02:00
|
|
|
extern const struct procfs_operations g_uptime_operations;
|
|
|
|
extern const struct procfs_operations g_version_operations;
|
procfs: add memory pressure notification support
This is a memory monitoring interface implemented with reference to Linux's PSI (Pressure Stall Information),
which can send notifications when the system's remaining memory is below the threshold.
The following example code sets two different thresholds.
When the system memory is below 10MB, a notification is triggered.
When the system memory is below 20 MB, a notification (POLLPRI event) is triggered every 1s.
```
int main(int argc, FAR char *argv[])
{
struct pollfd fds[2];
int ret;
if (argc == 2)
{
char *ptr = malloc(1024*1024*atoi(argv[1]));
printf("Allocating %d MB\n", atoi(argv[1]));
ptr[0] = 0;
return 0;
}
fds[0].fd = open("/proc/pressure/memory", O_RDWR);
fds[1].fd = open("/proc/pressure/memory", O_RDWR);
fds[0].events = POLLPRI;
fds[1].events = POLLPRI;
dprintf(fds[0].fd, "%llu -1", 1024LLU*1024 * 10);
dprintf(fds[1].fd, "%llu 1000000", 1024LLU*1024 * 20);
while (1)
{
ret = poll(fds, 2, -1);
if (ret > 0)
{
printf("Memory pressure: POLLPRI, %d\n", ret);
}
}
return 0;
}
```
https://docs.kernel.org/accounting/psi.html
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-11-29 14:59:49 +01:00
|
|
|
extern const struct procfs_operations g_pressure_operations;
|
2014-07-03 16:50:24 +02:00
|
|
|
|
2015-11-27 19:33:58 +01:00
|
|
|
/* This is not good. These are implemented in other sub-systems. Having to
|
|
|
|
* deal with them here is not a good coupling. What is really needed is a
|
|
|
|
* run-time procfs registration system vs. a build time, fixed procfs
|
|
|
|
* configuration.
|
2014-07-03 16:50:24 +02:00
|
|
|
*/
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
extern const struct procfs_operations g_mount_operations;
|
2023-04-17 20:27:33 +02:00
|
|
|
extern const struct procfs_operations g_net_operations;
|
|
|
|
extern const struct procfs_operations g_netroute_operations;
|
|
|
|
extern const struct procfs_operations g_part_operations;
|
2024-09-09 22:07:49 +02:00
|
|
|
extern const struct procfs_operations g_smartfs_procfs_operations;
|
2014-07-03 16:50:24 +02:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2019-03-01 22:00:00 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Table of all known / pre-registered procfs handlers / participants. */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
|
|
|
static const struct procfs_entry_s g_base_entries[] =
|
|
|
|
#else
|
|
|
|
static const struct procfs_entry_s g_procfs_entries[] =
|
|
|
|
#endif
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "[0-9]*/**", &g_proc_operations, PROCFS_UNKOWN_TYPE },
|
|
|
|
{ "[0-9]*", &g_proc_operations, PROCFS_DIR_TYPE },
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2023-08-29 04:59:04 +02:00
|
|
|
#if defined(CONFIG_CLK) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CLK)
|
|
|
|
{ "clk", &g_clk_operations, PROCFS_FILE_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2023-03-15 13:48:40 +01:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_CPUINFO) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CPUINFO)
|
|
|
|
{ "cpuinfo", &g_cpuinfo_operations, PROCFS_FILE_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2023-10-26 15:03:16 +02:00
|
|
|
#if !defined(CONFIG_SCHED_CPULOAD_NONE) && \
|
|
|
|
!defined(CONFIG_FS_PROCFS_EXCLUDE_CPULOAD)
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "cpuload", &g_cpuload_operations, PROCFS_FILE_TYPE },
|
2014-02-22 22:20:12 +01:00
|
|
|
#endif
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
#ifdef CONFIG_SCHED_CRITMONITOR
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "critmon", &g_critmon_operations, PROCFS_FILE_TYPE },
|
2018-11-24 20:33:37 +01:00
|
|
|
#endif
|
|
|
|
|
2023-05-07 09:13:31 +02:00
|
|
|
#if defined(CONFIG_DEVICE_TREE) && !defined(CONFIG_FS_PROCFS_EXCLUDE_FDT)
|
|
|
|
{ "fdt", &g_fdt_operations, PROCFS_FILE_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_BLOCKS
|
|
|
|
{ "fs/blocks", &g_mount_operations, PROCFS_FILE_TYPE },
|
2018-01-13 00:41:09 +01:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MOUNT
|
|
|
|
{ "fs/mount", &g_mount_operations, PROCFS_FILE_TYPE },
|
2016-02-06 18:35:30 +01:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#if defined(CONFIG_FS_SMARTFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_SMARTFS)
|
2024-09-09 22:07:49 +02:00
|
|
|
{ "fs/smartfs**", &g_smartfs_procfs_operations, PROCFS_UNKOWN_TYPE },
|
2022-07-29 06:17:14 +02:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_USAGE
|
|
|
|
{ "fs/usage", &g_mount_operations, PROCFS_FILE_TYPE },
|
2019-08-17 00:42:25 +02:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#if defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
|
|
|
|
{ "iobinfo", &g_iobinfo_operations, PROCFS_FILE_TYPE },
|
2015-12-13 00:42:25 +01:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#ifdef CONFIG_SCHED_IRQMONITOR
|
|
|
|
{ "irqs", &g_irq_operations, PROCFS_FILE_TYPE },
|
2017-10-27 14:06:20 +02:00
|
|
|
#endif
|
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMINFO
|
|
|
|
# ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP
|
|
|
|
{ "memdump", &g_memdump_operations, PROCFS_FILE_TYPE },
|
|
|
|
# endif
|
|
|
|
{ "meminfo", &g_meminfo_operations, PROCFS_FILE_TYPE },
|
2017-10-27 14:06:20 +02:00
|
|
|
#endif
|
|
|
|
|
2024-07-31 05:06:28 +02:00
|
|
|
#if defined(CONFIG_MM_HEAP_MEMPOOL) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
|
2023-04-17 20:33:48 +02:00
|
|
|
{ "mempool", &g_mempool_operations, PROCFS_FILE_TYPE },
|
2017-10-27 14:06:20 +02:00
|
|
|
#endif
|
2017-10-26 21:33:08 +02:00
|
|
|
|
2023-04-17 20:33:48 +02:00
|
|
|
#if defined(CONFIG_MODULE) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
|
|
|
{ "modules", &g_module_operations, PROCFS_FILE_TYPE },
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2015-11-27 19:33:58 +01:00
|
|
|
#if defined(CONFIG_NET) && !defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "net", &g_net_operations, PROCFS_DIR_TYPE },
|
2022-10-17 13:26:22 +02:00
|
|
|
# if defined(CONFIG_NET_ROUTE) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ROUTE)
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "net/route", &g_netroute_operations, PROCFS_DIR_TYPE },
|
|
|
|
{ "net/route/**", &g_netroute_operations, PROCFS_UNKOWN_TYPE },
|
2022-10-17 13:26:22 +02:00
|
|
|
# endif
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "net/**", &g_net_operations, PROCFS_UNKOWN_TYPE },
|
2015-11-27 19:33:58 +01:00
|
|
|
#endif
|
|
|
|
|
2015-05-24 01:08:35 +02:00
|
|
|
#if defined(CONFIG_MTD_PARTITION) && !defined(CONFIG_FS_PROCFS_EXCLUDE_PARTITIONS)
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "partitions", &g_part_operations, PROCFS_FILE_TYPE },
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2022-06-20 12:16:42 +02:00
|
|
|
#if defined(CONFIG_PM) && defined(CONFIG_PM_PROCFS)
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "pm", &g_pm_operations, PROCFS_DIR_TYPE },
|
|
|
|
{ "pm/**", &g_pm_operations, PROCFS_UNKOWN_TYPE },
|
2022-06-20 12:16:42 +02:00
|
|
|
#endif
|
|
|
|
|
procfs: add memory pressure notification support
This is a memory monitoring interface implemented with reference to Linux's PSI (Pressure Stall Information),
which can send notifications when the system's remaining memory is below the threshold.
The following example code sets two different thresholds.
When the system memory is below 10MB, a notification is triggered.
When the system memory is below 20 MB, a notification (POLLPRI event) is triggered every 1s.
```
int main(int argc, FAR char *argv[])
{
struct pollfd fds[2];
int ret;
if (argc == 2)
{
char *ptr = malloc(1024*1024*atoi(argv[1]));
printf("Allocating %d MB\n", atoi(argv[1]));
ptr[0] = 0;
return 0;
}
fds[0].fd = open("/proc/pressure/memory", O_RDWR);
fds[1].fd = open("/proc/pressure/memory", O_RDWR);
fds[0].events = POLLPRI;
fds[1].events = POLLPRI;
dprintf(fds[0].fd, "%llu -1", 1024LLU*1024 * 10);
dprintf(fds[1].fd, "%llu 1000000", 1024LLU*1024 * 20);
while (1)
{
ret = poll(fds, 2, -1);
if (ret > 0)
{
printf("Memory pressure: POLLPRI, %d\n", ret);
}
}
return 0;
}
```
https://docs.kernel.org/accounting/psi.html
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-11-29 14:59:49 +01:00
|
|
|
#ifdef CONFIG_FS_PROCFS_INCLUDE_PRESSURE
|
|
|
|
{ "pressure", &g_pressure_operations, PROCFS_DIR_TYPE },
|
|
|
|
{ "pressure/**", &g_pressure_operations, PROCFS_FILE_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 02:00:23 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "self", &g_proc_operations, PROCFS_DIR_TYPE },
|
|
|
|
{ "self/**", &g_proc_operations, PROCFS_UNKOWN_TYPE },
|
2018-08-19 02:00:23 +02:00
|
|
|
#endif
|
|
|
|
|
2023-07-19 23:28:37 +02:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_TCBINFO) && !defined(CONFIG_FS_PROCFS_EXCLUDE_TCBINFO)
|
2023-04-17 20:33:48 +02:00
|
|
|
{ "tcbinfo", &g_tcbinfo_operations, PROCFS_FILE_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2023-11-07 13:10:19 +01:00
|
|
|
#ifdef CONFIG_THERMAL_PROCFS
|
|
|
|
{ "thermal", &g_thermal_operations, PROCFS_DIR_TYPE },
|
|
|
|
{ "thermal/**", &g_thermal_operations, PROCFS_UNKOWN_TYPE },
|
|
|
|
#endif
|
|
|
|
|
2023-07-19 23:28:37 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_UPTIME
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "uptime", &g_uptime_operations, PROCFS_FILE_TYPE },
|
2013-12-14 17:34:08 +01:00
|
|
|
#endif
|
2018-11-08 17:13:45 +01:00
|
|
|
|
2023-07-19 23:28:37 +02:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_VERSION
|
2023-04-17 20:27:33 +02:00
|
|
|
{ "version", &g_version_operations, PROCFS_FILE_TYPE },
|
2018-11-08 17:13:45 +01:00
|
|
|
#endif
|
2013-11-13 22:59:14 +01:00
|
|
|
};
|
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
|
|
|
static const uint8_t g_base_entrycount = sizeof(g_base_entries) /
|
2019-05-25 22:12:00 +02:00
|
|
|
sizeof(struct procfs_entry_s);
|
2015-12-01 21:56:20 +01:00
|
|
|
|
|
|
|
static FAR struct procfs_entry_s *g_procfs_entries;
|
|
|
|
static uint8_t g_procfs_entrycount;
|
|
|
|
#else
|
|
|
|
static const uint8_t g_procfs_entrycount = sizeof(g_procfs_entries) /
|
2019-05-25 22:12:00 +02:00
|
|
|
sizeof(struct procfs_entry_s);
|
2015-12-01 21:56:20 +01:00
|
|
|
#endif
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-03-01 22:00:00 +01:00
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/* File system methods */
|
|
|
|
|
|
|
|
static int procfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int procfs_close(FAR struct file *filep);
|
|
|
|
static ssize_t procfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2014-12-09 20:24:38 +01:00
|
|
|
static ssize_t procfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2023-11-25 13:11:03 +01:00
|
|
|
static int procfs_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
2013-11-13 22:59:14 +01:00
|
|
|
static int procfs_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
static int procfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int procfs_fstat(FAR const struct file *filep,
|
|
|
|
FAR struct stat *buf);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_opendir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath, FAR struct fs_dirent_s **dir);
|
2013-11-13 22:59:14 +01:00
|
|
|
static int procfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int procfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir, FAR struct dirent *entry);
|
2013-11-13 22:59:14 +01:00
|
|
|
static int procfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
|
|
|
|
static int procfs_bind(FAR struct inode *blkdriver,
|
|
|
|
FAR const void *data, FAR void **handle);
|
2015-03-15 00:22:02 +01:00
|
|
|
static int procfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags);
|
2013-11-13 22:59:14 +01:00
|
|
|
static int procfs_statfs(FAR struct inode *mountpt,
|
|
|
|
FAR struct statfs *buf);
|
|
|
|
|
|
|
|
static int procfs_stat(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
2019-05-25 22:12:00 +02:00
|
|
|
static int procfs_initialize(void);
|
2015-12-01 21:56:20 +01:00
|
|
|
#endif
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2013-11-13 22:59:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 16:53:42 +02:00
|
|
|
const struct mountpt_operations g_procfs_operations =
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
|
|
|
procfs_open, /* open */
|
|
|
|
procfs_close, /* close */
|
|
|
|
procfs_read, /* read */
|
2014-12-09 20:24:38 +01:00
|
|
|
procfs_write, /* write */
|
2013-11-13 22:59:14 +01:00
|
|
|
NULL, /* seek */
|
|
|
|
procfs_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2023-11-25 13:11:03 +01:00
|
|
|
procfs_poll, /* poll */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
NULL, /* sync */
|
|
|
|
procfs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
procfs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
procfs_opendir, /* opendir */
|
|
|
|
procfs_closedir, /* closedir */
|
|
|
|
procfs_readdir, /* readdir */
|
|
|
|
procfs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
procfs_bind, /* bind */
|
|
|
|
procfs_unbind, /* unbind */
|
|
|
|
procfs_statfs, /* statfs */
|
|
|
|
|
|
|
|
NULL, /* unlink */
|
|
|
|
NULL, /* mkdir */
|
|
|
|
NULL, /* rmdir */
|
|
|
|
NULL, /* rename */
|
2021-07-09 07:43:26 +02:00
|
|
|
procfs_stat, /* stat */
|
|
|
|
NULL /* chstat */
|
2013-11-13 22:59:14 +01:00
|
|
|
};
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Level 0 contains the directory of active tasks in addition to other
|
2020-02-23 09:50:23 +01:00
|
|
|
* statically registered entries with custom handlers. This structure
|
2013-12-12 16:21:55 +01:00
|
|
|
* contains a snapshot of the active tasks when the directory is first
|
|
|
|
* opened.
|
2013-11-13 22:59:14 +01:00
|
|
|
*/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
struct procfs_level0_s
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2022-10-17 13:26:22 +02:00
|
|
|
struct procfs_dir_priv_s base; /* Base struct for ProcFS dir */
|
2014-09-22 19:07:08 +02:00
|
|
|
|
|
|
|
/* Our private data */
|
|
|
|
|
2021-06-15 10:45:40 +02:00
|
|
|
uint8_t lastlen; /* length of last reported static dir */
|
|
|
|
FAR const char *lastread; /* Pointer to last static dir read */
|
2024-03-05 03:44:31 +01:00
|
|
|
pid_t pid[1]; /* Snapshot of all active task IDs */
|
2013-11-13 22:59:14 +01:00
|
|
|
};
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Level 1 is an internal virtual directory (such as /proc/fs) which
|
|
|
|
* will contain one or more additional static entries based on the
|
|
|
|
* configuration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct procfs_level1_s
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2022-10-17 13:26:22 +02:00
|
|
|
struct procfs_dir_priv_s base; /* Base struct for ProcFS dir */
|
2014-09-22 19:07:08 +02:00
|
|
|
|
|
|
|
/* Our private data */
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
uint8_t lastlen; /* length of last reported static dir */
|
|
|
|
uint8_t subdirlen; /* Length of the subdir search */
|
|
|
|
uint16_t firstindex; /* Index of 1st entry matching this subdir */
|
|
|
|
FAR const char *lastread; /* Pointer to last static dir read */
|
2013-11-13 22:59:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_enum
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void procfs_enum(FAR struct tcb_s *tcb, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct procfs_level0_s *dir = (FAR struct procfs_level0_s *)arg;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
|
|
|
|
/* Add the PID to the list */
|
|
|
|
|
2024-03-05 03:44:31 +01:00
|
|
|
if (dir->base.index >= dir->base.nentries)
|
2021-06-15 10:45:40 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2024-03-05 03:44:31 +01:00
|
|
|
index = dir->base.index;
|
2013-11-13 22:59:14 +01:00
|
|
|
dir->pid[index] = tcb->pid;
|
2024-03-05 03:44:31 +01:00
|
|
|
dir->base.index = index + 1;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
2023-01-29 10:58:17 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_sort_pid
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void procfs_sort_pid(FAR struct procfs_level0_s *level0)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Sort the process id by Bubble.
|
|
|
|
* FIXME: improve searching algorithm.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < level0->base.nentries; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < level0->base.nentries - 1 - i; j++)
|
|
|
|
{
|
|
|
|
if (level0->pid[j] > level0->pid[j + 1])
|
|
|
|
{
|
|
|
|
pid = level0->pid[j];
|
|
|
|
level0->pid[j] = level0->pid[j + 1];
|
|
|
|
level0->pid[j + 1] = pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_open(FAR struct file *filep, FAR const char *relpath,
|
2022-10-17 13:26:22 +02:00
|
|
|
int oflags, mode_t mode)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
int x;
|
|
|
|
int ret = -ENOENT;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Perform the stat based on the procfs_entry operations */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
for (x = 0; x < g_procfs_entrycount; x++)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test if the path matches this entry's specification */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2021-08-01 10:54:08 +02:00
|
|
|
if (fnmatch(g_procfs_entries[x].pathpattern, relpath, 0) == 0)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
/* Match found! Stat using this procfs entry */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
DEBUGASSERT(g_procfs_entries[x].ops &&
|
2022-10-17 13:26:22 +02:00
|
|
|
g_procfs_entries[x].ops->open);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
ret = g_procfs_entries[x].ops->open(filep, relpath, oflags, mode);
|
2013-12-12 16:21:55 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(filep->f_priv);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
((FAR struct procfs_file_s *)filep->f_priv)->procfsentry =
|
2015-12-01 21:56:20 +01:00
|
|
|
&g_procfs_entries[x];
|
2017-08-11 22:50:39 +02:00
|
|
|
break;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
return ret;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct procfs_file_s *attr;
|
2023-06-05 12:09:04 +02:00
|
|
|
int ret = OK;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
attr = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(attr);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
2023-06-05 12:09:04 +02:00
|
|
|
if (attr->procfsentry->ops->close != NULL)
|
|
|
|
{
|
|
|
|
ret = attr->procfsentry->ops->close(filep);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(attr);
|
2023-06-05 12:09:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
filep->f_priv = NULL;
|
2023-06-05 12:09:04 +02:00
|
|
|
return ret;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t procfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_file_s *handler;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
handler = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(handler);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Call the handler's read routine */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
return handler->procfsentry->ops->read(filep, buffer, buflen);
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 20:24:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t procfs_write(FAR struct file *filep, FAR const char *buffer,
|
2022-10-17 13:26:22 +02:00
|
|
|
size_t buflen)
|
2014-12-09 20:24:38 +01:00
|
|
|
{
|
|
|
|
FAR struct procfs_file_s *handler;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
2014-12-09 20:24:38 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
handler = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(handler);
|
|
|
|
|
2023-09-20 05:27:12 +02:00
|
|
|
/* Call the handler's write routine */
|
2014-12-09 20:24:38 +01:00
|
|
|
|
|
|
|
if (handler->procfsentry->ops->write)
|
|
|
|
{
|
2022-10-17 13:26:22 +02:00
|
|
|
return handler->procfsentry->ops->write(filep, buffer, buflen);
|
2014-12-09 20:24:38 +01:00
|
|
|
}
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
return 0;
|
2014-12-09 20:24:38 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 13:11:03 +01:00
|
|
|
static int procfs_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct procfs_file_s *handler;
|
|
|
|
|
|
|
|
finfo("fds=%p setup=%d\n", fds, setup);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
handler = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(handler);
|
|
|
|
|
|
|
|
/* Call the handler's poll routine */
|
|
|
|
|
|
|
|
if (handler->procfsentry->ops->poll)
|
|
|
|
{
|
|
|
|
return handler->procfsentry->ops->poll(filep, fds, setup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("cmd: %d arg: %08lx\n", cmd, arg);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* No IOCTL commands supported */
|
|
|
|
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct procfs_file_s *oldattr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldattr = (FAR struct procfs_file_s *)oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldattr);
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Allow lower-level handler do the dup to get it's extra data */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
return oldattr->procfsentry->ops->dup(oldp, newp);
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_fstat
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain information about an open file associated with the file
|
|
|
|
* descriptor 'fd', and will write it to the area pointed to by 'buf'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-15 16:32:42 +01:00
|
|
|
FAR struct procfs_file_s *handler;
|
|
|
|
|
|
|
|
finfo("buf=%p\n", buf);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
handler = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(handler);
|
|
|
|
|
|
|
|
/* The procfs file system contains only directory and data file entries.
|
|
|
|
* Since the file has been opened, we know that this is a data file and,
|
|
|
|
* at a minimum, readable.
|
2017-02-14 17:00:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
2017-02-15 16:32:42 +01:00
|
|
|
|
|
|
|
/* If the write method is provided, then let's also claim that the file is
|
|
|
|
* writable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (handler->procfsentry->ops->write != NULL)
|
|
|
|
{
|
|
|
|
buf->st_mode |= S_IWOTH | S_IWGRP | S_IWUSR;
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:00:15 +01:00
|
|
|
return OK;
|
2017-02-12 20:42:27 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_opendir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_level0_s *level0;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(mountpt && dir && relpath);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* The relative must be either:
|
|
|
|
*
|
|
|
|
* "" - The top level directory of task/thread IDs
|
|
|
|
* "<pid>" - The sub-directory of task/thread attributes
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!relpath || relpath[0] == '\0')
|
|
|
|
{
|
2024-03-05 03:44:31 +01:00
|
|
|
size_t num = 0;
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/* The path refers to the top level directory. Allocate the level0
|
|
|
|
* dirent structure.
|
|
|
|
*/
|
|
|
|
|
2024-03-05 03:44:31 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
|
|
|
num = g_npidhash;
|
|
|
|
#endif
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
level0 = (FAR struct procfs_level0_s *)
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_zalloc(sizeof(struct procfs_level0_s) + sizeof(pid_t) * num);
|
2013-11-13 22:59:14 +01:00
|
|
|
if (!level0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate the level0 directory structure\n");
|
2013-11-13 22:59:14 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take a snapshot of all currently active tasks. Any new tasks
|
|
|
|
* added between the opendir() and closedir() call will not be
|
|
|
|
* visible.
|
|
|
|
*
|
|
|
|
* NOTE that interrupts must be disabled throughout the traversal.
|
|
|
|
*/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
2024-03-05 03:44:31 +01:00
|
|
|
level0->base.index = 0;
|
|
|
|
level0->base.nentries = num;
|
2020-05-09 16:04:45 +02:00
|
|
|
nxsched_foreach(procfs_enum, level0);
|
2024-03-05 03:44:31 +01:00
|
|
|
level0->base.nentries = level0->base.index;
|
|
|
|
level0->base.index = 0;
|
2023-01-29 10:58:17 +01:00
|
|
|
procfs_sort_pid(level0);
|
2013-12-12 16:21:55 +01:00
|
|
|
#else
|
2014-09-22 19:07:08 +02:00
|
|
|
level0->base.index = 0;
|
|
|
|
level0->base.nentries = 0;
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-27 19:33:58 +01:00
|
|
|
/* Initialize lastread entries */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
level0->lastread = "";
|
|
|
|
level0->lastlen = 0;
|
2014-09-22 19:07:08 +02:00
|
|
|
level0->base.procfsentry = NULL;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = (FAR struct fs_dirent_s *)level0;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-29 13:17:34 +01:00
|
|
|
int x;
|
|
|
|
int ret;
|
2013-12-12 16:21:55 +01:00
|
|
|
int len = strlen(relpath);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Search the static array of procfs_entries */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
for (x = 0; x < g_procfs_entrycount; x++)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test if the path matches this entry's specification */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2021-08-01 10:54:08 +02:00
|
|
|
if (fnmatch(g_procfs_entries[x].pathpattern, relpath, 0) == 0)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
2019-05-25 22:12:00 +02:00
|
|
|
/* Match found! Call the handler's opendir routine. If
|
|
|
|
* successful, this opendir routine will create an entry
|
2022-08-05 06:31:25 +02:00
|
|
|
* derived from struct procfs_dir_priv_s as dir.
|
2013-12-12 18:08:20 +01:00
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
DEBUGASSERT(g_procfs_entries[x].ops != NULL &&
|
|
|
|
g_procfs_entries[x].ops->opendir != NULL);
|
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
ret = g_procfs_entries[x].ops->opendir(relpath, dir);
|
2013-12-12 16:21:55 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct procfs_dir_priv_s *dirpriv;
|
|
|
|
|
|
|
|
DEBUGASSERT(*dir);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Set the procfs_entry handler */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
dirpriv = (FAR struct procfs_dir_priv_s *)(*dir);
|
2015-12-01 21:56:20 +01:00
|
|
|
dirpriv->procfsentry = &g_procfs_entries[x];
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Test for a sub-string match (e.g. "ls /proc/fs") */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
else if (strncmp(g_procfs_entries[x].pathpattern, relpath,
|
|
|
|
len) == 0)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
FAR struct procfs_level1_s *level1;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Doing an intermediate directory search */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
/* The path refers to the top level directory. Allocate
|
|
|
|
* the level1 dirent structure.
|
2013-12-12 16:21:55 +01:00
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
level1 = (FAR struct procfs_level1_s *)
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_zalloc(sizeof(struct procfs_level1_s));
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!level1)
|
|
|
|
{
|
2019-05-25 22:12:00 +02:00
|
|
|
ferr("ERROR: Failed to allocate the level0 directory "
|
|
|
|
"structure\n");
|
2013-12-12 16:21:55 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
level1->base.level = 1;
|
|
|
|
level1->base.index = x;
|
2013-12-12 16:21:55 +01:00
|
|
|
level1->firstindex = x;
|
|
|
|
level1->subdirlen = len;
|
|
|
|
level1->lastread = "";
|
|
|
|
level1->lastlen = 0;
|
2014-09-22 19:07:08 +02:00
|
|
|
level1->base.procfsentry = NULL;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = (FAR struct fs_dirent_s *)level1;
|
2013-12-12 16:21:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 14:16:29 +02:00
|
|
|
|
|
|
|
if (x == g_procfs_entrycount)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close the directory listing
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(mountpt && dir);
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_free(dir);
|
2013-11-13 22:59:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
static int procfs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR const struct procfs_entry_s *pentry = NULL;
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_dir_priv_s *priv;
|
|
|
|
FAR struct procfs_level0_s *level0;
|
2014-04-22 18:38:08 +02:00
|
|
|
FAR const char *name = NULL;
|
2013-11-13 22:59:14 +01:00
|
|
|
unsigned int index;
|
2013-12-12 16:21:55 +01:00
|
|
|
int ret = -ENOENT;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(mountpt && dir);
|
|
|
|
priv = (FAR struct procfs_dir_priv_s *)dir;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Are we reading the 1st directory level with dynamic PID and static
|
|
|
|
* entries?
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (priv->level == 0)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
level0 = (FAR struct procfs_level0_s *)priv;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Have we reached the end of the PID information */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
index = priv->index;
|
|
|
|
if (index >= priv->nentries)
|
|
|
|
{
|
2022-10-16 06:23:10 +02:00
|
|
|
index -= priv->nentries;
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* We must report the next static entry ... no more PID entries.
|
|
|
|
* skip any entries with wildcards in the first segment of the
|
|
|
|
* directory name.
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
while (index < g_procfs_entrycount)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
2022-10-16 06:23:10 +02:00
|
|
|
pentry = &g_procfs_entries[index];
|
2022-10-17 13:26:22 +02:00
|
|
|
name = pentry->pathpattern;
|
2017-08-11 22:50:39 +02:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
while (*name != '/' && *name != '\0')
|
|
|
|
{
|
|
|
|
if (*name == '*' || *name == '[' || *name == '?')
|
|
|
|
{
|
|
|
|
/* Wildcard found. Skip this entry */
|
|
|
|
|
|
|
|
index++;
|
|
|
|
name = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if we skipped this entry */
|
|
|
|
|
|
|
|
if (name != NULL)
|
2015-10-11 19:39:29 +02:00
|
|
|
{
|
2020-05-09 17:05:29 +02:00
|
|
|
/* This entry is okay to report. Test if it has a
|
|
|
|
* duplicate first level name as the one we just reported.
|
|
|
|
* This could happen in the event of procfs_entry_s such
|
|
|
|
* as:
|
2015-10-11 19:39:29 +02:00
|
|
|
*
|
|
|
|
* fs/smartfs
|
|
|
|
* fs/nfs
|
|
|
|
* fs/nxffs
|
|
|
|
*/
|
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
name = g_procfs_entries[index].pathpattern;
|
|
|
|
if (!level0->lastlen ||
|
|
|
|
strncmp(name, level0->lastread, level0->lastlen) != 0)
|
2015-10-11 19:39:29 +02:00
|
|
|
{
|
|
|
|
/* Not a duplicate, return the first segment of this
|
|
|
|
* entry
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Skip this entry ... duplicate 1st level name found */
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test if we are at the end of the directory */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
if (index < g_procfs_entrycount)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
/* Report the next static entry */
|
|
|
|
|
|
|
|
level0->lastlen = strcspn(name, "/");
|
|
|
|
level0->lastread = name;
|
2022-07-26 08:56:41 +02:00
|
|
|
strlcpy(entry->d_name, name, level0->lastlen + 1);
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2017-10-29 01:13:52 +02:00
|
|
|
/* If the entry is a directory type OR if the reported name is
|
|
|
|
* only a sub-string of the entry (meaning that it contains
|
|
|
|
* '/'), then report this entry as a directory.
|
|
|
|
*/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
if (pentry->type == PROCFS_DIR_TYPE ||
|
2017-10-29 01:13:52 +02:00
|
|
|
level0->lastlen != strlen(name))
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to next entry for the next read */
|
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
priv->index = priv->nentries + index;
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Verify that the pid still refers to an active task/thread */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
pid_t pid = level0->pid[index];
|
|
|
|
FAR struct tcb_s *tcb = nxsched_get_tcb(pid);
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2023-06-18 22:20:04 +02:00
|
|
|
ferr("ERROR: PID %d is no longer valid\n", pid);
|
2013-12-12 16:21:55 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Save the filename=pid and file type=directory */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2023-06-18 22:20:04 +02:00
|
|
|
procfs_snprintf(entry->d_name, NAME_MAX + 1, "%d", pid);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
/* Set up the next directory entry offset. NOTE that we could use
|
|
|
|
* the standard f_pos instead of our own private index.
|
2013-12-12 16:21:55 +01:00
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
level0->base.index = index + 1;
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Are we reading an intermediate subdirectory? */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-15 17:31:07 +01:00
|
|
|
else if (priv->level > 0 && priv->procfsentry == NULL)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
FAR struct procfs_level1_s *level1;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
level1 = (FAR struct procfs_level1_s *)priv;
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* Test if this entry matches. We assume all entries of the same
|
|
|
|
* subdirectory are listed in order in the procfs_entry array.
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-10-16 06:23:10 +02:00
|
|
|
if (level1->base.index < g_procfs_entrycount &&
|
|
|
|
level1->firstindex < g_procfs_entrycount &&
|
|
|
|
strncmp(g_procfs_entries[level1->base.index].pathpattern,
|
2015-12-01 21:56:20 +01:00
|
|
|
g_procfs_entries[level1->firstindex].pathpattern,
|
2015-11-27 19:33:58 +01:00
|
|
|
level1->subdirlen) == 0)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
/* This entry matches. Report the subdir entry */
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
name = &g_procfs_entries[level1->base.index].
|
|
|
|
pathpattern[level1->subdirlen + 1];
|
2013-12-12 16:21:55 +01:00
|
|
|
level1->lastlen = strcspn(name, "/");
|
|
|
|
level1->lastread = name;
|
2023-04-23 08:08:00 +02:00
|
|
|
strlcpy(entry->d_name, name, level1->lastlen + 1);
|
2014-09-22 19:07:08 +02:00
|
|
|
|
|
|
|
/* Some of the search entries contain '**' wildcards. When we
|
|
|
|
* report the entry name, we must remove this wildcard search
|
|
|
|
* specifier.
|
|
|
|
*/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
while (entry->d_name[level1->lastlen - 1] == '*')
|
2014-12-09 20:24:38 +01:00
|
|
|
{
|
|
|
|
level1->lastlen--;
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_name[level1->lastlen] = '\0';
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (name[level1->lastlen] == '/')
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
level1->base.index++;
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are performing a directory search of one of the subdirectories
|
|
|
|
* and we must let the handler perform the read.
|
2013-11-13 22:59:14 +01:00
|
|
|
*/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
DEBUGASSERT(priv->procfsentry && priv->procfsentry->ops->readdir);
|
2022-07-26 08:56:41 +02:00
|
|
|
ret = priv->procfsentry->ops->readdir(dir, entry);
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_dir_priv_s *priv;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(mountpt && dir);
|
|
|
|
priv = (FAR struct procfs_dir_priv_s *)dir;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-15 17:31:07 +01:00
|
|
|
if (priv->level > 0 && priv->procfsentry == NULL)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
2022-10-17 13:26:22 +02:00
|
|
|
priv->index = ((FAR struct procfs_level1_s *)priv)->firstindex;
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->index = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_bind
|
|
|
|
*
|
|
|
|
* Description: This implements a portion of the mount operation. This
|
|
|
|
* function allocates and initializes the mountpoint private data and
|
2019-05-25 22:12:00 +02:00
|
|
|
* binds the block driver inode to the filesystem private data. The final
|
|
|
|
* binding of the private data (containing the block driver) to the
|
2013-11-13 22:59:14 +01:00
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2019-05-25 22:12:00 +02:00
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
2015-12-01 21:56:20 +01:00
|
|
|
/* Make sure that we are properly initialized */
|
|
|
|
|
|
|
|
procfs_initialize();
|
2019-05-25 22:12:00 +02:00
|
|
|
#endif
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
2015-03-15 00:22:02 +01:00
|
|
|
unsigned int flags)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
|
|
|
/* Fill in the statfs info */
|
|
|
|
|
|
|
|
buf->f_type = PROCFS_MAGIC;
|
|
|
|
buf->f_bsize = 0;
|
|
|
|
buf->f_blocks = 0;
|
|
|
|
buf->f_bfree = 0;
|
|
|
|
buf->f_bavail = 0;
|
|
|
|
buf->f_namelen = NAME_MAX;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-17 13:26:22 +02:00
|
|
|
static int procfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2022-10-17 13:26:22 +02:00
|
|
|
int ret = -ENOENT;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/* Three path forms are accepted:
|
|
|
|
*
|
|
|
|
* "" - The relative path refers to the top level directory
|
|
|
|
* "<pid>" - If <pid> refers to a currently active task/thread, then it
|
|
|
|
* is a directory
|
|
|
|
* "<pid>/<attr>" - If <attr> is a recognized attribute then, then it
|
|
|
|
* is a file.
|
|
|
|
*/
|
|
|
|
|
2017-02-14 16:00:48 +01:00
|
|
|
memset(buf, 0, sizeof(struct stat));
|
2013-11-13 22:59:14 +01:00
|
|
|
if (!relpath || relpath[0] == '\0')
|
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
/* The path refers to the top level directory.
|
|
|
|
* It's a read-only directory.
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = OK;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
int x;
|
|
|
|
int len = strlen(relpath);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Perform the stat based on the procfs_entry operations */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
for (x = 0; x < g_procfs_entrycount; x++)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test if the path matches this entry's specification */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2021-08-01 10:54:08 +02:00
|
|
|
if (fnmatch(g_procfs_entries[x].pathpattern, relpath, 0) == 0)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
/* Match found! Stat using this procfs entry */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
DEBUGASSERT(g_procfs_entries[x].ops &&
|
2022-10-17 13:26:22 +02:00
|
|
|
g_procfs_entries[x].ops->stat);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
return g_procfs_entries[x].ops->stat(relpath, buf);
|
2013-12-12 16:21:55 +01:00
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test for an internal subdirectory stat */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
else if (strncmp(g_procfs_entries[x].pathpattern, relpath,
|
|
|
|
len) == 0)
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
/* It's an internal subdirectory */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
return ret;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the initial set of entries in the procfs file system.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
|
|
|
int procfs_initialize(void)
|
|
|
|
{
|
|
|
|
/* Are we already initialized? */
|
|
|
|
|
|
|
|
if (g_procfs_entries == NULL)
|
|
|
|
{
|
2019-05-25 22:12:00 +02:00
|
|
|
/* No.. allocate a modifiable list of entries */
|
2015-12-01 21:56:20 +01:00
|
|
|
|
|
|
|
g_procfs_entries = (FAR struct procfs_entry_s *)
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_malloc(sizeof(g_base_entries));
|
2015-12-01 21:56:20 +01:00
|
|
|
if (g_procfs_entries == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And copy the fixed entries into the allocated array */
|
|
|
|
|
|
|
|
memcpy(g_procfs_entries, g_base_entries, sizeof(g_base_entries));
|
|
|
|
g_procfs_entrycount = g_base_entrycount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-01 21:56:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add a new entry to the procfs file system.
|
|
|
|
*
|
2015-12-02 12:20:02 +01:00
|
|
|
* NOTE: This function should be called *prior* to mounting the procfs
|
|
|
|
* file system to prevent concurrency problems with the modification of
|
|
|
|
* the procfs data set while it is in use.
|
|
|
|
*
|
2015-12-01 21:56:20 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* entry - Describes the entry to be registered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_PROCFS_REGISTER
|
2015-12-01 23:31:40 +01:00
|
|
|
int procfs_register(FAR const struct procfs_entry_s *entry)
|
2015-12-01 21:56:20 +01:00
|
|
|
{
|
|
|
|
FAR struct procfs_entry_s *newtable;
|
|
|
|
unsigned int newcount;
|
|
|
|
size_t newsize;
|
2022-10-17 13:26:22 +02:00
|
|
|
int ret = -ENOMEM;
|
2015-12-01 21:56:20 +01:00
|
|
|
|
|
|
|
/* Make sure that we are properly initialized */
|
|
|
|
|
|
|
|
procfs_initialize();
|
|
|
|
|
2015-12-02 12:20:02 +01:00
|
|
|
/* realloc the table of procfs entries.
|
|
|
|
*
|
|
|
|
* REVISIT: This reallocation may free memory previously used for the
|
|
|
|
* procfs entry table. If that table were actively in use, then that
|
|
|
|
* could cause procfs logic to use a stale memory pointer! We avoid that
|
|
|
|
* problem by requiring that the procfs file be unmounted when the new
|
2018-07-09 02:24:45 +02:00
|
|
|
* entry is added. That requirement, however, is not enforced explicitly.
|
2015-12-02 12:20:02 +01:00
|
|
|
*
|
|
|
|
* Locking the scheduler as done below is insufficient. As would be just
|
|
|
|
* marking the entries as volatile.
|
|
|
|
*/
|
2015-12-01 21:56:20 +01:00
|
|
|
|
|
|
|
newcount = g_procfs_entrycount + 1;
|
|
|
|
newsize = newcount * sizeof(struct procfs_entry_s);
|
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
newtable = (FAR struct procfs_entry_s *)
|
2024-09-09 14:28:16 +02:00
|
|
|
fs_heap_realloc(g_procfs_entries, newsize);
|
2022-10-17 13:26:22 +02:00
|
|
|
if (newtable != NULL)
|
2015-12-01 21:56:20 +01:00
|
|
|
{
|
2015-12-02 12:20:02 +01:00
|
|
|
/* Copy the new entry at the end of the reallocated table */
|
|
|
|
|
2019-05-25 22:12:00 +02:00
|
|
|
memcpy(&newtable[g_procfs_entrycount], entry,
|
|
|
|
sizeof(struct procfs_entry_s));
|
2015-12-02 12:20:02 +01:00
|
|
|
|
|
|
|
/* Instantiate the reallocated table */
|
|
|
|
|
|
|
|
g_procfs_entries = newtable;
|
2015-12-01 21:56:20 +01:00
|
|
|
g_procfs_entrycount = newcount;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|