Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The / proc file system The / proc file system is a pseudo file system that exists only in kernel memory and is used primarily
The proc file system
The proc file system is a "pseudo" file system that exists only in kernel memory and is used primarily for querying various kernel and perprocess statistics. This exercise involves designing kernel modules that create additional entries in the proc file system involving both kernel statistics and information related to specific processes. The entire program is included in the figures below.
Figure : The proc filesystem kernel module, Part
#include
#include
#include
#include
#include
#define BUFFERSIZE
#define PROCNAME "hello"
ssizet procreadstruct file file char user usrbuf,
sizet count, lofft pos;
static struct fileoperations procops
owner THISMODULE,
read procread,
;
This function is called when the module is loaded.
int procinitvoid
creates the prochello entry
proccreatePROCNAME, NULL, &procops;
return ;
This function is called when the module is removed.
void procexitvoid
removes the prochello entry
removeprocentryPROCNAME, NULL;
Feedback?
Figure : The proc file system kernel module, Part
This function is called each time prochello is read
ssizet procreadstruct file file char user usrbuf,
sizet count, lofft pos
int rv ;
char bufferBUFFERSIZE;
static int completed ;
if completed
completed ;
return ;
completed ;
rv sprintfbuffer "Hello World
;
copies kernel space buffer to user space usrbuf
copytouserusrbuf, buffer, rv;
return rv;
moduleinitprocinit;
moduleexitprocexit;
MODULELICENSEGPL;
MODULEDESCRIPTIONHello Module";
MODULEAUTHORSGG;
Feedback?
We begin by describing how to create a new entry in the proc file system. The following program example named hello.c and available with the source code for this text creates a proc entry named prochello If a user enters the command
cat prochello
the infamous Hello World message is returned.
In the module entry point procinit we create the new prochello entry using the proccreate function. This function is passed procops, which contains a reference to a struct fileoperations. This struct initializes the owner and read members. The value of read is the name of the function procread that is to be called whenever prochello is read.
Examining this procread function, we see that the string "Hello World
is written to the variable buffer where buffer exists in kernel memory. Since prochello can be accessed from user space, we must copy the contents of buffer to user space using the kernel function copytouser This function copies the contents of kernel memory buffer to the variable usrbuf, which exists in user space.
Each time the prochello file is read, the procread function is called repeatedly until it returns so there must be logic to ensure that this function returns once it has collected the data in this case, the string "Hello World
that is to go into the corresponding prochello file.
Finally, notice that the prochello file is removed in the module exit point procexit using the function removeprocentry
IV Assignment
This assignment will involve designing two kernel modules:
Design a kernel module that creates a proc file named procjiffies that reports the current value of jiffies when the procjiffies file is read, such as with the command
cat procjiffies
Be sure to remove procjiffies when the module is removed.
Design a kernel module that creates a proc file named procseconds that reports the number of elapsed seconds since the kernel module was loaded. This will involve using the value of jiffies as well as the HZ rate. When a user enters the command
cat procseconds
your kernel module will report the number of seconds that have elapsed since the kernel module was first loaded. Be sure to remove procseconds when the module is removed.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started