2 Replies - 456 Views - Last Post: 21 April 2012 - 09:55 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

How can I check if my wrapper function for read() is correct ?

Posted 21 April 2012 - 07:05 AM

I'm requested to write wrapper function for `read , write , close , open & fork` .

I've written 4 wrapper functions for `read , write , close , open` .

My questions are :

1. 1. How can I write wrapper function for `fork` ? I must not use a direct call for fork() , what I can use is the same method as I used for read() , below . Is it the same ? just get the number of the system call and use inline assembler for that ?

2. How can I check if the wrapper that I wrote is correct ? Here is the code for the wrapper function of `read` - called `my_read` :

   ssize_t my_read(int fd, void *buf, size_t count)   
    {    
    	  ssize_t res;
    
    	  __asm__ volatile(
    		"int $0x80"        /* make the request to the OS */
    		: "=a" (res),       /* return result in eax ("a") */
    		  "+b" (fd),     /* pass arg1 in ebx ("b") */
    		  "+c" (buf),     /* pass arg2 in ecx ("c") */
    		  "+d" (count)      /* pass arg3 in edx ("d") */
    		: "a"  (5)          /* passing the system call for read to %eax , with call number 5  */
    		: "memory", "cc"); 
    
    	  /* The operating system will return a negative value on error;
    	   * wrappers return -1 on error and set the errno global variable */
    
    	  if (-125 <= res && res < 0)
    	  {
    		errno = -res;
    		res   = -1;
    	  }
    
    	  return res;
    }


Remarks : I'm not allowed to use directly the `open ,close ,read , write & fork` commands .

I can attach the rest of the code of the other 3 wrappers , if it is needed , so please say so if they are required . The above is the wrapper for `read`.

Regards

Ron

This post has been edited by passer_by: 21 April 2012 - 07:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I check if my wrapper function for read() is correct ?

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: How can I check if my wrapper function for read() is correct ?

Posted 21 April 2012 - 09:22 AM

> Is it the same ? just get the number of the system call and use inline assembler for that ?
Well it's the same idea, but you might need to set up some additional information.
Read the syscall documentation for fork() in detail to see what it expects/returns.

> 2. How can I check if the wrapper that I wrote is correct ?
Write some code.
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
  if ( fork() == 0 ) {
    write(1,"From Child.\n",12);
  } else {
    write(1,"From Parent\n",12);
  }
  return 0;
}


Replace with my_ and re-test.
Do you see the same results?
Was This Post Helpful? 0
  • +
  • -

#3 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: How can I check if my wrapper function for read() is correct ?

Posted 21 April 2012 - 09:55 AM

Salem ,I think there's something wrong with my system call .
My my_read system call goes into failing mode (res returns -1) when I go into my_read .

Something is wrong with the code I guess . Any idea what ? I've tried to fix it but nothing works.

thanks !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1