MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Functions | Variables
procfs.c File Reference

Proc file system implementation. More...

Classes

struct  procfs_file_t
 Information concerning a file. More...
 
struct  procfs_t
 The details regarding the filesystem. More...
 

Macros

#define __DEBUG_HEADER__   "[PROCFS]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define PROCFS_NAME_MAX   255U
 Maximum length of name in PROCFS.
 
#define PROCFS_MAX_FILES   1024U
 Maximum number of files in PROCFS.
 
#define PROCFS_MAGIC_NUMBER   0xBF
 The magic number used to check if the procfs file is valid.
 

Typedefs

typedef struct procfs_file_t procfs_file_t
 Information concerning a file.
 
typedef struct procfs_t procfs_t
 The details regarding the filesystem. More...
 

Functions

static int procfs_mkdir (const char *path, mode_t mode)
 Creates a new directory. More...
 
static int procfs_rmdir (const char *path)
 Removes a directory. More...
 
static int procfs_stat (const char *path, stat_t *stat)
 Retrieves information concerning the file at the given position. More...
 
static vfs_file_tprocfs_open (const char *path, int flags, mode_t mode)
 Open the file at the given path and returns its file descriptor. More...
 
static int procfs_unlink (const char *path)
 Deletes the file at the given path. More...
 
static int procfs_close (vfs_file_t *file)
 Closes the given file. More...
 
static ssize_t procfs_read (vfs_file_t *file, char *buffer, off_t offset, size_t nbyte)
 Reads from the file identified by the file descriptor. More...
 
static ssize_t procfs_write (vfs_file_t *file, const void *buffer, off_t offset, size_t nbyte)
 Writes the given content inside the file. More...
 
static off_t procfs_lseek (vfs_file_t *file, off_t offset, int whence)
 Repositions the file offset inside a file. More...
 
static int procfs_fstat (vfs_file_t *file, stat_t *stat)
 Retrieves information concerning the file at the given position. More...
 
static int procfs_ioctl (vfs_file_t *file, int request, void *data)
 Perform the I/O control operation specified by REQUEST on FD. One argument may follow; its presence and type depend on REQUEST. More...
 
static ssize_t procfs_getdents (vfs_file_t *file, dirent_t *dirp, off_t doff, size_t count)
 Reads contents of the directories to a dirent buffer, updating the offset and returning the number of written bytes in the buffer, it assumes that all paths are well-formed. More...
 
static bool_t procfs_check_file (procfs_file_t *procfs_file)
 Checks if the file is a valid PROCFS file. More...
 
static procfs_file_tprocfs_get_file (list_head *entry)
 Returns the PROCFS file associated with the given list entry. More...
 
static procfs_file_tprocfs_find_entry_path (const char *path)
 Finds the PROCFS file at the given path. More...
 
static procfs_file_tprocfs_find_entry_inode (uint32_t inode)
 Finds the PROCFS file with the given inode. More...
 
static int procfs_find_inode (const char *path)
 Finds the inode associated with a PROCFS file at the given path. More...
 
static int procfs_get_free_inode (void)
 Finds a free inode. More...
 
static int procfs_check_if_empty (const char *path)
 Checks if the PROCFS directory at the given path is empty. More...
 
static procfs_file_tprocfs_create_file (const char *path, unsigned flags)
 Creates a new PROCFS file. More...
 
static int procfs_destroy_file (procfs_file_t *procfs_file)
 Destroyes the given PROCFS file. More...
 
static vfs_file_tprocfs_create_file_struct (procfs_file_t *procfs_file)
 Creates a VFS file, from a PROCFS file. More...
 
static void dump_procfs (void)
 Dumps on debugging output the PROCFS.
 
static int __procfs_stat (procfs_file_t *file, stat_t *stat)
 Saves the information concerning the file. More...
 
static vfs_file_tprocfs_mount (vfs_file_t *block_device, const char *path)
 Mounts the block device as a procfs filesystem. More...
 
static vfs_file_tprocfs_mount_callback (const char *path, const char *device)
 Mounts the filesystem at the given path. More...
 
int procfs_module_init (void)
 Initialize the procfs filesystem. More...
 
int procfs_cleanup_module (void)
 Clean up the procfs filesystem. More...
 
proc_dir_entry_tproc_dir_entry_get (const char *name, proc_dir_entry_t *parent)
 Finds the direntry inside /proc or under the given parent. More...
 
proc_dir_entry_tproc_mkdir (const char *name, proc_dir_entry_t *parent)
 Creates a new directory inside the procfs filesystem. More...
 
