2015-12-13 07:42:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/module/mod_procfs.c
|
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 23:33:58 +08: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
|
2015-12-13 07:42:25 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-13 07:42:25 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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.
|
2015-12-13 07:42:25 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/module.h>
|
2017-01-29 22:24:42 +08:00
|
|
|
#include <nuttx/lib/modlib.h>
|
2015-12-13 07:42:25 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
!defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Determines the size of an intermediate buffer that must be large enough
|
|
|
|
* to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MOD_LINELEN 64
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes one open "file" */
|
|
|
|
|
|
|
|
struct modprocfs_file_s
|
|
|
|
{
|
|
|
|
struct procfs_file_s base; /* Base open file structure */
|
|
|
|
|
|
|
|
/* Read helpers */
|
|
|
|
|
|
|
|
FAR char *buffer; /* User buffer pointer */
|
|
|
|
size_t buflen; /* Size of the user buffer */
|
|
|
|
size_t remaining; /* Space remaining in user buffer */
|
|
|
|
size_t totalsize; /* Total size returned by read() */
|
|
|
|
off_t offset; /* Offset skip on output */
|
2015-12-13 20:48:30 +08:00
|
|
|
char line[MOD_LINELEN]; /* Buffer for line formatting */
|
2015-12-13 07:42:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* File system methods */
|
|
|
|
|
2020-03-10 13:38:38 +08:00
|
|
|
static int modprocfs_open(FAR struct file *filep,
|
|
|
|
FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
2015-12-13 07:42:25 +08:00
|
|
|
static int modprocfs_close(FAR struct file *filep);
|
2020-03-10 13:38:38 +08:00
|
|
|
static ssize_t modprocfs_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer,
|
|
|
|
size_t buflen);
|
2015-12-13 07:42:25 +08:00
|
|
|
static int modprocfs_dup(FAR const struct file *oldp,
|
2020-03-10 13:38:38 +08:00
|
|
|
FAR struct file *newp);
|
2015-12-13 07:42:25 +08:00
|
|
|
static int modprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See include/nutts/fs/procfs.h
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2023-04-18 02:23:18 +08:00
|
|
|
const struct procfs_operations g_module_operations =
|
2015-12-13 07:42:25 +08:00
|
|
|
{
|
|
|
|
modprocfs_open, /* open */
|
|
|
|
modprocfs_close, /* close */
|
|
|
|
modprocfs_read, /* read */
|
|
|
|
NULL, /* write */
|
2023-11-25 20:11:03 +08:00
|
|
|
NULL, /* poll */
|
2015-12-13 07:42:25 +08:00
|
|
|
modprocfs_dup, /* dup */
|
|
|
|
|
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
|
|
|
|
|
|
|
modprocfs_stat /* stat */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modprocfs_callback(FAR struct module_s *modp, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct modprocfs_file_s *priv;
|
|
|
|
size_t linesize;
|
|
|
|
size_t copysize;
|
|
|
|
|
|
|
|
DEBUGASSERT(modp != NULL && arg != NULL);
|
|
|
|
priv = (FAR struct modprocfs_file_s *)arg;
|
|
|
|
|
2020-03-10 13:38:38 +08:00
|
|
|
linesize = snprintf(priv->line, MOD_LINELEN,
|
2024-04-28 18:16:07 +08:00
|
|
|
"%s,%p,%p,%u,%p,%lu,%p,%lu\n",
|
|
|
|
modp->modname,
|
2017-01-23 01:37:49 +08:00
|
|
|
modp->modinfo.uninitializer, modp->modinfo.arg,
|
2020-03-06 16:05:25 +08:00
|
|
|
modp->modinfo.nexports,
|
|
|
|
modp->textalloc,
|
2017-01-23 01:37:49 +08:00
|
|
|
(unsigned long)modp->textsize,
|
2020-03-06 16:05:25 +08:00
|
|
|
(FAR uint8_t *)modp->dataalloc,
|
2015-12-13 08:56:32 +08:00
|
|
|
(unsigned long)modp->datasize);
|
2015-12-13 07:42:25 +08:00
|
|
|
copysize = procfs_memcpy(priv->line, linesize, priv->buffer,
|
|
|
|
priv->remaining, &priv->offset);
|
|
|
|
priv->totalsize += copysize;
|
|
|
|
priv->buffer += copysize;
|
|
|
|
priv->remaining -= copysize;
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
return priv->totalsize >= priv->buflen;
|
2015-12-13 07:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modprocfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct modprocfs_file_s *priv;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2015-12-13 07:42:25 +08:00
|
|
|
|
|
|
|
/* PROCFS is read-only. Any attempt to open with any kind of write
|
|
|
|
* access is not permitted.
|
|
|
|
*
|
|
|
|
* REVISIT: Write-able proc files could be quite useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0))
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
2015-12-13 07:42:25 +08:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the open file structure */
|
|
|
|
|
2020-03-10 13:38:38 +08:00
|
|
|
priv = (FAR struct modprocfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct modprocfs_file_s));
|
2015-12-13 07:42:25 +08:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2015-12-13 07:42:25 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the open file structure as the open-specific state in
|
|
|
|
* filep->f_priv.
|
|
|
|
*/
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
filep->f_priv = priv;
|
2015-12-13 07:42:25 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modprocfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct modprocfs_file_s *priv;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = (FAR struct modprocfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
|
|
|
kmm_free(priv);
|
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t modprocfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct modprocfs_file_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
finfo("buffer=%p buflen=%zu\n", buffer, buflen);
|
2015-12-13 07:42:25 +08:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = (FAR struct modprocfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Traverse all installed modules */
|
|
|
|
|
|
|
|
priv->remaining = buflen;
|
|
|
|
priv->totalsize = 0;
|
|
|
|
priv->buffer = buffer;
|
|
|
|
priv->buflen = buflen;
|
|
|
|
priv->offset = filep->f_pos;
|
|
|
|
|
2017-01-30 00:05:15 +08:00
|
|
|
ret = modlib_registry_foreach(modprocfs_callback, priv);
|
2015-12-13 07:42:25 +08:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos += priv->totalsize;
|
|
|
|
return priv->totalsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modprocfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct modprocfs_file_s *oldpriv;
|
|
|
|
FAR struct modprocfs_file_s *newpriv;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2015-12-13 07:42:25 +08:00
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldpriv = (FAR struct modprocfs_file_s *)oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldpriv);
|
|
|
|
|
|
|
|
/* Allocate a new container to hold the task and attribute selection */
|
|
|
|
|
2019-12-02 03:01:16 +08:00
|
|
|
newpriv = (FAR struct modprocfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct modprocfs_file_s));
|
2015-12-13 07:42:25 +08:00
|
|
|
if (!newpriv)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2015-12-13 07:42:25 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-01-27 23:37:46 +08:00
|
|
|
/* The copy the file attributes from the old attributes to the new */
|
2015-12-13 07:42:25 +08:00
|
|
|
|
|
|
|
memcpy(newpriv, oldpriv, sizeof(struct modprocfs_file_s));
|
|
|
|
|
|
|
|
/* Save the new attributes in the new file structure */
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
newp->f_priv = newpriv;
|
2015-12-13 07:42:25 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modprocfs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modprocfs_stat(FAR const char *relpath, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
2023-07-03 00:11:02 +08:00
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
2015-12-13 07:42:25 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
|
|
|
|
* !CONFIG_FS_PROCFS_EXCLUDE_MODULE */
|