AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
procfs.c File Reference

procfs: the kernel's and the processes' state as files, usually mounted on /proc. More...

#include <core/system.h>
#include <core/arch.h>
#include <core/module.h>
#include <core/string.h>
#include <core/time.h>
#include <aquila/errno.h>
#include <boot/boot.h>
#include <fs/vfs.h>
#include <fs/iobuf.h>
#include <fs/devfs.h>
#include <fs/posix.h>
#include <fs/vcache.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <mm/vm.h>
#include <mm/buddy.h>
#include <ds/buddy.h>

Go to the source code of this file.

Data Structures

struct  procfs_entry
 A file of the top directory: its name, and what makes its text. More...
struct  procfs_proc_entry
 A file of a process's directory: its name, and what makes its text. More...

Macros

#define PROCFS_ENTRIES   (sizeof(entries)/sizeof(entries[0]))
#define PROCFS_PROC_ENTRIES   (sizeof(proc_entries)/sizeof(proc_entries[0]))
#define PROC_INO(pid)
#define INO_PID(ino)

Functions

static ssize_t procfs_meminfo (off_t off, size_t size, char *buf)
 Makes /proc/meminfo: the memory there is, and how it's used.
static ssize_t procfs_kvmem (off_t off, size_t size, char *buf)
 Makes /proc/kvmem: the kernel's allocations, by malloc type.
static ssize_t procfs_version (off_t off, size_t size, char *buf)
 Makes /proc/version: the kernel's name and version.
static ssize_t procfs_uptime (off_t off, size_t size, char *buf)
 Makes /proc/uptime: the time since boot.
int arch_interrupts_info (char *buf, size_t size)
static ssize_t procfs_interrupts (off_t off, size_t size, char *buf)
 Makes /proc/interrupts: the interrupt controller and the IRQs, as the architecture describes them.
static ssize_t procfs_filesystems (off_t off, size_t size, char *buf)
 Makes /proc/filesystems: the installed filesystems.
static ssize_t procfs_cmdline (off_t off, size_t size, char *buf)
 Makes /proc/cmdline: the kernel's command line.
static ssize_t procfs_rdcmdline (off_t off, size_t size, char *buf)
 Makes /proc/rdcmdline: the RAM disk module's command line.
static ssize_t procfs_mounts (off_t off, size_t size, char *buf)
 Makes /proc/mounts: the mounted filesystems.
static ssize_t procfs_buddyinfo (off_t off, size_t size, char *buf)
 Makes /proc/buddyinfo: the buddy allocator's free blocks, by size.
static ssize_t procfs_devices (off_t off, size_t size, char *buf)
 Makes /proc/devices: the registered device drivers.
static ssize_t procfs_proc_status (int pid, off_t off, size_t size, void *buf)
 Makes a process's status: its IDs, its state and its threads'.
static ssize_t procfs_proc_maps (int pid, off_t off, size_t size, void *buf)
 Makes a process's maps: its mappings, as Linux's /proc/<pid>/maps shows them.
static ssize_t procfs_proc_vmstats (int pid, off_t off, size_t size, void *buf)
 Makes a process's vmstats: its mappings and their vm objects.
static ssize_t procfs_entry_read (struct vnode *node, off_t offset, size_t size, char *buf)
 Makes the text of a file of procfs, from an offset, into a kernel buffer.
static ssize_t procfs_read (struct vnode *node, off_t offset, struct iobuf *io)
 Reads a file of procfs: its text is made a page at a time, and copied out through the iobuf.
static ssize_t procfs_readdir (struct vnode *vnode, off_t offset, struct dirent *dirent)
 Reads a directory's entry at an offset.
static int procfs_finddir (struct vnode *parent, const char *name, struct dirent *dirent)
 Finds the entry of a directory that has a name, and its inode number.
static int procfs_vget (struct vnode *super, ino_t ino, struct vnode **vnode)
 Gets the vnode of an inode number, making it if it isn't in use.
static int procfs_close (struct vnode *vnode)
 Frees a vnode that has no references left; the root stays.
static int procfs_init ()
 Sets procfs up at boot: its root directory, and its vnode cache.
static int procfs_mount (struct vnode *dir, int flags, void *data)
 Mounts procfs on a directory: its one root directory is bound there.