int proc_rmdir (const char *name, proc_dir_entry_t *parent)
 Removes a directory from the procfs filesystem. More...
 
proc_dir_entry_tproc_create_entry (const char *name, proc_dir_entry_t *parent)
 Creates a new entry inside the procfs filesystem. More...
 
int proc_destroy_entry (const char *name, proc_dir_entry_t *parent)
 Removes an entry from the procfs filesystem. More...
 
int proc_entry_set_mask (proc_dir_entry_t *entry, mode_t mask)
 Sets the mask of a given procfs entry. More...
 

Variables

procfs_t fs
 The procfs filesystem.
 
static vfs_sys_operations_t procfs_sys_operations
 Filesystem general operations. More...
 
static vfs_file_operations_t procfs_fs_operations
 Filesystem file operations. More...
 
static file_system_type procfs_file_system_type
 Filesystem information. More...
 

Detailed Description

Proc file system implementation.

Typedef Documentation

◆ procfs_t

typedef struct procfs_t procfs_t

The details regarding the filesystem.

Contains the number of files inside the procfs filesystem.

Function Documentation

◆ __procfs_stat()

static int __procfs_stat ( procfs_file_t file,
stat_t stat 
)
static

Saves the information concerning the file.

Parameters
fileThe file containing the data.
statThe structure where the information are stored.
Returns
0 if success.

◆ proc_create_entry()

proc_dir_entry_t* proc_create_entry ( const char *  name,
proc_dir_entry_t parent 
)

Creates a new entry inside the procfs filesystem.

Parameters
nameThe name of the entry we are creating.
parentThe parent (optional).
Returns
A pointer to the entry, or NULL if fails.

◆ proc_destroy_entry()

int proc_destroy_entry ( const char *  name,
proc_dir_entry_t parent 
)

Removes an entry from the procfs filesystem.

Parameters
nameThe name of the entry we are removing.
parentThe parent (optional).
Returns
0 if succeed, or -errno in case of error.

◆ proc_dir_entry_get()

proc_dir_entry_t* proc_dir_entry_get ( const char *  name,
proc_dir_entry_t parent 
)

Finds the direntry inside /proc or under the given parent.

Parameters
nameThe name of the entry we are searching.
parentThe parent (optional).
Returns
A pointer to the entry, or NULL.

◆ proc_entry_set_mask()

int proc_entry_set_mask ( proc_dir_entry_t entry,
mode_t  mask 
)

Sets the mask of a given procfs entry.

Parameters
entryPointer to the entry.
maskThe mask to set.
Returns
0 if succeed, or -errno in case of error.

◆ proc_mkdir()

proc_dir_entry_t* proc_mkdir ( const char *  name,
proc_dir_entry_t parent 
)

Creates a new directory inside the procfs filesystem.

Parameters
nameThe name of the entry we are creating.
parentThe parent (optional).
Returns
A pointer to the entry, or NULL if fails.

◆ proc_rmdir()

int proc_rmdir ( const char *  name,
proc_dir_entry_t parent 
)

Removes a directory from the procfs filesystem.

Parameters
nameThe name of the entry we are removing.
parentThe parent (optional).
Returns
0 if succeed, or -errno in case of error.

◆ procfs_check_file()

static bool_t procfs_check_file ( procfs_file_t procfs_file)
inlinestatic

Checks if the file is a valid PROCFS file.

Parameters
procfs_filethe file to check.
Returns
true if valid, false otherwise.

◆ procfs_check_if_empty()

static int procfs_check_if_empty ( const char *  path)
inlinestatic

Checks if the PROCFS directory at the given path is empty.

Parameters
paththe path to the directory.
Returns
0 if empty, 1 if not.

◆ procfs_cleanup_module()

int procfs_cleanup_module ( void  )

Clean up the procfs filesystem.

Returns
0 if fails, 1 if succeed.

◆ procfs_close()

static int procfs_close ( vfs_file_t file)
static

Closes the given file.

Parameters
fileThe file structure.
Returns
0 on success, -errno on failure.

◆ procfs_create_file()

static procfs_file_t* procfs_create_file ( const char *  path,
unsigned  flags 
)
inlinestatic

Creates a new PROCFS file.

Parameters
pathwhere the file resides.
flagsthe creation flags.
Returns
a pointer to the new PROCFS file, NULL otherwise.

◆ procfs_create_file_struct()

static vfs_file_t* procfs_create_file_struct ( procfs_file_t procfs_file)
inlinestatic

