Total Pageviews

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