Variables

struct fs procfs
struct vnode * procfs_root = NULL
struct vcache procfs_vcache = {0}
static struct procfs_entry entries []
static struct procfs_proc_entry proc_entries []

Detailed Description

procfs: the kernel's and the processes' state as files, usually mounted on /proc.

Its files hold no data: reading one makes its text then, a snapshot. The top directory has the system's files (meminfo, mounts, ...), self, and a directory for each process, named by its ID, with the process's files (status, maps, vmstats). Vnodes are made when they are looked up, and their inode numbers say what they are: a system file's is negative, and a process's directory and its files have consecutive numbers from the process's ID.

Definition in file procfs.c.

Macro Definition Documentation

◆ INO_PID

#define INO_PID ( ino)
Value:
((ino) / (PROCFS_PROC_ENTRIES + 1) - 1)
#define PROCFS_PROC_ENTRIES
Definition procfs.c:519

Definition at line 524 of file procfs.c.

Referenced by procfs_entry_read(), procfs_finddir(), procfs_readdir(), and procfs_vget().

◆ PROC_INO

#define PROC_INO ( pid)
Value:
(((pid) + 1) * (PROCFS_PROC_ENTRIES + 1))

Definition at line 523 of file procfs.c.

Referenced by procfs_finddir().

◆ PROCFS_ENTRIES

#define PROCFS_ENTRIES   (sizeof(entries)/sizeof(entries[0]))

Definition at line 334 of file procfs.c.

Referenced by procfs_entry_read(), procfs_finddir(), procfs_readdir(), and procfs_vget().

◆ PROCFS_PROC_ENTRIES

#define PROCFS_PROC_ENTRIES   (sizeof(proc_entries)/sizeof(proc_entries[0]))

Definition at line 519 of file procfs.c.

Referenced by procfs_entry_read(), procfs_finddir(), procfs_readdir(), and procfs_vget().

Function Documentation

◆ arch_interrupts_info()

◆ procfs_buddyinfo()

ssize_t procfs_buddyinfo ( off_t off,
size_t size,
char * buf )
static

Makes /proc/buddyinfo: the buddy allocator's free blocks, by size.

Definition at line 250 of file procfs.c.

References buddies, BUDDY_MAX_ORDER, BUDDY_ZONE_NR, memcpy(), MIN, procfs_buddyinfo(), snprintf(), and buddy::usable.

Referenced by procfs_buddyinfo().

◆ procfs_close()

int procfs_close ( struct vnode * vnode)
static

Frees a vnode that has no references left; the root stays.

Definition at line 770 of file procfs.c.

References vnode::ino, kfree(), procfs_close(), procfs_root, procfs_vcache, vnode::ref, vcache_find(), vcache_remove(), and vnode_pages_free().

Referenced by procfs_close().

◆ procfs_cmdline()

ssize_t procfs_cmdline ( off_t off,
size_t size,
char * buf )
static

Makes /proc/cmdline: the kernel's command line.

Definition at line 185 of file procfs.c.

References __kboot, boot::cmdline, memcpy(), MIN, procfs_cmdline(), and snprintf().

Referenced by procfs_cmdline().

◆ procfs_devices()

ssize_t procfs_devices ( off_t off,
size_t size,
char * buf )
static

Makes /proc/devices: the registered device drivers.

Definition at line 283 of file procfs.c.

References blkdev, chrdev, memcpy(), MIN, dev::name, procfs_devices(), and snprintf().

Referenced by procfs_devices().

◆ procfs_entry_read()

ssize_t procfs_entry_read ( struct vnode * node,
off_t offset,
size_t size,
char * buf )
static

Makes the text of a file of procfs, from an offset, into a kernel buffer.

Returns how many bytes there are, or a negative errno.

Definition at line 528 of file procfs.c.

References entries, vnode::ino, INO_PID, vnode::p, proc_entries, PROCFS_ENTRIES, procfs_entry_read(), PROCFS_PROC_ENTRIES, procfs_entry::read, and procfs_proc_entry::read.

Referenced by procfs_entry_read(), and procfs_read().

◆ procfs_filesystems()

ssize_t procfs_filesystems ( off_t off,
size_t size,
char * buf )
static