Creates a VFS file, from a PROCFS file.

Parameters
procfs_filethe PROCFS file.
Returns
a pointer to the newly create VFS file, NULL on failure.

◆ procfs_destroy_file()

static int procfs_destroy_file ( procfs_file_t procfs_file)
inlinestatic

Destroyes the given PROCFS file.

Parameters
procfs_filepointer to the PROCFS file to destroy.
Returns
0 on success, 1 on failure.

◆ procfs_find_entry_inode()

static procfs_file_t* procfs_find_entry_inode ( uint32_t  inode)
inlinestatic

Finds the PROCFS file with the given inode.

Parameters
inodethe inode we search.
Returns
a pointer to the PROCFS file, NULL otherwise.

◆ procfs_find_entry_path()

static procfs_file_t* procfs_find_entry_path ( const char *  path)
inlinestatic

Finds the PROCFS file at the given path.

Parameters
paththe path to the entry.
Returns
a pointer to the PROCFS file, NULL otherwise.

◆ procfs_find_inode()

static int procfs_find_inode ( const char *  path)
inlinestatic

Finds the inode associated with a PROCFS file at the given path.

Parameters
paththe path to the entry.
Returns
a valid inode, or -1 on failure.

◆ procfs_fstat()

static int procfs_fstat ( vfs_file_t file,
stat_t stat 
)
static

Retrieves information concerning the file at the given position.

Parameters
fileThe file struct.
statThe structure where the information are stored.
Returns
0 if success.

◆ procfs_get_file()

static procfs_file_t* procfs_get_file ( list_head *  entry)
inlinestatic

Returns the PROCFS file associated with the given list entry.

Parameters
entrythe entry to transform to PROCFS file.
Returns
a valid pointer to a PROCFS file, NULL otherwise.

◆ procfs_get_free_inode()

static int procfs_get_free_inode ( void  )
inlinestatic

Finds a free inode.

Returns
the free inode index, or -1 on failure.

◆ procfs_getdents()

static ssize_t procfs_getdents ( vfs_file_t file,
dirent_t dirp,
off_t  doff,
size_t  count 
)
inlinestatic

Reads contents of the directories to a dirent buffer, updating the offset and returning the number of written bytes in the buffer, it assumes that all paths are well-formed.

Parameters
fileThe directory handler.
dirpThe buffer where the data should be written.
doffThe offset inside the buffer where the data should be written.
countThe maximum length of the buffer.
Returns
The number of written bytes in the buffer.

◆ procfs_ioctl()

static int procfs_ioctl ( vfs_file_t file,
int  request,
void *  data 
)
static

Perform the I/O control operation specified by REQUEST on FD. One argument may follow; its presence and type depend on REQUEST.

Parameters
filethe file on which we perform the operations.
requestthe device-dependent request code
dataan untyped pointer to memory.
Returns
Return value depends on REQUEST. Usually -1 indicates error.

◆ procfs_lseek()

off_t procfs_lseek ( vfs_file_t file,
off_t  offset,
int  whence 
)
static

Repositions the file offset inside a file.

Parameters
filethe file we are working with.
offsetthe offest to use for the operation.
whencethe type of operation.
Returns
Upon successful completion, returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.

◆ procfs_mkdir()

static int procfs_mkdir ( const char *  path,
mode_t  mode 
)
static

Creates a new directory.

Parameters
pathThe path to the new directory.
modeThe file mode.
Returns
0 if success.

◆ procfs_module_init()

int procfs_module_init ( void  )

Initialize the procfs filesystem.

Returns
0 if fails, 1 if succeed.

◆ procfs_mount()

static vfs_file_t* procfs_mount ( vfs_file_t block_device,
const char *  path 
)
static

Mounts the block device as a procfs filesystem.

Parameters
block_devicethe block device formatted as procfs.
pathlocation where we mount the filesystem.
Returns
the VFS root node of the procfs filesystem.

◆ procfs_mount_callback()

static vfs_file_t* procfs_mount_callback ( const char *  path,
const char *  device 
)
static

Mounts the filesystem at the given path.

Parameters
paththe path where we want to mount a procfs.
devicewe expect it to be NULL.
Returns
a pointer to the root VFS file.

◆ procfs_open()

static vfs_file_t * procfs_open ( const char *  path,
int  flags,
mode_t  mode 
)
static

Open the file at the given path and returns its file descriptor.

