Click here to load reader
Upload
ziming-hu
View
804
Download
1
Embed Size (px)
Citation preview
Using and Programming Proc filesystem
from internet ...
Content
● What's Procfs
● Basic useness of Procfs
● Programming Procfs
● Example source code
● Conclusion
What is Procfs
● In its simplest form procfs provides file-system abstraction for processes, presenting information about processes, their execution environment, resource utilization etc. as files.
● The original design goal of procfs was to make debugger implementation easier.
● Systems like Linux have exposed a lot of other system interfaces and information through procfs
● There are Linux system utilities that essentially cat files in procfs!
What is Procfs (cont.)
● It is a virtual file system
– Is a special file system in the linux kernel.
– It exists only in memory.
– Is a virtual file system that is not associated with a block device but exists only in memory.
– allows programs in the user space to access to certain information from the kernel.
What is Procfs (cont.)
● It is an abstraction allowing
– access information on
– configure the kernel
● It can be mounted by
# mount -t proc proc /mnt
Now procfs is mounted on /mnt, same to /proc
Basic useness of Procfs
● /proc/sys are sysctl files
– They do not belong to procfs
– Managed by different APIs
● Number named Dirs
– PID as name
– Appear and disappear as processes are created, finish
Basic useness of Procfs (cont.)● Structure
– cmdline : argument list, null-separated strings
– cwd : symbolic link to the current working directory
– environ : environment for the process
– exe : a symbolic link containing the actual path name of the executed command
– fd : a subdirectory containing one entry for each file which the process has open (file descriptor), a symbolic link to the actual file)
– maps : currently mapped memory regions and their access permissions
– root : points to the file system root, e.g /
– stat : information about the process (used by ps)
– statm : information about memory status in pages
– status : much of the information in stat and statm in a more human readable/parsable format
Basic useness of Procfs (cont.)● /proc/self: symbolic link to the current process
● /proc/cpuinfo: a collection of CPU and system architecture dependent items, for each supported architecture a different list
● /proc/devices: listing of major numbers and device groups
● /proc/pci: listing of all PCI devices found during kernel initialization and their configuration (lspci command)
● /proc/tty/driver/serial: information on serial ports
● /proc/version: information on the kernel version
– /proc/sys/kernel/ostype
– /proc/sys/kernel/osrelease
– /proc/sys/kernel/version
Basic useness of Procfs (cont.)● /proc/sys/kernel/hostname: the name of the machine
● /proc/sys/kernel/domainname: the name of the domain
● /proc/meminfo: information on memory usage
● /proc/loadavg: load average numbers give information on the workload:
– the number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes
– PID of the last process being executed
● /proc/uptime: two pieces of information on the uptime of the system (seconds), and the amount of time spent in idle process (seconds)
– Same information as uptime
Programming Procfs● Program as kernel module
● #includeu <linux/proc_fs.h>
● struct proc_dir_entry* proc_mkdir
(const char* name,struct proc_dir_entry* parent);
● struct proc_dir_entry* create_proc_entry
(const char* name, mode_t mode,struct proc_dir_entry* parent);
● struct proc_dir_entry* proc_symlink
(const char* name,struct proc_dir_entry* parent,
const char *dest);
● void remove_proc_entry
(const char *name, struct proc_dir_entry *parent);
Programming Procfs (cont.)● procfs works with call back functionsfor files:
functions that are called when a specific file is being read or written.
● Such functions have to be initialized by setting the file_operations member in the struct proc_dir_entry* that the function create_proc_entry returned.
● For read only proc file, call create_proc_read_entry is enough.
Programming Procfs (cont.)● define read, write, open, release methods for your proc file
see example source code.
● Original procfs can not pass data larger than 1 page. In order to pass large data, you should define a sequence method.
● Other detailed example, see the source code.
Example source code● kernel module frame work.
● read the source code for each method of proc fs.
● build it
● insmod into kernel
● cd /proc, see /proc/foo and /proc/foo/foo
● try read/write permission
● be root user
● try read/write permission
● rmmod the kernel module
Conclusion● Its candidate is sysfs.
● But Procfs is still very useful, because it is simple, and lot of legacy software need procfs.
Thank you !
Q & A