Makes /proc/filesystems: the installed filesystems.

Definition at line 161 of file procfs.c.

References memcpy(), MIN, fs::name, fs::nodev, procfs_filesystems(), registered_fs, and snprintf().

Referenced by procfs_filesystems().

◆ procfs_finddir()

int procfs_finddir ( struct vnode * parent,
const char * name,
struct dirent * dirent )
static

Finds the entry of a directory that has a name, and its inode number.

Definition at line 646 of file procfs.c.

References curproc, entries, INO_PID, proc::name, proc::parent, proc::pid, proc_entries, PROC_INO, PROCFS_ENTRIES, procfs_finddir(), PROCFS_PROC_ENTRIES, procfs_root, procs, queue_for, and snprintf().

Referenced by procfs_finddir().

◆ procfs_init()

int procfs_init ( )
static

Sets procfs up at boot: its root directory, and its vnode cache.

Definition at line 789 of file procfs.c.

References vnode::atime, vnode::ctime, vnode::fs, gettime(), vnode::ino, kmalloc(), M_ZERO, vnode::mode, vnode::mtime, procfs, procfs_init(), procfs_root, procfs_vcache, vnode::ref, vcache_init(), and vfs_install().

Referenced by procfs_init().

◆ procfs_interrupts()

ssize_t procfs_interrupts ( off_t off,
size_t size,
char * buf )
static

Makes /proc/interrupts: the interrupt controller and the IRQs, as the architecture describes them.

Definition at line 144 of file procfs.c.

References arch_interrupts_info(), memcpy(), MIN, and procfs_interrupts().

Referenced by procfs_interrupts().

◆ procfs_kvmem()

ssize_t procfs_kvmem ( off_t off,
size_t size,
char * buf )
static

Makes /proc/kvmem: the kernel's allocations, by malloc type.

Definition at line 76 of file procfs.c.

References malloc_types, memcpy(), MIN, malloc_type::name, malloc_type::nr, procfs_kvmem(), queue_for, snprintf(), and malloc_type::total.

Referenced by procfs_kvmem().

◆ procfs_meminfo()

ssize_t procfs_meminfo ( off_t off,
size_t size,
char * buf )
static

Makes /proc/meminfo: the memory there is, and how it's used.

Definition at line 48 of file procfs.c.

References k_total_mem, k_used_mem, kvmem_obj_cnt, kvmem_used, memcpy(), MIN, procfs_meminfo(), slab_used, and snprintf().

Referenced by procfs_meminfo().

◆ procfs_mount()

int procfs_mount ( struct vnode * dir,
int flags,
void * data )
static

Mounts procfs on a directory: its one root directory is bound there.

Returns 0, or -EBUSY if something is mounted on the directory, or procfs is mounted elsewhere already.

Definition at line 815 of file procfs.c.

References procfs_mount(), procfs_root, and vfs_bind().

Referenced by procfs_mount().

◆ procfs_mounts()

ssize_t procfs_mounts ( off_t off,
size_t size,
char * buf )
static

Makes /proc/mounts: the mounted filesystems.

Definition at line 224 of file procfs.c.

References mountpoint::dev, memcpy(), MIN, mounts, mountpoint::options, mountpoint::path, procfs_mounts(), queue_for, snprintf(), and mountpoint::type.

Referenced by procfs_mounts().

◆ procfs_proc_maps()

ssize_t procfs_proc_maps ( int pid,
off_t off,
size_t size,
void * buf )
static

◆ procfs_proc_status()

ssize_t procfs_proc_status ( int pid,
off_t off,
size_t size,
void * buf )
static

◆ procfs_proc_vmstats()

ssize_t procfs_proc_vmstats ( int pid,
off_t off,
size_t size,
void * buf )
static

◆ procfs_rdcmdline()

ssize_t procfs_rdcmdline ( off_t off,
size_t size,
char * buf )
static

Makes /proc/rdcmdline: the RAM disk module's command line.

Definition at line 203 of file procfs.c.

References __kboot, boot::cmdline, module_t::cmdline, memcpy(), MIN, boot::modules, boot::modules_count, procfs_rdcmdline(), and snprintf().

Referenced by procfs_rdcmdline().

◆ procfs_read()

