2019-08-17 06:42:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/procfs/fs_procfsiobinfo.c
|
|
|
|
*
|
2021-05-26 16:24:26 +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
|
2019-08-17 06:42:25 +08:00
|
|
|
*
|
2021-05-26 16:24:26 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-08-17 06:42:25 +08:00
|
|
|
*
|
2021-05-26 16:24:26 +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.
|
2019-08-17 06:42:25 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.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/kmalloc.h>
|
2019-08-20 02:16:43 +08:00
|
|
|
#include <nuttx/mm/iob.h>
|
2019-08-17 06:42:25 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
|
2024-09-09 20:28:16 +08:00
|
|
|
#include "fs_heap.h"
|
|
|
|
|
2019-08-17 06:42:25 +08:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-12-02 03:01:16 +08:00
|
|
|
|
2019-08-17 06:42:25 +08:00
|
|
|
/* Determines the size of an intermediate buffer that must be large enough
|
|
|
|
* to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define IOBINFO_LINELEN 80
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes one open "file" */
|
|
|
|
|
|
|
|
struct iobinfo_file_s
|
|
|
|
{
|
|
|
|
struct procfs_file_s base; /* Base open file structure */
|
|
|
|
unsigned int linesize; /* Number of valid characters in line[] */
|
|
|
|
char line[IOBINFO_LINELEN]; /* Pre-allocated buffer for formatted lines */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* File system methods */
|
|
|
|
|
|
|
|
static int iobinfo_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int iobinfo_close(FAR struct file *filep);
|
|
|
|
static ssize_t iobinfo_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int iobinfo_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
static int iobinfo_stat(FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* 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-18 02:23:18 +08:00
|
|
|
const struct procfs_operations g_iobinfo_operations =
|
2019-08-17 06:42:25 +08:00
|
|
|
{
|
|
|
|
iobinfo_open, /* open */
|
|
|
|
iobinfo_close, /* close */
|
|
|
|
iobinfo_read, /* read */
|
|
|
|
NULL, /* write */
|
2023-11-25 20:11:03 +08:00
|
|
|
NULL, /* poll */
|
2019-08-17 06:42:25 +08:00
|
|
|
iobinfo_dup, /* dup */
|
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
|
|
|
iobinfo_stat /* stat */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iobinfo_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iobinfo_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct iobinfo_file_s *procfile;
|
|
|
|
|
|
|
|
finfo("Open '%s'\n", relpath);
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a container to hold the file attributes */
|
|
|
|
|
|
|
|
procfile = (FAR struct iobinfo_file_s *)
|
2024-09-09 20:28:16 +08:00
|
|
|
fs_heap_zalloc(sizeof(struct iobinfo_file_s));
|
2019-08-17 06:42:25 +08:00
|
|
|
if (!procfile)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the attributes as the open-specific state in filep->f_priv */
|
|
|
|
|
|
|
|
filep->f_priv = (FAR void *)procfile;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iobinfo_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iobinfo_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct iobinfo_file_s *procfile;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
procfile = (FAR struct iobinfo_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(procfile);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
2024-09-09 20:28:16 +08:00
|
|
|
fs_heap_free(procfile);
|
2019-08-17 06:42:25 +08:00
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iobinfo_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t iobinfo_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct iobinfo_file_s *iobfile;
|
2022-08-08 10:21:03 +08:00
|
|
|
FAR struct iob_stats_s stats;
|
2019-08-17 06:42:25 +08:00
|
|
|
size_t linesize;
|
|
|
|
size_t copysize;
|
|
|
|
size_t totalsize;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
|
|
|
|
2023-08-28 17:17:05 +08:00
|
|
|
DEBUGASSERT(buffer != NULL && buflen > 0);
|
2019-08-17 06:42:25 +08:00
|
|
|
offset = filep->f_pos;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
iobfile = (FAR struct iobinfo_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(iobfile);
|
|
|
|
|
|
|
|
/* The first line is the headers */
|
|
|
|
|
2021-04-14 22:37:52 +08:00
|
|
|
linesize = procfs_snprintf(iobfile->line, IOBINFO_LINELEN,
|
2022-08-08 10:21:03 +08:00
|
|
|
"%10s%10s%10s%10s\n",
|
|
|
|
"ntotal", "nfree", "nwait", "nthrottle");
|
2019-08-17 06:42:25 +08:00
|
|
|
|
|
|
|
copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize = copysize;
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
buffer += copysize;
|
|
|
|
buflen -= copysize;
|
2019-08-17 06:42:25 +08:00
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
/* The second line is the usage statistics */
|
2019-08-17 06:42:25 +08:00
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
iob_getstats(&stats);
|
|
|
|
linesize = procfs_snprintf(iobfile->line, IOBINFO_LINELEN,
|
|
|
|
"%10d%10d%10d%10d\n",
|
|
|
|
stats.ntotal, stats.nfree,
|
|
|
|
stats.nwait, stats.nthrottle);
|
2019-08-17 06:42:25 +08:00
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize += copysize;
|
2019-08-17 06:42:25 +08:00
|
|
|
|
|
|
|
/* Update the file offset */
|
|
|
|
|
|
|
|
filep->f_pos += totalsize;
|
|
|
|
return totalsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iobinfo_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iobinfo_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct iobinfo_file_s *oldattr;
|
|
|
|
FAR struct iobinfo_file_s *newattr;
|
|
|
|
|
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldattr = (FAR struct iobinfo_file_s *)oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldattr);
|
|
|
|
|
|
|
|
/* Allocate a new container to hold the task and attribute selection */
|
|
|
|
|
|
|
|
newattr = (FAR struct iobinfo_file_s *)
|
2024-09-09 20:28:16 +08:00
|
|
|
fs_heap_malloc(sizeof(struct iobinfo_file_s));
|
2019-08-17 06:42:25 +08:00
|
|
|
if (!newattr)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The copy the file attributes from the old attributes to the new */
|
|
|
|
|
|
|
|
memcpy(newattr, oldattr, sizeof(struct iobinfo_file_s));
|
|
|
|
|
|
|
|
/* Save the new attributes in the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = (FAR void *)newattr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iobinfo_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iobinfo_stat(FAR const char *relpath, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
/* "iobinfo" is the name for a read-only file */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
|
|
|
|
* CONFIG_MM_IOB && !CONFIG_FS_PROCFS_EXCLUDE_IOBINFO */
|