我如何检查我的包装功能的系统调用 - 阅读()是正确的? [重复](How can I chec

2019-09-16 16:13发布

可能重复:
对于包装函数内联汇编不能出于某种原因

我要求写的包装功能read , write , close , open & fork

我已经写了4种包装功能的read , write , close , open

我的问题是:

  1. 我怎么能写包装函数fork ,使用,我写了4包装函数read , write , close & open

  2. 如何检查,如果是我写的包装是正确的? 下面是的包装函数的代码read -所谓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;
}

备注 :我不能直接使用的open ,close ,read , write & fork命令。

我可以附加其他3个包装的代码的其余部分,如果需要的话。 以上是用于包装read

问候

罗恩

Answer 1:

叉应该是系统调用2,所以

    __asm__ volatile ("int $0x80" : "=a" (res) : "0" (2)); 

应该管用。 请记住,fork函数返回两次, res是孩子的PID(父)和0(在儿童)。



文章来源: How can I check if my wrapper function system call - read() is correct? [duplicate]