2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/procfs/fs_procfs.c
|
|
|
|
*
|
2014-02-22 22:20:12 +01:00
|
|
|
* Copyright (C) 2013-2014 Gregory Nutt. All rights reserved.
|
2013-11-13 22:59:14 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/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
|
|
|
#include <nuttx/fs/dirent.h>
|
2013-12-12 16:21:55 +01:00
|
|
|
#include <nuttx/regex.h>
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-22 22:20:12 +01:00
|
|
|
#define PROCFS_NATTRS 2
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2013-12-12 16:21:55 +01:00
|
|
|
* External Definitons
|
2013-11-13 22:59:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-15 17:31:07 +01:00
|
|
|
extern const struct procfs_operations proc_operations;
|
2014-02-22 22:20:12 +01:00
|
|
|
extern const struct procfs_operations cpuload_operations;
|
2013-12-14 17:34:08 +01:00
|
|
|
extern const struct procfs_operations uptime_operations;
|
2014-07-03 16:50:24 +02:00
|
|
|
|
|
|
|
/* This is not good. These are implemented in drivers/mtd. Having to
|
|
|
|
* deal with them here is not a good coupling.
|
|
|
|
*/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
extern const struct procfs_operations mtd_procfsoperations;
|
|
|
|
extern const struct procfs_operations part_procfsoperations;
|
|
|
|
extern const struct procfs_operations smartfs_procfsoperations;
|
2014-07-03 16:50:24 +02:00
|
|
|
|
|
|
|
/* And even worse, this one is specific to the STM32. The solution to
|
|
|
|
* this nasty couple would be to replace this hard-coded, ROM-able
|
|
|
|
* operations table with a RAM-base registration table.
|
|
|
|
*/
|
|
|
|
|
2014-07-03 17:28:36 +02:00
|
|
|
#if defined(CONFIG_STM32_CCM_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CCM)
|
2014-07-03 16:18:24 +02:00
|
|
|
extern const struct procfs_operations ccm_procfsoperations;
|
|
|
|
#endif
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
/* Table of all known / pre-registered procfs handlers / participants. */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
static const struct procfs_entry_s g_procfsentries[] =
|
2013-11-13 22:59:14 +01:00
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
|
2013-12-15 17:31:07 +01:00
|
|
|
{ "[0-9]*/**", &proc_operations },
|
|
|
|
{ "[0-9]*", &proc_operations },
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_SCHED_CPULOAD) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CPULOAD)
|
|
|
|
{ "cpuload", &cpuload_operations },
|
|
|
|
#endif
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#if defined(CONFIG_FS_SMARTFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_SMARTFS)
|
|
|
|
//{ "fs/smartfs", &smartfs_procfsoperations },
|
|
|
|
{ "fs/smartfs**", &smartfs_procfsoperations },
|
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#if defined(CONFIG_MTD) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MTD)
|
|
|
|
{ "mtd", &mtd_procfsoperations },
|
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#if defined(CONFIG_MTD_PARTITION) && !defined(CONFIG_FS_PROCFS_EXCLUDE_PARTITON)
|
|
|
|
{ "partitions", &part_procfsoperations },
|
|
|
|
#endif
|
2014-02-22 22:20:12 +01:00
|
|
|
|
2013-12-14 17:34:08 +01:00
|
|
|
#if !defined(CONFIG_FS_PROCFS_EXCLUDE_UPTIME)
|
|
|
|
{ "uptime", &uptime_operations },
|
|
|
|
#endif
|
2014-07-03 16:50:24 +02:00
|
|
|
|
2014-07-03 17:28:36 +02:00
|
|
|
#if defined(CONFIG_STM32_CCM_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CCM)
|
2014-07-03 16:18:24 +02:00
|
|
|
{ "ccm", &ccm_procfsoperations },
|
|
|
|
#endif
|
2013-11-13 22:59:14 +01:00
|
|
|
};
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
static const uint8_t g_procfsentrycount = sizeof(g_procfsentries) /
|
2014-02-22 22:20:12 +01:00
|
|
|
sizeof(struct procfs_entry_s);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void procfs_enum(FAR struct tcb_s *tcb, FAR void *arg);
|
|
|
|
|
|
|
|
/* 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);
|
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);
|
|
|
|
|
|
|
|
static int procfs_opendir(FAR struct inode *mountpt, const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int procfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int procfs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
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);
|
|
|
|
static int procfs_unbind(FAR void *handle, FAR struct inode **blkdriver);
|
|
|
|
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);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct mountpt_operations procfs_operations =
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
|
|
|
NULL, /* sync */
|
|
|
|
procfs_dup, /* dup */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
procfs_stat /* stat */
|
|
|
|
};
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Level 0 contains the directory of active tasks in addition to other
|
|
|
|
* statically registered entries with custom handlers. This strcture
|
|
|
|
* 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
|
|
|
{
|
2014-09-22 19:07:08 +02:00
|
|
|
struct procfs_dir_priv_s base; /* Base struct for ProcFS dir */
|
|
|
|
|
|
|
|
/* Our private data */
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
uint8_t lastlen; /* length of last reported static dir */
|
|
|
|
pid_t pid[CONFIG_MAX_TASKS]; /* Snapshot of all active task IDs */
|
|
|
|
FAR const char *lastread; /* Pointer to last static dir read */
|
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
|
|
|
{
|
2014-09-22 19:07:08 +02:00
|
|
|
struct procfs_dir_priv_s base; /* Base struct for ProcFS dir */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
index = dir->base.nentries;
|
2013-11-13 22:59:14 +01:00
|
|
|
DEBUGASSERT(index < CONFIG_MAX_TASKS);
|
|
|
|
|
|
|
|
dir->pid[index] = tcb->pid;
|
2014-09-22 19:07:08 +02:00
|
|
|
dir->base.nentries = index + 1;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
int x, ret = -ENOENT;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
fvdbg("Open '%s'\n", relpath);
|
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
for (x = 0; x < g_procfsentrycount; 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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (match(g_procfsentries[x].pathpattern, relpath))
|
|
|
|
{
|
|
|
|
/* Match found! Stat using this procfs entry */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
DEBUGASSERT(g_procfsentries[x].ops &&
|
|
|
|
g_procfsentries[x].ops->open);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = g_procfsentries[x].ops->open(filep, relpath, oflags, mode);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(filep->f_priv);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
((struct procfs_file_s *) filep->f_priv)->procfsentry =
|
|
|
|
&g_procfsentries[x];
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(attr);
|
2013-11-13 22:59:14 +01:00
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
ssize_t ret = 0;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
fvdbg("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = handler->procfsentry->ops->read(filep, buffer, buflen);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-09 20:24:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t procfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct procfs_file_s *handler;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
|
|
|
|
fvdbg("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
handler = (FAR struct procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(handler);
|
|
|
|
|
|
|
|
/* Call the handler's read routine */
|
|
|
|
|
|
|
|
if (handler->procfsentry->ops->write)
|
|
|
|
{
|
|
|
|
ret = handler->procfsentry->ops->write(filep, buffer, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
fvdbg("cmd: %d arg: %08lx\n", cmd, arg);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
fvdbg("Dup %p->%p\n", oldp, newp);
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_opendir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_level0_s *level0;
|
2013-12-12 18:08:20 +01:00
|
|
|
FAR struct procfs_dir_priv_s *dirpriv;
|
2013-11-13 22:59:14 +01:00
|
|
|
FAR void *priv = NULL;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
fvdbg("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
|
|
|
DEBUGASSERT(mountpt && relpath && dir && !dir->u.procfs);
|
|
|
|
|
|
|
|
/* 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')
|
|
|
|
{
|
|
|
|
/* The path refers to the top level directory. Allocate the level0
|
|
|
|
* dirent structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
level0 = (FAR struct procfs_level0_s *)
|
2014-09-01 01:34:44 +02:00
|
|
|
kmm_zalloc(sizeof(struct procfs_level0_s));
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
if (!level0)
|
|
|
|
{
|
|
|
|
fdbg("ERROR: Failed to allocate the level0 directory structure\n");
|
|
|
|
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
|
2013-11-13 22:59:14 +01:00
|
|
|
flags = irqsave();
|
|
|
|
sched_foreach(procfs_enum, level0);
|
|
|
|
irqrestore(flags);
|
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
|
|
|
|
|
|
|
|
/* Initialze lastread entries */
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
priv = (FAR void *)level0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
int x, ret;
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
for (x = 0; x < g_procfsentrycount; 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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (match(g_procfsentries[x].pathpattern, relpath))
|
|
|
|
{
|
2013-12-12 18:08:20 +01:00
|
|
|
/* Match found! Call the handler's opendir routine. If successful,
|
|
|
|
* this opendir routine will create an entry derived from struct
|
|
|
|
* procfs_dir_priv_s as dir->u.procfs.
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
DEBUGASSERT(g_procfsentries[x].ops && g_procfsentries[x].ops->opendir);
|
|
|
|
ret = g_procfsentries[x].ops->opendir(relpath, dir);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dir->u.procfs);
|
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
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
dirpriv = (FAR struct procfs_dir_priv_s *)dir->u.procfs;
|
|
|
|
dirpriv->procfsentry = &g_procfsentries[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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Test for a sub-string match (e.g. "ls /proc/fs") */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
else if (strncmp(g_procfsentries[x].pathpattern, relpath, len) == 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* The path refers to the top level directory. Allocate the level0
|
|
|
|
* dirent structure.
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
level1 = (FAR struct procfs_level1_s *)
|
2014-09-01 01:34:44 +02:00
|
|
|
kmm_zalloc(sizeof(struct procfs_level1_s));
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!level1)
|
|
|
|
{
|
|
|
|
fdbg("ERROR: Failed to allocate the level0 directory structure\n");
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
priv = (FAR void *)level1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dir->u.procfs = priv;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close the directory listing
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_dir_priv_s *priv;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && dir && dir->u.procfs);
|
|
|
|
priv = dir->u.procfs;
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dir->u.procfs = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_dir_priv_s *priv;
|
|
|
|
FAR struct procfs_level0_s *level0;
|
2013-11-13 22:59:14 +01:00
|
|
|
FAR struct tcb_s *tcb;
|
2014-04-22 18:38:08 +02:00
|
|
|
FAR const char *name = NULL;
|
2013-11-13 22:59:14 +01:00
|
|
|
unsigned int index;
|
|
|
|
irqstate_t flags;
|
|
|
|
pid_t pid;
|
2013-12-12 16:21:55 +01:00
|
|
|
int ret = -ENOENT;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && dir && dir->u.procfs);
|
|
|
|
priv = dir->u.procfs;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
while (index < priv->nentries + g_procfsentrycount)
|
|
|
|
{
|
|
|
|
name = g_procfsentries[index - priv->nentries].pathpattern;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* 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:
|
|
|
|
*
|
|
|
|
* fs/smartfs
|
|
|
|
* fs/nfs
|
|
|
|
* fs/nxffs
|
|
|
|
*/
|
|
|
|
|
|
|
|
name = g_procfsentries[index - priv->nentries].pathpattern;
|
|
|
|
if (!level0->lastlen || (strncmp(name, level0->lastread,
|
|
|
|
level0->lastlen) != 0))
|
|
|
|
{
|
|
|
|
/* Not a duplicate, return the first segment of this
|
|
|
|
* entry
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Skip this entry ... duplicate 1st level name found */
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (index >= priv->nentries + g_procfsentrycount)
|
|
|
|
{
|
|
|
|
/* We signal the end of the directory by returning the special
|
|
|
|
* error -ENOENT
|
|
|
|
*/
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
fvdbg("Entry %d: End of directory\n", index);
|
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Report the next static entry */
|
|
|
|
|
|
|
|
level0->lastlen = strcspn(name, "/");
|
|
|
|
level0->lastread = name;
|
|
|
|
strncpy(dir->fd_dir.d_name, name, level0->lastlen);
|
|
|
|
dir->fd_dir.d_name[level0->lastlen] = '\0';
|
|
|
|
|
|
|
|
if (name[level0->lastlen] == '/')
|
|
|
|
{
|
|
|
|
dir->fd_dir.d_type = DTYPE_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->fd_dir.d_type = DTYPE_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to next entry for the next read */
|
|
|
|
|
|
|
|
priv->index = index;
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
pid = level0->pid[index];
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
flags = irqsave();
|
|
|
|
tcb = sched_gettcb(pid);
|
|
|
|
irqrestore(flags);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!tcb)
|
|
|
|
{
|
|
|
|
fdbg("ERROR: PID %d is no longer valid\n", (int)pid);
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
dir->fd_dir.d_type = DTYPE_DIRECTORY;
|
|
|
|
snprintf(dir->fd_dir.d_name, NAME_MAX+1, "%d", (int)pid);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/* Set up the next directory entry offset. NOTE that we could use the
|
|
|
|
* standard f_pos instead of our own private index.
|
|
|
|
*/
|
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
|
|
|
|
2013-12-12 18:08:20 +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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
level1 = (FAR struct procfs_level1_s *) priv;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
if (strncmp(g_procfsentries[level1->base.index].pathpattern,
|
2013-12-12 16:21:55 +01:00
|
|
|
g_procfsentries[level1->firstindex].pathpattern,
|
|
|
|
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 */
|
|
|
|
|
2014-09-22 19:07:08 +02:00
|
|
|
name = &g_procfsentries[level1->base.index].pathpattern[
|
2013-12-12 16:21:55 +01:00
|
|
|
level1->subdirlen + 1];
|
|
|
|
level1->lastlen = strcspn(name, "/");
|
|
|
|
level1->lastread = name;
|
|
|
|
strncpy(dir->fd_dir.d_name, name, level1->lastlen);
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (dir->fd_dir.d_name[level1->lastlen - 1] == '*')
|
2014-12-09 20:24:38 +01:00
|
|
|
{
|
|
|
|
level1->lastlen--;
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
dir->fd_dir.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] == '/')
|
|
|
|
{
|
|
|
|
dir->fd_dir.d_type = DTYPE_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->fd_dir.d_type = DTYPE_FILE;
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
/* No more entries in the subdirectory */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
ret = priv->procfsentry->ops->readdir(dir);
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR struct procfs_dir_priv_s *priv;
|
2013-11-13 22:59:14 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && dir && dir->u.procfs);
|
|
|
|
priv = dir->u.procfs;
|
|
|
|
|
2013-12-15 17:31:07 +01:00
|
|
|
if (priv->level > 0 && priv->procfsentry == NULL)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
priv->index = ((struct procfs_level1_s *) priv)->firstindex;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* binds the blockdriver inode to the filesystem private data. The final
|
|
|
|
* binding of the private data (containing the blockdriver) to the
|
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_bind(FAR struct inode *blkdriver, const void *data,
|
|
|
|
void **handle)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_unbind(void *handle, FAR struct inode **blkdriver)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_statfs(struct inode *mountpt, struct statfs *buf)
|
|
|
|
{
|
|
|
|
/* Fill in the statfs info */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct statfs));
|
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int procfs_stat(struct inode *mountpt, const char *relpath,
|
|
|
|
struct stat *buf)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
int ret = -ENOSYS;
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!relpath || relpath[0] == '\0')
|
|
|
|
{
|
|
|
|
/* The path refers to the top level directory */
|
|
|
|
/* It's a read-only directory */
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
for (x = 0; x < g_procfsentrycount; 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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
if (match(g_procfsentries[x].pathpattern, relpath))
|
|
|
|
{
|
|
|
|
/* Match found! Stat using this procfs entry */
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
DEBUGASSERT(g_procfsentries[x].ops &&
|
|
|
|
g_procfsentries[x].ops->stat);
|
2013-11-13 22:59:14 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
return g_procfsentries[x].ops->stat(relpath, buf);
|
|
|
|
}
|
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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
else if (strncmp(g_procfsentries[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
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
buf->st_mode = S_IFDIR|S_IROTH|S_IRGRP|S_IRUSR;
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* File/directory size, access block size */
|
|
|
|
|
|
|
|
buf->st_size = 0;
|
|
|
|
buf->st_blksize = 0;
|
|
|
|
buf->st_blocks = 0;
|
2013-12-12 16:21:55 +01:00
|
|
|
return ret;
|
2013-11-13 22:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */
|