Answered step by step
Verified Expert Solution
Link Copied!

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 per-process 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 2.15.2: The /proc file-system kernel module, Part 1
#include
#include
#include
#include
#include
#define BUFFER_SIZE 128
#define PROC_NAME "hello"
ssize_t proc_read(struct file *file, char __user *usr_buf,
size_t count, loff_t *pos);
static struct file_operations proc_ops ={
.owner = THIS_MODULE,
.read = proc_read,
};
/* This function is called when the module is loaded. */
int proc_init(void)
{
/* creates the /proc/hello entry */
proc_create(PROC_NAME, 0666, NULL, &proc_ops);
return 0;
}
/* This function is called when the module is removed. */
void proc_exit(void)
{
/* removes the /proc/hello entry */
remove_proc_entry(PROC_NAME, NULL);
}
Feedback?
Figure 2.15.3: The /proc file system kernel module, Part 2
/* This function is called each time /proc/hello is read */
ssize_t proc_read(struct file *file, char __user *usr_buf,
size_t count, loff_t *pos)
{
int rv =0;
char buffer[BUFFER_SIZE];
static int completed =0;
if (completed){
completed =0;
return 0;
}
completed =1;
rv = sprintf(buffer, "Hello World
");
/* copies kernel space buffer to user space usr_buf */
copy_to_user(usr_buf, buffer, rv);
return rv;
}
module_init(proc_init);
module_exit(proc_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello Module");
MODULE_AUTHOR("SGG");
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 /proc/hello. If a user enters the command
cat /proc/hello
the infamous Hello World message is returned.
In the module entry point proc_init(), we create the new /proc/hello entry using the proc_create() function. This function is passed proc_ops, which contains a reference to a struct file_operations. This struct initializes the .owner and .read members. The value of .read is the name of the function proc_read() that is to be called whenever /proc/hello is read.
Examining this proc_read() function, we see that the string "Hello World
" is written to the variable buffer where buffer exists in kernel memory. Since /proc/hello can be accessed from user space, we must copy the contents of buffer to user space using the kernel function copy_to_user(). This function copies the contents of kernel memory buffer to the variable usr_buf, which exists in user space.
Each time the /proc/hello file is read, the proc_read() function is called repeatedly until it returns 0, so there must be logic to ensure that this function returns 0 once it has collected the data (in this case, the string "Hello World
") that is to go into the corresponding /proc/hello file.
Finally, notice that the /proc/hello file is removed in the module exit point proc_exit() using the function remove_proc_entry().
IV. Assignment
This assignment will involve designing two kernel modules:
Design a kernel module that creates a /proc file named /proc/jiffies that reports the current value of jiffies when the /proc/jiffies file is read, such as with the command
cat /proc/jiffies
Be sure to remove /proc/jiffies when the module is removed.
Design a kernel module that creates a proc file named /proc/seconds 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 /proc/seconds
your kernel module will report the number of seconds that have elapsed since the kernel module was first loaded. Be sure to remove /proc/seconds when the module is removed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

Find the next three terms in this sequence: 16, 40, 100, 250, . . .

Answered: 1 week ago

Question

Define the terms debit and credit. P=59

Answered: 1 week ago