Question: Project 2 Linux kernel module for task information In this project, you will write a Linux kernel module that uses the / proc file system

Project 2Linux kernel module for task information
In this project, you will write a Linux kernel module that uses the /proc file system for displaying a task's information based on its process identifier value pid. Before beginning this project, be sure you have completed the Linux kernel module programming project in the chapter Operating-System Structures, which involves creating an entry in the /proc file system. This project will involve writing a process identifier to the file /proc/pid. Once a pid has been written to the /proc file, subsequent reads from /proc/pid will report (1) the command the task is running, (2) the value of the task's pid, and (3) the current state of the task. An example of how your kernel module will be accessed once loaded into the system is as follows:
echo "1395">/proc/pid
cat /proc/pid
command =[bash] pid =[1395] state =[1]
The echo command writes the characters "1395" to the /proc/pid file. Your kernel module will read this value and store its integer equivalent as it represents a process identifier. The cat command reads from /proc/pid, where your kernel module will retrieve the three fields from the task_struct associated with the task whose pid value is 1395.
I. Writing to the /proc file system
In the kernel module project in the chapter Operating-System Structures, you learned how to read from the /proc file system. We now cover how to write to /proc. Setting the field .write in struct file_operations to
.write = proc_write
causes the proc_write() function of the figure below to be called when a write operation is made to /proc/pid
ssize_t proc_write(struct file *file, char __user *usr_buf,
size_t count, loff_t *pos)
{
int rv =0;
char *k_mem;
/* allocate kernel memory */
k_mem = kmalloc(count, GFP_KERNEL);
/* copies user space usr_buf to kernel memory */
copy_from_user(k_mem, usr_buf, count);
printk(KERN_INFO "%s
", k_mem);
/* return kernel memory */
kfree(k_mem);
return count;
}
The kmalloc() function is the kernel equivalent of the user-level malloc() function for allocating memory, except that kernel memory is being allocated. The GFP_KERNEL flag indicates routine kernel memory allocation. The copy_from_user() function copies the contents of usr_buf (which contains what has been written to /proc/pid) to the recently allocated kernel memory. Your kernel module will have to obtain the integer equivalent of this value using the kernel function kstrtol(), which has the signature
int kstrtol(const char *str, unsigned int base, long *res)
This stores the character equivalent of str, which is expressed as a base into res.
Finally, note that we return memory that was previously allocated with kmalloc() back to the kernel with the call to kfree(). Careful memory managementwhich includes releasing memory to prevent memory leaksis crucial when developing kernel-level code.
II. Reading from the /proc file system
Once the process identifier has been stored, any reads from /proc/pid will return the name of the command, its process identifier, and its state. As illustrated in Section 3.1, the PCB in Linux is represented by the structure task_struct, which is found in the include file. Given a process identifier, the function pid_task() returns the associated task_struct. The signature of this function appears as follows:
struct task_struct pid_task(struct pid *pid,
enum pid_type type)
The kernel function find_vpid(int pid) can be used to obtain the struct pid, and PIDTYPE_PID can be used as the pid_type.
For a valid pid in the system, pid_task will return its task_struct. You can then display the values of the command, pid, and state. (You will probably have to read through the task_struct structure in to obtain the names of these fields.)
If pid_task() is not passed a valid pid, it returns NULL. Be sure to perform appropriate error checking to check for this condition. If this situation occurs, the kernel module function associated with reading from /proc/pid should return 0.
In the source code download, we give the C program pid.c, which provides some of the basic building blocks for beginning this project. Submit a Word or PDF document, including a screenshot of each completed step:
I. Writing to the /proc File System (45 points)
II. Reading from the /proc File System (45 points)
III. Include in your document a screenshot of the code you had to add or modify to produce the results in parts I and II. Make sure your name appears in the output of both parts.
Include in your document the name of each student and their contribution to this assignment. (10 points)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!