Create a \"proc\" file entry called myProcessMem. You should write to myProccssM
ID: 3583984 • Letter: C
Question
Create a "proc" file entry called myProcessMem. You should write to myProccssMem the process identification of a running process. Later, when you read from myProcessMem it should return the following lines for each virtual memory area inside the process in the following format: start_address (size) permission Path This means: start address of the memory area size of the memory area in KB List of permissions of the memory area: read write execute private shared at the end. Three summary lines should display the total writable virtual memory areas size in KB total shared area size in KB, and total memory areas in KB.Explanation / Answer
Happy New Year
The below program will perform read and write operations with proc file.
To get the starting address of the running process, try to modify the
function - "write_data()" to add an entry.
PROGRAM:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>
#define MAX_LEN 4096
int read_data( char *page, char **start, off_t off,int count, int *eof, void *data );
ssize_t write_data( struct file *filp, const char __user *buff,unsigned long len, void *data );
static int writeIndex;
static int readIndex;
static struct proc_dir_entry *proc_entry;
static char *info;
void cleanup_module( void )
{
remove_proc_entry("myProcessMem", proc_entry);
printk(KERNEL_INFO "myProcessMem unloaded. ");
vfree(info);
}
// Function to create an entry myProcessMem in proc file.
int init_module( void )
{
int result = 0;
info = (char *)vmalloc( MAX_LEN );
memset( info, 0, MAX_LEN );
// create_proc_entry library funciton to create an entry.
// 2nd parameter here specifies the privileges for the file.
proc_entry = create_proc_entry( "myProcessMem", 0644, NULL );
if (proc_entry == NULL)
{
result = -1;
vfree(info);
printk(KERNEL_INFO "myProcessMem could not be created ");
}
else
{
writeIndex = 0;
readIndex = 0;
// Address of the read_data and write_data functions assigned to members of
// proc_entry structure.
// This is done so that code knows which function to call when proc_file is
// read and written.
proc_entry->read_proc = read_data;
proc_entry->write_proc = write_data;
printk(KERNEL_INFO "myProcessMem created. ");
}
return result;
}
// To read the proc file.
int read_data( char *page, char **start, off_t off, int count, int *eof, void *data )
{
int len;
if (off > 0)
{
*eof = 1;
return 0;
}
if (readIndex >= writeIndex)
readIndex = 0;
len = sprintf(page, "%s ", &info[readIndex]);
readIndex += len;
// Information present in the buffer is sent back to the user space.
return len;
}
// To write to the proc file.
ssize_t write_data( struct file *filp, const char __user *buff, unsigned long len, void *data )
{
int capacity = (MAX_LEN-writeIndex)+1;
// Check if there is capacity to write to the buffer.
if (len > capacity)
{
printk(KERNEL_INFO "No space to write in myProcessMem! ");
return -1;
}
// If there is capacity, copy_from_user() function is used to copy
// from user space to kernel module.
if (copy_from_user( &info[writeIndex], buff, len ))
{
return -2;
}
writeIndex += len;
info[writeIndex-1] = 0;
return len;
}
MAKE FILE:
obj-m += proc.o
all:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
COMPILING THE CODE:
$ make
sudo make -C /lib/modules/2.6.32-21-generic/build M=/home/personal modules
make: Entering directory `/usr/src/linux-headers-2.6.32-21-generic'
CC [M] /home/personal/proc.o
/home/personal/proc.c: In function ‘init_module’:
/home/personal/proc.c:33: warning: assignment from incompatible pointer type
Building modules, stage 2.
MODPOST 1 modules
LD [M] /home/personal/proc.ko
make: Leaving directory `/usr/src/linux-headers-2.6.32-21-generic'
EXECUTION:
Once the compilation is successfull, we have to load the module.
The below command will insert and load the module.
$ sudo insmod proc.ko
After inserting and loading, we can see the /proc directory to find "myProcessMem"
entry.
$ ls /proc/myProcessMem
/proc/myProcessMem
We can now read and write
$ echo "Hello" > /proc/myProcessMem
$ cat /proc/myProcessMem
Hello