procfs Demo
Description
Demonstrates the /proc filesystem interface for exposing kernel information and accepting commands.
Features
- Directory creation (/proc/demo/)
- Read-only files (info, stats)
- Write-only files (control)
- seq_file interface for complex output
- Runtime control via echo commands
Build & Test
make
sudo insmod procfs_demo.ko
# View information
cat /proc/demo/info
cat /proc/demo/stats
# Send commands
echo "enable" | sudo tee /proc/demo/control
echo "disable" | sudo tee /proc/demo/control
echo "reset" | sudo tee /proc/demo/control
# Watch info change
watch -n 1 cat /proc/demo/info
# Cleanup
sudo rmmod procfs_demo
/proc Entries Created
| Path | Mode | Purpose |
|---|---|---|
| /proc/demo/ | directory | Container |
| /proc/demo/info | 0444 (r–r–r–) | Device information |
| /proc/demo/stats | 0444 (r–r–r–) | Statistics (seq_file) |
| /proc/demo/control | 0200 (-w——-) | Command interface |
Commands
enable- Enable devicedisable- Disable devicereset- Reset counter
Example Session
$ sudo insmod procfs_demo.ko
$ cat /proc/demo/info
Device Information
==================
Enabled: no
Count: 0
Status: idle
Jiffies: 4294892314
$ echo "enable" | sudo tee /proc/demo/control
$ cat /proc/demo/info
Device Information
==================
Enabled: yes
Count: 1
Status: enabled
Jiffies: 4294893142
$ cat /proc/demo/stats
=== Device Statistics ===
Boot time: 17179569
Operations: 1
Status: enabled
HZ: 250
PAGE_SIZE: 4096
Configuration:
Enabled: yes
Learning Points
Simple vs seq_file
- Simple read: Small, fixed output (info file)
- seq_file: Multi-line, complex output (stats file)
- seq_file handles pagination automatically
File Permissions
- 0444 (r–r–r–): Read by all
- 0200 (-w——-): Write by root only
- 0644 (rw-r–r–): Read all, write root
Why Use /proc?
- Debugging: Expose internal state
- Monitoring: Runtime statistics
- Configuration: Runtime control
- User-friendly: Standard tools (cat, echo)
Common Uses
- Device status reporting
- Performance statistics
- Debug information
- Runtime configuration
- Kernel module parameters
Modern Alternative: sysfs
For new code, prefer sysfs (/sys) over procfs:
- procfs: Process information, legacy interfaces
- sysfs: Device and driver information
Advanced: seq_file Operations
static const struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show,
};
Used for iterating over data structures.