Parameters
pathThe path to the file.
flagsThe flags used to determine the behavior of the function.
modeThe mode with which we open the file.
Returns
The file descriptor of the opened file, otherwise returns -1.

◆ procfs_read()

static ssize_t procfs_read ( vfs_file_t file,
char *  buffer,
off_t  offset,
size_t  nbyte 
)
static

Reads from the file identified by the file descriptor.

Parameters
fileThe file.
bufferBuffer where the read content must be placed.
offsetOffset from which we start reading from the file.
nbyteThe number of bytes to read.
Returns
The number of red bytes.

◆ procfs_rmdir()

static int procfs_rmdir ( const char *  path)
static

Removes a directory.

Parameters
pathThe path to the directory.
Returns
0 if success.

◆ procfs_stat()

static int procfs_stat ( const char *  path,
stat_t stat 
)
static

Retrieves information concerning the file at the given position.

Parameters
pathThe path to the file.
statThe structure where the information are stored.
Returns
0 if success.

◆ procfs_unlink()

static int procfs_unlink ( const char *  path)
inlinestatic

Deletes the file at the given path.

Parameters
pathThe path to the file.
Returns
On success, zero is returned. On error, -1 is returned.

◆ procfs_write()

static ssize_t procfs_write ( vfs_file_t file,
const void *  buffer,
off_t  offset,
size_t  nbyte 
)
static

Writes the given content inside the file.

Parameters
fileThe file descriptor of the file.
bufferThe content to write.
offsetOffset from which we start writing in the file.
nbyteThe number of bytes to write.
Returns
The number of written bytes.

Variable Documentation

◆ procfs_file_system_type

file_system_type procfs_file_system_type
static
Initial value:
= {
.name = "procfs",
.fs_flags = 0,
}
static vfs_file_t * procfs_mount_callback(const char *path, const char *device)
Mounts the filesystem at the given path.
Definition: procfs.c:842

Filesystem information.

◆ procfs_fs_operations

vfs_file_operations_t procfs_fs_operations
static
Initial value:
= {
.open_f = procfs_open,
.unlink_f = procfs_unlink,
.close_f = procfs_close,
.read_f = procfs_read,
.write_f = procfs_write,
.lseek_f = procfs_lseek,
.stat_f = procfs_fstat,
.ioctl_f = procfs_ioctl,
.getdents_f = procfs_getdents,
.readlink_f = NULL,
}
static int procfs_close(vfs_file_t *file)
Closes the given file.
Definition: procfs.c:541
static int procfs_fstat(vfs_file_t *file, stat_t *stat)
Retrieves information concerning the file at the given position.
Definition: procfs.c:688
static ssize_t procfs_read(vfs_file_t *file, char *buffer, off_t offset, size_t nbyte)
Reads from the file identified by the file descriptor.
Definition: procfs.c:590
static int procfs_unlink(const char *path)
Deletes the file at the given path.
Definition: procfs.c:555
static ssize_t procfs_write(vfs_file_t *file, const void *buffer, off_t offset, size_t nbyte)
Writes the given content inside the file.
Definition: procfs.c:609
static off_t procfs_lseek(vfs_file_t *file, off_t offset, int whence)
Repositions the file offset inside a file.
Definition: procfs.c:630
static ssize_t procfs_getdents(vfs_file_t *file, dirent_t *dirp, off_t doff, size_t count)
Reads contents of the directories to a dirent buffer, updating the offset and returning the number of...
Definition: procfs.c:747
static vfs_file_t * procfs_open(const char *path, int flags, mode_t mode)
Open the file at the given path and returns its file descriptor.
Definition: procfs.c:440
static int procfs_ioctl(vfs_file_t *file, int request, void *data)
Perform the I/O control operation specified by REQUEST on FD. One argument may follow; its presence a...
Definition: procfs.c:726
#define NULL
Define NULL.
Definition: stddef.h:10

Filesystem file operations.

◆ procfs_sys_operations

vfs_sys_operations_t procfs_sys_operations
static
Initial value:
= {
.mkdir_f = procfs_mkdir,
.rmdir_f = procfs_rmdir,
.stat_f = procfs_stat,
.creat_f = NULL,
.symlink_f = NULL,
}
static int procfs_rmdir(const char *path)
Removes a directory.
Definition: procfs.c:402
static int procfs_stat(const char *path, stat_t *stat)
Retrieves information concerning the file at the given position.
Definition: procfs.c:706
static int procfs_mkdir(const char *path, mode_t mode)
Creates a new directory.
Definition: procfs.c:366

Filesystem general operations.