ssize_t procfs_read ( struct vnode * node,
off_t offset,
struct iobuf * io )
static

Reads a file of procfs: its text is made a page at a time, and copied out through the iobuf.

Definition at line 547 of file procfs.c.

References iobuf_out(), kfree(), kmalloc(), MIN, PAGE_SIZE, procfs_entry_read(), procfs_read(), and iobuf::resid.

Referenced by procfs_read().

◆ procfs_readdir()

ssize_t procfs_readdir ( struct vnode * vnode,
off_t offset,
struct dirent * dirent )
static

Reads a directory's entry at an offset.

The entries are . and .., then the top directory's files, self and the processes, or a process's files.

Definition at line 582 of file procfs.c.

References entries, vnode::ino, INO_PID, procfs_entry::name, procfs_proc_entry::name, proc::pid, proc_entries, PROCFS_ENTRIES, PROCFS_PROC_ENTRIES, procfs_readdir(), procfs_root, procs, queue_for, and snprintf().

Referenced by procfs_readdir().

◆ procfs_uptime()

ssize_t procfs_uptime ( off_t off,
size_t size,
char * buf )
static

Makes /proc/uptime: the time since boot.

Definition at line 124 of file procfs.c.

References arch_rtime_ms(), memcpy(), MIN, procfs_uptime(), and snprintf().

Referenced by procfs_uptime().

◆ procfs_version()

ssize_t procfs_version ( off_t off,
size_t size,
char * buf )
static

Makes /proc/version: the kernel's name and version.

Definition at line 102 of file procfs.c.

References __CONFIG_COMPILER__, __CONFIG_COMPILER_VERSION__, __CONFIG_TIMESTAMP__, memcpy(), MIN, procfs_version(), snprintf(), UTSNAME_RELEASE, and UTSNAME_SYSNAME.

Referenced by procfs_version().

◆ procfs_vget()

int procfs_vget ( struct vnode * super,
ino_t ino,
struct vnode ** vnode )
static

Gets the vnode of an inode number, making it if it isn't in use.

Definition at line 692 of file procfs.c.

References vnode::atime, vnode::ctime, vnode::fs, gettime(), vnode::ino, INO_PID, kmalloc(), M_ZERO, vnode::mode, vnode::mtime, NULL, vnode::p, procfs, PROCFS_ENTRIES, PROCFS_PROC_ENTRIES, procfs_root, procfs_vcache, procfs_vget(), vcache_find(), and vcache_insert().

Referenced by procfs_vget().

Variable Documentation

◆ entries

struct procfs_entry entries[]
static
Initial value:
= {
{"meminfo", procfs_meminfo},
{"cmdline", procfs_cmdline},
{"rdcmdline", procfs_rdcmdline},
{"version", procfs_version},
{"uptime", procfs_uptime},
{"interrupts", procfs_interrupts},
{"filesystems", procfs_filesystems},
{"buddyinfo", procfs_buddyinfo},
{"kvmem", procfs_kvmem},
{"mounts", procfs_mounts},
{"devices", procfs_devices},
}
static ssize_t procfs_interrupts(off_t off, size_t size, char *buf)
Makes /proc/interrupts: the interrupt controller and the IRQs, as the architecture describes them.
Definition procfs.c:144
static ssize_t procfs_buddyinfo(off_t off, size_t size, char *buf)
Makes /proc/buddyinfo: the buddy allocator's free blocks, by size.
Definition procfs.c:250
static ssize_t procfs_rdcmdline(off_t off, size_t size, char *buf)
Makes /proc/rdcmdline: the RAM disk module's command line.
Definition procfs.c:203
static ssize_t procfs_kvmem(off_t off, size_t size, char *buf)
Makes /proc/kvmem: the kernel's allocations, by malloc type.
Definition procfs.c:76
static ssize_t procfs_devices(off_t off, size_t size, char *buf)
Makes /proc/devices: the registered device drivers.
Definition procfs.c:283
static ssize_t procfs_cmdline(off_t off, size_t size, char *buf)
Makes /proc/cmdline: the kernel's command line.
Definition procfs.c:185
static ssize_t procfs_meminfo(off_t off, size_t size, char *buf)
Makes /proc/meminfo: the memory there is, and how it's used.
Definition procfs.c:48
static ssize_t procfs_filesystems(off_t off, size_t size, char *buf)
Makes /proc/filesystems: the installed filesystems.
Definition procfs.c:161
static ssize_t procfs_uptime(off_t off, size_t size, char *buf)
Makes /proc/uptime: the time since boot.
Definition procfs.c:124
static ssize_t procfs_mounts(off_t off, size_t size, char *buf)
Makes /proc/mounts: the mounted filesystems.
Definition procfs.c:224
static ssize_t procfs_version(off_t off, size_t size, char *buf)
Makes /proc/version: the kernel's name and version.
Definition procfs.c:102

