Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It is time to move from programming with system calls to actually creating kerne

ID: 3816659 • Letter: I

Question

It is time to move from programming with system calls to actually creating kernel code. In this project you will create a kernel module for linux version you had setup as per the instructions online. Your module will be defined in a file named CMPE142-km.c and do the following:

1.-When your kernel module is loaded it will print out to /var/log/messages the following message (verbatim):

"CMPE142-km loaded!"

2.-When the kernel module is removed it will print the following message (verbatim) in the same log file as above:

"CMPE142-km removed!"

3.-While loaded, your module will add the file CMPE142-km to the "proc" file system and act as an 'echo' file, anything written should be read back as it was written. I.e. if I do echo "This is a km test" > /proc/CMP142-km and then do cat /proc/CMPE142-km, you should see the following text in the screen: "This is a km test". Further read from the file should find the file empty.

Please write the code in C

Explanation / Answer

answer:

1)

/*

*"CMPE142-km.c"   -   The simplest kernel module.

*/

#include<linux/module.h>     /*Needed by all modules*/

#include<linux/kernek.h>      /*Needed for KERN_INFO*/

int init_module(void)

{

    printk(KERN_INFO "verbatim. ");

    return 0;

}

void cleanup_module(void)

{

    printk(KERN_INFO "verbatim");

}

Firsty,you should check that whether your module is prperly loaded or not,by using this command,

       lsmod | grep "CMPE142-km" //CMPE142-km is the name of your mdule

Since wrote a kernal module,which prints some message.The messages from kernal and its module can be found

in /var/log/messages or you can view these kind of messages using dmesg command.AS module prints "verbatim".

       dmesg |grep "CMPE142-km"

2answer):

Now in above programme we have CMPE142-km file,we can insert this module to kernel by sing insmod command as shown below.

   #inmd CMPE142-km

   #dmesg | tail -1

    [8394.731865]verbatim

#rmmd CMPE142-km

#dmesg | tail -1

[8707.989819]Cleaning up module

when a module is inserted into kernal, the mdule_init macro will be invoked, which will call the function CMPE142-km. Similaly, when the module is removed with rmmod,module_exit macr will be invoked, ehich will call the CMPE142-km_exit. Using dmesg command,we can see the output from the sample kernel module.

3answer):

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<errno.h>

#include<sys/types.h>

#include<unistd>

#define Buf_size 8192

int main(int argc,char* argv[])

{

   int CMPE142-km,proc;   /*Input and output file descriptors */

   ssize_t ret_in,ret_out; /*Number f bytes returned by read() and write()*/

    char buffer[BUF_SIZE];                                 /*Character buffer*/

    /*Are src and dest file name arguments missing */

    if(argc!=3)

    {

       printf("Usage:cp file1 file2");

      return 1;

    }

/*Create input file descriptor*/

CMPE142-km_fd=open(argv[1],O_RDNLY);

   if(CMPE142-km_fd==-1)

     {

       perror("open");

       return 2;

     }

   /*Create output file descriptor*/

   proc_fd=pen(arg[2],O_WRONLY | O_CREATE,0644);

     if(proc_fd==-1)

     {

       perror("open");

       return 3;

    }

/*Copy process*/

   while((ret_in=read(CMPE142-km_fd,&buffer,BUF_SIZE))>0)

    {

       ret_out=write(proc_fd,&buffer,((ssize_t) ret_in);

        if(ret_out!=ret_in)

         {

           /*write error*/

           perror("write");

           return 4;

       }

}

/*Close file descriptor*/

   close(CMPE142-km_fd);

   closeprc_fd);

return(EXIT_SUCCESS);

}

Then if your souce file is named CMPE142-km.txt and if you want to name destination file is proc.txt then you will compile like this:

   ./sp_linux_copy CMPE142-km.txt proc.txt

in output you will write message in source file is "This is a km test"

then it diplays destination message is "This is a km test"

    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote