Follow by Email

Total Pageviews

Tuesday, December 25, 2012

Steps to add Linux Character Device Driver


Linux Device driver

Hi we will learn step to add character device driver to Linux


1)      Create Device in /dev
$mknod  /dev/mydev c 200 10

  200- major number ->Driver Identifier
   10-minor number->Device identifier

2)      Change permission if  necessary
$chmod  xxx /dev/mydev

3)      Write Device driver Program

#include <generated/autoconf.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

char data[1024];
int mydev_open(struct inode *inode, struct file *filp)
{
   printk(KERN_ALERT "Entering mydev_open\n");
   return 0;
}
ssize_t mydev_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
{
   printk(KERN_ALERT "Entering mydev_read\n");
   copy_to_user(buf, data, count);
   return count;
}
ssize_t mydev_write(struct file *filp, const char __user *buf, size_t count,
                loff_t *f_pos)
{
   printk(KERN_ALERT "Entering mydev_write\n");
   copy_from_user(data, buf, count);
   return count;
}
loff_t mydev_llseek(struct file *filp, loff_t off, int whence)
{
   printk(KERN_ALERT "Entering mydev_llseek\n");
   return 0;
}
int mydev_release(struct inode *inode, struct file *filp)
{
   printk(KERN_ALERT "Entering mydev_release\n");
        return 0;
}
struct file_operations mydev_ops = {
   .owner = THIS_MODULE,
   .open = mydev_open,
   .read = mydev_read, 
   .write = mydev_write,
   .llseek = mydev_llseek,
   .release = mydev_release,
};

dev_t devno;
struct cdev cdev;
int mydev_init(void) {
   int res, err;
   int major, minor;
   printk(KERN_ALERT "Starting initialisation\n");

   /* Ask the kernel for a major number */
   res = alloc_chrdev_region(&devno, 10, 1, "mydev");
   if(res < 0) {
         printk(KERN_ALERT "Failed Getting Devno\n");
         return EINVAL;
   }
   major = MAJOR(devno);
   minor = MINOR(devno);
   printk(KERN_ALERT "Got Major No. %d Minor no. %d\n", major, minor);

   /* Tell ther kernel, that these are my functions*/
   cdev_init(&cdev, &mydev_ops);
   err = cdev_add(&cdev, devno, 1);
   if(err) {
         printk(KERN_ALERT "Error in cdev_add \n");
         return EINVAL;
   }
   return 0;
}
void mydev_exit(void ) {
   printk(KERN_ALERT "Finishing: In mydev_exit\n");
   cdev_del(&cdev); 
   unregister_chrdev_region(devno, 1);
}
module_init(mydev_init);
module_exit(mydev_exit);

4)      Write makefile to compile driver program
obj-m += mydev.o
all:
                make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
                #make -C /lib/modules/2.6.39.4/build M=$(PWD) modules
clean:
                #make -C /lib/modules/2.6.39.4/build M=$(PWD) clean
                make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
5)      $make
6)      $insmod  mydev.ko
7)      Write user program using that driver

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main() {
                int fd, len;
                char ch[128];

                scanf("%s", ch);
                len = strlen(ch);
                fd = open("/dev/mydev", O_WRONLY);
                printf("Got fd = %d\n", fd);
                write(fd, ch, len -1);
                printf("Got char %s \n", ch);
                close(fd);
                strcpy(ch, "XYZ");
                fd = open("/dev/mydev", O_RDONLY);
                printf("Got fd = %d\n", fd);
                read(fd, ch, len -1 );
                printf("Got char %s \n", ch);
                close(fd);
                return 0;
}
8)       $rmmod mydev 

Tuesday, December 18, 2012

Hadoop MapReduce

The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-avaiability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-availabile service on top of a cluster of computers, each of which may be prone to failures.

Plagiarism Checker for Free  http://plagiarisma.net

Saturday, April 7, 2012

mmap to implement copy command

/*
Program to Implement copy using mmap

*/

#include
#include
#include
#include
#include
#include
#include
#include
struct stat statbuf;

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

int fdin, fdout, ret;
char *src, *dest;
if(argc < 3) {
printf("\n Check number of arguments\n");
exit(EXIT_FAILURE);
}
fdin = open(argv[1], O_RDONLY);
if(fdin < 0) {
printf("\n Cant open source file \n");
exit(EXIT_FAILURE);
}

ret=fstat(fdin,&statbuf);
if(ret < 0 ) {
printf("\nfstat system call failed ");
exit(EXIT_FAILURE);
}
printf("\nInpute File size is - %lu ",statbuf.st_size);
src=(char*)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin,0);
fdout = open(argv[2],O_RDWR | O_CREAT | O_TRUNC,S_IRWXU | S_IRWXO);
if(fdout < 0) {
printf("\n Cant open source file");
exit(EXIT_FAILURE);
}
ret=lseek(fdout,statbuf.st_size-1,SEEK_SET);
if(ret < 0)
printf("\n lseek failed ");
ret=write(fdout,"",1);
if(ret < 0)
printf("\n write failed ");
dest=(char *)mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout,0);
dest=memcpy(dest,src,statbuf.st_size);
if(!dest)
printf("\n memcpy failed ");
// msync((void *)dest,statbuf.st_size,MS_ASYNC);
printf("\nFile copied succefully\n");
printf("\n");
}

Thursday, April 5, 2012

Description of fget_light and fput_light

Hi friends 
Description of fget_light, which is currently incorrect
about needing a prior refcnt (judging by the way it is actually used).




/*
- * Lightweight file lookup - no refcnt increment if fd table isn't shared.
- * You can use this only if it is guranteed that the current task already
- * holds a refcnt to that file. That check has to be done at fget() only
- * and a flag is returned to be passed to the corresponding fput_light().
- * There must not be a cloning between an fget_light/fput_light pair.
+ * Lightweight file lookup - no refcnt increment if fd table isn't shared.
+ *
+ * You can use this instead of fget if you satisfy all of the following
+ * conditions:
+ * 1) You must call fput_light before exiting the syscall and returning control
+ * to userspace (i.e. you cannot remember the returned struct file * after
+ * returning to userspace).
+ * 2) You must not call filp_close on the returned struct file * in between
+ * calls to fget_light and fput_light.
+ * 3) You must not clone the current task in between the calls to fget_light
+ * and fput_light.
+ *
+ * The fput_needed flag returned by fget_light should be passed to the
+ * corresponding fput_light.

Friday, March 30, 2012

Test Cases for read system call

Hi Everyone

following are the some of test cases if anyone has (idea) more test cases then plz share it

Three Test Cases
test case 1:
Give Bad file descriptor as input to the read and expect EBADF ,read return -1
test case 2:
Give descriptor of directory as input to the read and excpect EISDIR,read reurns -1
test case 3:
Give Bad address input to the read and excpect EFAULT,read reurns -1


Code for these test cases
/*
MIS NO-121122019
Testing Read System call

*/
#include
#include
#include
#include

int badfd=-1;
int fd2,fd3;
char buffer[1024];

struct test_case_t {
int *fd;
char *buf;
int error;
}TC[] = {
{&badfd,buffer,EBADF},
{&fd2,buffer,EISDIR},
{&fd3,(void *)-1,EFAULT}
};

int TST_TOTAL=sizeof(TC)/sizeof(*TC);

int main(int argc,char *argv[])
{
//badfd=-1 holding bad file descriptor
//fd2 for holding directory
//fd3 for holding normal file
int retval,i;
char ch;
if(argc < 2){
printf("\n Check arguments ..");
exit(1);
}
fd2=open(argv[1],O_RDONLY);
if(fd2 < 0) {
printf("\n Cant open directory ");
exit(1);
}
fd3=open(argv[2],O_RDONLY);
if(fd3 < 0) {
printf("\n Cant open directory ");
exit(1);
}

for(i = 0; i < TST_TOTAL; i++){
retval=read(*TC[i].fd,TC[i].buf,1);
if(retval != -1 && errno != TC[i].error)
{
return i+1;
} /*else {
printf("\n For test %d ---> retval = %d errno =%d",i+1,retval,errno);
}*/
}
return 0;
}

#shell file
#!/bin/bash

#test2 prepration create regular file
touch file
ls -l > file
#test3 prepration create directory
mkdir dir
# cc read01.c -o bad_fd_dir_addr
./bad_fd_dir_addr dir file
a=$?
if [ $a -eq 0 ] ; then
echo "TEST_CASE_SUCESS"
else
echo "TEST_CASE_FAILURE"
fi

rm file
rmdir dir






Thank You.

Friday, March 23, 2012

Adding System Call

Hi

These are the steps to add system call to the linux kernel 2.6.39.4


  1. Download source code http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.4.tar.bz2

  2. Open file linux-2.6.39/arch/x86/kernel/syscall_table_32.S

    add line

    .long sys_add2


  1. Open linux-2.6.39/arch/x86/include/asm/unistd_32.h

After Line

#define __NR_syncfs 344

Add line

#define __NR_add2 345

and also change NR_syscalls to

#define NR_syscalls 346


  1. Now edit linux-2.6.39/arch/x86/include/asm/unistd_64.h

    Find Lines

    #define __NR_syncfs 306

    __SYSCALL(__NR_syncfs, sys_syncfs)

    Add Line

    #define __NR_add2 307

    __SYSCALL(__NR_add2,sys_add2)


  1. open file linux-2.6.39/include/linux/syscalls.h

    before #endif add

    asmlinkage long sys_add2(int i,int j);


  1. Now create add2.c

    linux-2.6.39/kernel/add2.c

    #include

    asmlinkage long sys_add2(int i,int j)

    {

    return i+j;

    }

  2. Now open the Makefile in this folder(linux-2.6.39/kernel/Makefile) and find out

    obj-y += groups.o

    Add a new line before this line :

    obj-y += add2.o

  3. Now its Time to compile kernel

    Go to linux-2.6.39/

    $make

  4. Start compiling to kernel modules:


$make modules

  1. Install kernel modules

    su -

    $make modules_install

  2. It is time to install kernel itself

    $make install

  3. $update-grub