Definition at line 320 of file procfs.c.

Referenced by procfs_entry_read(), procfs_finddir(), and procfs_readdir().

◆ proc_entries

struct procfs_proc_entry proc_entries[]
static
Initial value:
= {
{"status", procfs_proc_status},
{"maps", procfs_proc_maps},
{"vmstats", procfs_proc_vmstats},
}
static ssize_t procfs_proc_status(int pid, off_t off, size_t size, void *buf)
Makes a process's status: its IDs, its state and its threads'.
Definition procfs.c:337
static ssize_t procfs_proc_maps(int pid, off_t off, size_t size, void *buf)
Makes a process's maps: its mappings, as Linux's /proc/<pid>/maps shows them.
Definition procfs.c:403
static ssize_t procfs_proc_vmstats(int pid, off_t off, size_t size, void *buf)
Makes a process's vmstats: its mappings and their vm objects.
Definition procfs.c:458

Definition at line 513 of file procfs.c.

Referenced by procfs_entry_read(), procfs_finddir(), and procfs_readdir().

◆ procfs

struct fs procfs
Initial value:
= {
.name = "procfs",
.nodev = 1,
.init = procfs_init,
.mount = procfs_mount,
.vops = {
.read = procfs_read,
.readdir = procfs_readdir,
.finddir = procfs_finddir,
.vget = procfs_vget,
.close = procfs_close,
},
.fops = {
.open = posix_file_open,
.read = posix_file_read,
.readdir = posix_file_readdir,
.close = posix_file_close,
.eof = __vfs_eof_always,
},
}
int posix_file_open(struct file *file)
Finishes opening a file: with O_TRUNC, a regular file is truncated to 0 bytes.
Definition open.c:17
int posix_file_close(struct file *file)
Closes an open file by dropping its reference to its vnode.
Definition close.c:15
ssize_t posix_file_read(struct file *file, struct iobuf *io)
Reads from an open file at its offset, and moves the offset past what was read.
Definition read.c:29
ssize_t posix_file_readdir(struct file *file, struct dirent *dirent)
Reads the directory entry at an open directory's offset, and moves the offset past it.
Definition readdir.c:22
static int procfs_vget(struct vnode *super, ino_t ino, struct vnode **vnode)
Gets the vnode of an inode number, making it if it isn't in use.
Definition procfs.c:692
static int procfs_finddir(struct vnode *parent, const char *name, struct dirent *dirent)
Finds the entry of a directory that has a name, and its inode number.
Definition procfs.c:646
static ssize_t procfs_read(struct vnode *node, off_t offset, struct iobuf *io)
Reads a file of procfs: its text is made a page at a time, and copied out through the iobuf.
Definition procfs.c:547
static int procfs_mount(struct vnode *dir, int flags, void *data)
Mounts procfs on a directory: its one root directory is bound there.
Definition procfs.c:815
static int procfs_close(struct vnode *vnode)
Frees a vnode that has no references left; the root stays.
Definition procfs.c:770
static ssize_t procfs_readdir(struct vnode *vnode, off_t offset, struct dirent *dirent)
Reads a directory's entry at an offset.
Definition procfs.c:582
static int procfs_init()
Sets procfs up at boot: its root directory, and its vnode cache.
Definition procfs.c:789

Definition at line 35 of file procfs.c.

Referenced by procfs_init(), and procfs_vget().

◆ procfs_root

struct vnode* procfs_root = NULL

◆ procfs_vcache

struct vcache procfs_vcache = {0}

Definition at line 45 of file procfs.c.

Referenced by procfs_close(), procfs_init(), and procfs_vget().